Reputation: 1030
i want to get array of input from user and store it to database on the same page is it possible???
<!doctype html>
<html>
<head>
<title>sql</title>
</head>
<body>
<?php
$mysqli=new mysqli("localhost","root","hiitisme","data");
$name=$mysqli->escape_string($_POST['name']);
$sql="INSERT INTO user(name)VALUES ('$name')";
$mysqli->query($sql);
?>
<script type="text/javascript">
function show() {
<?php
$my=new mysqli("localhost","root","hiitisme","database");
$sq="SELECT name FROM user";
$result=$my->query($sq);
echo $result[0];
?>
}
</script>
<form method="post">
<input type="text" name="name">
<input type="submit" value="submit">
<input type="button" value="show" onClick="show();" />
</form>
</body>
</html>
this code is correct or wrong please help me..
Upvotes: 1
Views: 122
Reputation: 765
i want to get array of input from user and store it to database on the same page is it possible???
Yes
this code is correct or wrong please help me..
It's wrong on both logic and syntax parts
PHP does not work the way you think it does.
<script type="text/javascript">
function show()
{
<?php
$my=new mysqli("localhost","root","hiitisme","database");
$sq="SELECT name FROM user";
$result=$my->query($sq);
echo $result[0];
?>
}
This will only execute javascript function with invalid content (that is username you queried from database) when you click show button. The php script will be run at time of page generation. For it to execute on server you would have to do rpc (search ajax) and write handlers for both server and client.
To answer the question, array input from HTML form may be gotten simply as
<input type="text" name="name[0]">
<input type="text" name="name[1]">
<input type="text" name="name[2]">
However for this to work you would have to have client-side script which will create new inputs every time you want add new user (and it's bad idea anyway).
<?php
$mysqli=new mysqli("localhost","root","hiitisme","data");
$name=$mysqli->escape_string($_POST['name']);
$sql="INSER INTO user(name)VALUES ('$name')";
$mysqli->query($sql);
?>
Now you need to fix this to actually store array, escaping should be per item, and array should be serialized into VALUES compatible format (that is 'A','B','C'
$name = implode("','"array_map($mysqli->escape_string, $_POST['name']));
$sql = "INSERT INTO user(name) VALUES ('$name')";
You should also check if the page is actually generated (that is it should show add password) or it should store data (simply check if $_POST['name'] exists...
Upvotes: 1
Reputation:
You can try jQuery POST
Before try this, please read about jQuery Post
form.php
<form action="" method="post" onsubmit="processForm(this);return false;">
<input type="text" name="name" value="">
<input type="submit" name="submit" value="submit" >
</form>
<div id='message'></div>
<script type='text/javascript'>
function processForm(form) {
$.ajax( {
type: 'POST',
url: form_process.php,
data: $(form).serialize(),
success: function(data) {
$('#message').html(data);
}
} );
return false;
}
</script>
form_process.php
<?php
// make sure your connection and insert query
$mysqli = new mysqli("localhost","root","hiitisme","data");
$name = $mysqli->escape_string($_POST['name']);
$sqlInsert = "INSER INTO user(name)VALUES ('$name')";
$mysqli->query($sqlInsert);
$sqlSelect = "SELECT name FROM user";
$result=$my->query($sqlSelect);
echo $result[0];
?>
Hope, this will work...
Upvotes: 0
Reputation: 414
<!doctype html>
<html>
<head>
<title>sql</title>
</head>
<body>
<?php
$mysqli=new mysqli("localhost","root","hiitisme","data");
$name=$mysqli->escape_string($_POST['name']);
$sql="INSERT INTO user(name)VALUES ('$name')"; // query now is correct. ( you write INSER )
$mysqli->query($sql);
?>
<script type="text/javascript">
function show()
{
<?php
$my=new mysqli("localhost","root","hiitisme","database");
$sq="SELECT name FROM user";
$result=$my->query($sq);
echo $result[0];
?>
}
</script>
<form method="post">
<input type="text" name="name">
<input type="submit" value="submit">
<input type="button" value="show" onClick="show()">
</form>
</body>
</html>
Upvotes: 0