在使用jquery提交表单时,我们从右侧div框中获取警报 并且表单已提交但表单值未传递给php文件。在PHP中回应$ _POST的值我们什么也得不到。
html文件:
<!DOCTYPE html>
<html>
<head>
<title>Submit Form Without Refreshing Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link href="check5.css" rel="stylesheet">
<script src="check3.js"></script>
<script>
function ajax2()
{
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status == 200){
document.getElementById('me3').innerHTML = req.responseText;
}
}
req.open('GET','allfriend.php','true');
req.send();
}
</script>
</head>
<body onload="ajax2();">
<div id="mainform">
<h2>Submit Form Without Refreshing Page</h2>
<!-- Required Div Starts Here -->
<form id="form" name="form">
<h3>Fill Your Information!</h3>
<label>Name:</label>
<input id="name" placeholder="Your Name" type="text">
<label>Message:</label>
<textarea id="msg" placeholder="Your message..">
</textarea>
<input id="submit" type="button" value="Send">
</form>
</div>
<div id="me">
<div id="me1" style="overflow-y:auto;overflow-x:hidden;">
<div id="me3"></div>
</div>
<div id="me2">
<form id="form1" name="form1">
<input id="name1" placeholder="Your Name" type="text">
<input id="submit1" type="button" value="Send">
</form>
</div>
</div>
</body>
</html>
这是我的jquery文件,它提交表单数据而不刷新页面。
** Jquery文件:**
$(document).ready(function() {
$("#submit").click(function() {
var msg = $("#msg").val();
if (msg == '') {
alert("Insertion Failed Some Fields are Blank....!!");
} else {
// Returns successful data submission message when the entered information is stored in database.
$.post("check2.php", {
msg1: msg
}, function(data) {
$('#form')[0].reset(); // To reset form fields
});
}
});
});
$(document).ready(function() {
$("#submit1").click(function() {
var name1 = $("#name1").val();
if (name1 == '') {
alert("insert this blank!");
} else {
// Returns successful data submission message when the entered information is stored in database.
$.post("allfriend.php", {
nam1: name1
}, function(data) {
$('#form1')[0].reset(); // To reset form fields
});
}
});
});
php文件:
<?php
include 'db.php';
$friends=$_POST['nam1'];
$q="select first_name from registry where first_name like '$friends%' || user_name='$friends%'";
$run = $db->query($q);
while($row = $run->fetch_array()) :
?>
<div id="chat_data">
<span><?= $row['first_name'].''; ?></span>
</div>
<?php endwhile; ?>