3

How to do coding of this Code With Using Ajax.Please Help. I am Bignner here and i have written this code it's working but i want to use with ajax this because don't want to reload the page...?

PHP File

    //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>

//code for taking data from form data.

<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>

I have Tried Yet ... Is Blewo...

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>

code for query...

<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>
6
  • What if my password is pas'word123? Take a look at PDO or how to avoid mysql injection stackoverflow.com/questions/60174/… Commented Mar 18, 2015 at 10:51
  • 2
    Could you show us what you have tried so far? We are here to help not to write your code for you Commented Mar 18, 2015 at 10:52
  • What are you planning to use for AJAX? jQuery? Have you tried anything so far? Commented Mar 18, 2015 at 10:54
  • yet i have done some coding shows edited... Commented Mar 18, 2015 at 11:03
  • not getting that what are mistakes ...? Commented Mar 18, 2015 at 11:19

3 Answers 3

5

I have Solved It ... How To Use Ajax and 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>
Sign up to request clarification or add additional context in comments.

Comments

2

change your script because your ajax was outside the click function

//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>

and change your php code to this

<?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);
?>

Comments

0

In this code I'm just submitting your two input fields, the rest you can add by yourself. Try this:

 <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);
                }
            });


        });

5 Comments

make sure you give index name correct in php file $name =$_POST['name']; $PASSWORD =$_POST['password']
email me your html page and php page .. i will fix [email protected]
check my new answer ..its working at my end .. you can check also by adding a line just after if(isset(---)){ print_r($_POST); and alert result in script
Please Check Youar Inbox.. (Mail)
check above answer problem is fixed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.