I am trying to insert data from form to database without refreshing page, my HTML page looks like this:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<form name="form1" method="post" action="">Enter Name
<input type="text" name="t1" id="t1">
</td>
<br>Enter City
<input type="text" name="t2" id="t2">
</td>
<br>
<input type="button" name="button1" value="Insert to DB" onClick="aa()">
</form>
<div id="d1"></div>
<script type="text/javascript">
function aa() {
var xmlhttp = new XMLhttpRequest();
xmlhttp.open("GET", "insert.php?name=" + document.getElementById("t1").value + "&city" + document.getElementById("t2").value, false);
xmlhttp.send(null);
document.getElementById("d1").innerHTML = xmlhttp.responseText;;
}
</script>
</body>
</html>
and my insert.php looks like:
<?php
$name = $_GET["name"];
$city = $_GET["city"];
mysql_connect("localhost","root", "");
mysql_select_db("dwh");
mysql_query("insert into table1 values('$name','$city')");
echo "Record inserted";
?>
In my opinion this should send PHP all data in "get" and php should just take it and insert it. Somehow it's not. Maybe i miss something, can you help me to find a problem?
browser console say:
(index):16 Uncaught ReferenceError: XMLhttpRequest is not defined(index):16 aa(index):9 onclick
Thank you a lot.
p.s. I understand my MYSQL connect is security risk, this was created just to test how to make it work.
method="post"mysqlextension, it's depreacted, and has been for some time. Read the red warning on the man, and click the links. Learn to use (and love)PDOand/ormysqli(theiis for improved). Those are the replacement extensions