使用Ajax使用php将记录插入mysql数据库
如何使用Ajax编写此代码。请帮助。 我在这里是Bignner,我已经编写了这段代码,但是我想使用ajax,因为不想重新加载页面……?
PHP文件
//Code For Making Form And getting Data….. Fill -ID,NAME,EMAIL_ID,PASSWORD,CREDITS,
//从表单数据中获取数据的代码。
我试过了……是Blewo ……
file for html and ajax Fill -ID,NAME,EMAIL_ID,PASSWORD,CREDITS,
//on the click of the submit button $("#btn_submit").click(function(){ //get the form values var ID = $('#ID').val(); var NAME = $('#NAME').val(); var PASSWORD = $('#PASSWORD').val(); var CREDITS = $('#CREDITS').val(); var EMAIL_ID = $('#EMAIL_ID').val(); var CREATED_ON = $('#CREATED_ON').val(); var MODIFIED_ON = $('#MODIFIED_ON').val(); //make the postdata var postData = '&ID='+ID+'&NAME='+NAME+'&PASSWORD='+PASSWORD+'&REDITS'+CREDITS+'&EMAIL_ID'+EMAIL_ID+'&CREATED_ON'+CREATED_ON+'&MODIFIED_ON'+MODIFIED_ON; //call your .php script in the background, //when it returns it will call the success function if the request was successful or //the error one if there was an issue (like a 404, 500 or any other error status) }); $.ajax({ url : "Form_Data.php", type: "POST", data : postData, success: function(data,status, xhr) { //if success then just output the text to the status div then clear the form inputs to prepare for new data $("#status_text").html(data); $('#ID').val(); $('#NAME').val(''); $('#PASSWORD').val(''); $('#EMAIL_ID').val(''); $('#CREATED_ON').val(''); $('#MODIFIED_ON').val(''); } });
更改您的脚本,因为您的ajax在click函数之外
//on the click of the submit button $("#btn_submit").click(function(){ //get the form values var ID = $('#ID').val(); var NAME = $('#NAME').val(); var PASSWORD = $('#PASSWORD').val(); var CREDITS = $('#CREDITS').val(); var EMAIL_ID = $('#EMAIL_ID').val(); var CREATED_ON = $('#CREATED_ON').val(); var MODIFIED_ON = $('#MODIFIED_ON').val(); //make the postdata var postData = '&ID='+ID+'&NAME='+NAME+'&PASSWORD='+PASSWORD+'&REDITS'+CREDITS+'&EMAIL_ID'+EMAIL_ID+'&CREATED_ON'+CREATED_ON+'&MODIFIED_ON'+MODIFIED_ON; //call your .php script in the background, //when it returns it will call the success function if the request was successful or //the error one if there was an issue (like a 404, 500 or any other error status) $.ajax({ url : "Form_Data.php", type: "POST", data : postData, success: function(data,status, xhr) { //if success then just output the text to the status div then clear the form inputs to prepare for new data $("#status_text").html(data); $('#ID').val(); $('#NAME').val(''); $('#PASSWORD').val(''); $('#EMAIL_ID').val(''); $('#CREATED_ON').val(''); $('#MODIFIED_ON').val(''); } }); });
并将您的PHP代码更改为此
在这段代码中,我只是提交你的两个输入字段,其余的你可以自己添加。 试试这个:
Fill -ID,NAME,EMAIL_ID,PASSWORD,CREDITS, $("#submit").click(function() { var name= $("#name").val(); var password= $("#password").val(); $.ajax({ type: "POST", url: "your_php_path.php", data: 'name=' + name+ '&password=' + password, success: function(result) { alert(result); } }); });