158
rated 0 times
[
161]
[
3]
/ answers: 1 / hits: 17379
/ 11 Years ago, sun, june 16, 2013, 12:00:00
I have html form with three values fname,lname,age. I want to send it on server side php file and insert into database.
My html form is like this:
<html>
<head>
<script>
function insert(fname,lname,age)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(txtHint).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open(GET,ajax_db_php.php?fname=fname&lname=lname&age=age,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<table>
<tr><td>First Name : </td><td> <input type=text fname=fname/> </td> </tr>
<tr><td>Last Name : </td><td> <input type=text fname=lname/> </td> </tr>
<tr><td>City : </td><td> <input type=text fname=age/> </td> </tr>
<input type=submit onclick=insert(fname,lname,age)>
</table>
</form>
</body>
</html>
As I have used ajax it should not load entire page, but when I click submit button it load entire page. why?
And php page which receives values and insert into database is:
<html>
<body>
<?php
$fname=$_GET['fname'];
$lname=$_GET['lname'];
$age=$_GET['age'];
//echo firstname : $fname;
$con=mysqli_connect('127.0.0.1:3306' ,'root','root','my_db');
if (mysqli_connect_errno())
{
echo Failed to connect to MySQL: . mysqli_connect_error();
}
$sql=INSERT INTO table1 (fname, lname, age)
VALUES
('$_POST[fname]','$_POST[lname]','$_POST[age]');
$result=mysql_query($sql);
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo 1 record added;
mysqli_close($con);
?>
</body>
</html>
When I click the submit button it shows nothing, no error and no update in database too.
More From » php