我尝试用ajax提交我的表单而没有重新加载整个页面,但是ajax不能正常工作,这里是我使用的代码
$("#sendingform").submit(function(){
var mess = $("#urmess").val();
var mid = "1";
alert (mid);
$.ajax({
url: 'ajax/send.php',
data: { mid: mid, mess: mess},
success: function (data){
alert(data);
}
});
return false;
});
html是
<hr/>
<form action="#" id="sendingform" method="post">
<textarea id="urmess" class="conposer" name="messtxt"></textarea>
<input type="submit" class="sendbtn" name="go" class="send" value="Send"/>
</form>
答案 0 :(得分:0)
试试这个
$(function(){ /* Execute when the DOM is ready */
$(document).on("submit","#sendingform",function(e){
e.preventDefault(); /* Prevent the default action of the form */
var mess = $("#urmess").val(); /* Get the textarea value */
var mid = "1";
alert (mid); /* Alert 1 */
$.ajax({
url: 'ajax/send.php',
data: {
mid: mid,
mess: mess
}, /* Define AJAX data */
success: function (data){
alert(data); /* Alert return */
}
});
});
});
答案 1 :(得分:0)
Here's The Code Hows Your PHP Code Must Look Like :
<?php
$a=json_decode($_POST["Jdata"]);
$con=mysql_connect("localhost","root","");
mysql_select_db("json",$con);
$t=$a->mess;
$u=$a->mid;
$x=mysql_query("insert into test(data1,data2) values('$t','$u')");
?>
And Here's HTML+JQuery Code
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
</head>
<body>
<form action="#" id="sendingform" method="post">
<textarea id="urmess" class="conposer" name="messtxt"></textarea>
<input type="submit" class="sendbtn" name="go" class="send" value="Send"/>
</form>
<script>
$("#sendingform").submit(function(){
var mess = $("#urmess").val();
var mid = "1";
alert (mid);
$.ajax({
type: "POST",
url: 'ajax/send.php',
dataType: "json",
data:{Jdata:JSON.stringify({'mid': mid,'mess': mess})},
success: function (data){
console.log(data);
}
});
return false;
});
</script>
</body>
</html>