Вставка записей в базу данных mysql с использованием php с использованием Ajax

Как сделать кодировку этого кода с помощью справки Ajax.Please. Я здесь Bignner, и я написал этот код, он работает, но я хочу использовать с ajax, потому что не хочу перезагружать страницу …?

Файл PHP

//Code For Making Form And getting Data….. <html> <body> Fill -ID,NAME,EMAIL_ID,PASSWORD,CREDITS, <form action="Form_Data.php" method="post"> ID: <input type="text" name="ID"><br><br> NAME: <input type="text" name="NAME"><br><br> PASSWORD: <input type="text" name="PASSWORD"><br><br> CREDITS: <input type="text" name="CREDITS"><br><br> E_mail: <input type="text" name="EMAIL_ID"><br><br> CREATED_ON:<input type="text" name="CREATED_ON"><br><br> MODIFIED_ON:<input type="text" name="MODIFIED_ON"><br><br> <input type="submit"> </form> </body> </html> 

// код для взятия данных из данных формы.

 <html> <?php include 'connnect.php'; mysql_set_charset('utf8'); //query for insert data into tables $ID = $_POST['ID']; $NAME =$_POST['NAME']; $EMAIL_ID =$_POST['EMAIL_ID']; $PASSWORD =$_POST['PASSWORD']; $CREDITS =$_POST['CREDITS']; $CREATED_ON=$_POST['CREATED_ON']; $MODIFIED_ON=$_POST['MODIFIED_ON']; $query = "INSERT INTO `user_table` (`ID`,`NAME`,`EMAIL_ID`,`PASSWORD`,`CREDITS`,`CREATED_ON`,`MODIFIED_ON`) VALUES ('$ID','$NAME','$EMAIL_ID','$PASSWORD','$CREDITS','$CREATED_ON','$MODIFIED_ON')"; $query_run= mysql_query($query); $retval=mysql_query($query,$conn); if ($query_run) { echo 'It is working'; } mysql_close($conn); ?> </html> 

Я все еще пробовал … Это Блево …

 file for html and ajax <html> <HEAD> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> </HEAD> <body> <div id="status_text"> Fill -ID,NAME,EMAIL_ID,PASSWORD,CREDITS, <form onsubmit="return false" method="post"> ID: <input type="text" id="ID" name="ID"><br><br> NAME: <input type="text" id="NMAE" name="NAME"><br><br> PASSWORD: <input type="text" id= "PASSWORD"name="PASSWORD"><br><br> CREDITS: <input type="text" Id= "CREDITS"name="CREDITS"><br><br> Email_ID: <input type="text" id="Email_ID"name="EMAIL_ID"><br><br> CREATED_ON:<input type="text" id="CREATED_ON" name="CREATED_ON"><br><br> MODIFIED_ON:<input type="text" id="MODIFIED_ON" name="MODIFIED_ON"><br><br> <input type="submit" id="btn_submit" name="submit" value="Send"> </div> <script> //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(''); } }); </script> </form> </body> </div> </html> 

код для запроса …

 <html> <?php include 'connnect.php'; mysql_set_charset('utf8'); //query for insert data into tables $ID = $_POST['ID']; $NAME =$_POST['NAME']; $EMAIL_ID =$_POST['EMAIL_ID']; $PASSWORD =$_POST['PASSWORD']; $CREDITS =$_POST['CREDITS']; $CREATED_ON=$_POST['CREATED_ON']; $MODIFIED_ON=$_POST['MODIFIED_ON']; $query = "INSERT INTO `user_table` (`ID`,`NAME`,`EMAIL_ID`,`PASSWORD`,`CREDITS`,`CREATED_ON`,`MODIFIED_ON`) VALUES ('$ID','$NAME','$EMAIL_ID','$PASSWORD','$CREDITS','$CREATED_ON','$MODIFIED_ON')"; $query_run= mysql_query($query); $retval=mysql_query($query,$conn); if ($query_run) { echo 'It is working'; } mysql_close($conn); ?> </html> 

Я решил это … Как использовать Ajax и MYSQL … PHP CODE

 <?php include 'connnect.php'; mysql_set_charset('utf8'); //query for insert data into tables $ID = $_POST['ID']; $NAME =$_POST['NAME']; $EMAIL_ID =$_POST['EMAIL_ID']; $PASSWORD =$_POST['PASSWORD']; $CREDITS =$_POST['CREDITS']; $CREATED_ON=$_POST['CREATED_ON']; $MODIFIED_ON=$_POST['MODIFIED_ON']; $query = "INSERT INTO `user_table` (`NAME`,`EMAIL_ID`,`PASSWORD`,`CREDITS`,`CREATED_ON`,`MODIFIED_ON`) VALUES ('$NAME','$EMAIL_ID','$PASSWORD','$CREDITS','$CREATED_ON','$MODIFIED_ON')"; $query_run= mysql_query($query); // $retval=mysql_query($query,$conn); if ($query_run) { echo 'It is working'; } mysql_close($conn); ?> 

HTML FILE ….

 <html> <HEAD> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> </HEAD> <body> <div id="status_text"> Fill -ID,NAME,EMAIL_ID,PASSWORD,CREDITS, ID: <input type="text" id="ID" name="ID"><br><br> NAME: <input type="text" id="NAME" name="NAME"><br><br> PASSWORD: <input type="text" id= "PASSWORD"name="PASSWORD"><br><br> CREDITS: <input type="text" Id= "CREDITS"name="CREDITS"><br><br> Email_ID: <input type="text" id="EMAIL_ID"name="EMAIL_ID"><br><br> CREATED_ON:<input type="text" id="CREATED_ON" name="CREATED_ON"><br><br> MODIFIED_ON:<input type="text" id="MODIFIED_ON" name="MODIFIED_ON"><br><br> <input type="submit" id="btn_submit" name="submit" value="Send"/> </div> <script> <!-- //Browser Support Code function ajaxFunction(){ var ajaxRequest; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); }catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); }catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } } //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+'&CREDITS'+CREDITS+'&EMAIL_ID'+EMAIL_ID+'&CREATED_ON'+CREATED_ON+'&MODIFIED_ON'+MODIFIED_ON; // alert(postData); var myData={"ID":ID,"NAME":NAME,"PASSWORD":PASSWORD,"CREDITS":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 : myData, 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(''); } }); }); </script> </body> </div> </html> 

измените свой скрипт, потому что ваш ajax находился вне функции щелчка

 //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(''); } }); }); </script> 

и измените ваш php-код на этот

 <?php include 'connnect.php'; mysql_set_charset('utf8'); //query for insert data into tables if(isset($_POST['NAME'])){ $ID = $_POST['ID']; $NAME =$_POST['NAME']; $EMAIL_ID =$_POST['EMAIL_ID']; $PASSWORD =$_POST['PASSWORD']; $CREDITS =$_POST['CREDITS']; $CREATED_ON=$_POST['CREATED_ON']; $MODIFIED_ON=$_POST['MODIFIED_ON']; $query = "INSERT INTO `user_table` (`ID`,`NAME`,`EMAIL_ID`,`PASSWORD`,`CREDITS`,`CREATED_ON`,`MODIFIED_ON`) VALUES ('$ID','$NAME','$EMAIL_ID','$PASSWORD','$CREDITS','$CREATED_ON','$MODIFIED_ON')"; $query_run= mysql_query($query); $retval=mysql_query($query,$conn); if ($query_run) { echo 'It is working'; } } mysql_close($conn); ?> 

В этом коде я просто представляю ваши два поля ввода, остальное вы можете добавить сами. Попробуй это:

  <html> <body> Fill -ID,NAME,EMAIL_ID,PASSWORD,CREDITS, <form action="Form_Data.php" method="post"> NAME: <input id="name" type="text" name="NAME"><br><br> PASSWORD: <input id="password" type="text" name="PASSWORD"><br><br> <input type="submit" id="submit"> </form> </body> </html> $("#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); } }); });