I know this is a repetitive question, but please help me. I have this code which send form data to php using ajax, but the code doesn't work and I don't know why. Any help please ?
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script>
function chk(){
var name = document.getElementById('name').value;
var datastring = 'name = ' + name;
$.ajax({
type:"post",
url:"hi.php",
data: datastring,
cache:false,
success: function(html){
$('msg').html(html);
}
});
return false;
}
</script>
</head>
<body>
<form>
<input type="text" name="id" >
<input type="submit" value="submit" onclick="return chk()">
</form>
<p id="msg"></p>
</body>
</html>
PHP
<?php
if($_POST)
{
$name=$_POST['name'];
echo $name."<br>";
}
?>
var name = document.getElementById('name').value;. But where is the elementnamedefined? I cannot find this in your form, I see an element with the name id, but no id name...