<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="admin page.php">
<p>
<label for="Name">Name</label>
<input type="text" name="Name" id="Name" />
</p>
<p>
<label for="Age">Age </label>
<input type="text" name="Age" id="Age" />
</p>
<p>
<label for="Email">Email</label>
<input type="text" name="Email" id="Email" />
</p>
<p>
<label for="DOB">DOB</label>
<input type="text" name="DOB" id="DOB" />
</p>
<p>
<input type="button" name="add" id="add" value="Add" onclick="func();" />
</p>
</form>
<?php
function func()
{
$con = mysql_connect("127.0.0.1","root","") or die(mysql_error());
$db = mysql_select_db("lr") or die(mysql_error());
$sql=mysqli_query("INSERT INTO Persons (Name,age,Email,DOB)
VALUES ('&Name', '&Age','&Email','&DOB')");
if($sql){echo "Welcome ".$Name.",you may Login now! ";}
mysql_close($con);
}
?>
</body>
This is my code i want to run my query on this very same page! I cannot do it on other page because i want to add users recursively in this page .I called function func on click to do that but its not working.
Try something like this
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$name= $_POST['Name'];
$age= $_POST['Age'];
$email= $_POST['Email'];
$dob= $_POST['DOB'];
$sql = "INSERT INTO employee ".
"(name,age, email, dob) ".
"VALUES('$name','$age','$email','$dob')";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully
";
mysql_close($conn);
}
else
{
?>
<form id="form1" name="form1" method="post" action="<?php $_PHP_SELF ?>">
<p>
<label for="Name">Name</label>
<input type="text" name="Name" id="Name" />
</p>
<p>
<label for="Age">Age </label>
<input type="text" name="Age" id="Age" />
</p>
<p>
<label for="Email">Email</label>
<input type="text" name="Email" id="Email" />
</p>
<p>
<label for="DOB">DOB</label>
<input type="text" name="DOB" id="DOB" />
</p>
<p>
<input type="submit" name="add" id="add" value="Add" />
</p>
</form>
<?php
}
?>
</body>
</html>
Try this corrections/changes in your code:
action in form: <form id="form1" name="form1" method="post" action="admin page.php">
- no white-space between admin and page.php!
func()
. You cannot call php with onclick. Just remove that line onclick="func();"
. The form submit action will do what you want. And remove also the func() {}
and leave just the php
INSERT: change &
to $
and concatenate your variables. For example I STRONGLY suggest you add also some validation for your form inputs to avoid sql injections.
Getting the variables from the input fields: use $Age = $_POST["Age"]
to get the variable from the form post.
Your code is vulnerable to sql injection if you just replace &
to $
in your query you need to properly escape all request in mysql_*
you can use mysql_real_escape_string()
Mysql_*
api is deprecated so either use PDO or MySQLi and imo use PDO
you are also mixing tow different api mysqli_*
(mysqli_query
) and mysql_*
api which wont work
You can do this using ajax like this :
//Submit form without refreshing page
$.ajax({
type: "POST",
url: "update.php",
data: dataString,
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
Please refer HERE and jQuery.ajax()