我正在尝试使用jquery将表单数据发送到php文件 但当我按下按钮发送这些数据时,没有任何事情可以发生
jquery代码是
$('#submit').click(function(e)
e.preventDefault() //Prevent the default action of the form.
$.post( //Send a POST request to the PHP file via AJAX
"path/to/file.php", //Path to the PHP file where you want to send the POST
$(.contact-form).serializeArray().reduce(function(obj, item) {
obj[item.name] = item.value;
return obj;
}, {}) // This takes all the form fields and returns key-value pairs of form data
, function (data) { //data = response from the PHP file. In your case "Sucess"
if(data==="success"){
//do something with data. Have assumed you want to append it to a div called "response". You will need to add <div id="response"> </div> below your form
$('#response').html(data);
} else {
$('#response').html("Sorry Try Again");
}
//Alternatively you can just append data to response and not have this IF
});
)
发送数据的save.php代码是
$(document).ready(function(){
$("#daily_user_but").click(function(){
//event.preventDefault();
$('#result').html('<img src="images/ajax_loader_red_128.gif"/>');
var jusername = $('#usernamed').val();
var jpassword = $('#passwordd').val();
var jadsoyad=$('#adsoyadd').val();
var jtcno = $('#tcnod').val();
var jtelefon = $('#telefond').val();
var jmir=$('#mirror_field').val();
//syntax - $.post('filename', {data}, function(response){});
$.post('save.php',{id: "NewDailyUser", username:jusername, password:jpassword,tcno:jtcno,telefon:jtelefon,expair:jmir,adsoyad:jadsoyad},function(res){
$('#result').html(res);
});
});
});
答案 0 :(得分:0)
你应该使用jQuery Form plugin非常容易使用
示例:强>
<!-- Get processed form result -->
<div id="resultOfMyProcessedForm"></div>
<!-- Your Form -->
<form action="path/to/save.php" method="
post" id="myForm">
<!-- Form content will go here -->
</form>
<!-- include reuired libraries -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.1/jquery.form.min.js" integrity="sha256-8G/BdtcUMWw3c6j5nBvVtzaoj3sq/kX6xNN2FQ0w0MY=" crossorigin="anonymous"></script>
<!-- Script to process your form -->
<script>
$('#myForm').ajaxForm(function(response){
$('#resultOfMyProcessedForm').html(response);
});
</script>