insert data into database with ajax

Discussion in 'PHP' started by roice, Jul 2, 2012.

  1. #1
    Hello,
    I'm looking for a simple example of insert form data into my db using ajax
    if all good I will get success text message and if not than fail message (not ALERT message).

    Can you please help me?
    Thanks in advanced
    Roi
     
    roice, Jul 2, 2012 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
  3. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you,
    But I'm looking for something much more simple than that. Do you have one?
     
    roice, Jul 2, 2012 IP
  4. aman179

    aman179 Greenhorn

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    You can visit W3Schools, great source of learning.
    http://www.w3schools.com/php/php_ajax_php.asp
    Code (markup):
     
    aman179, Jul 4, 2012 IP
  5. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OK, so I built this code, but I don't get any result:

    <html>
    <head>
    <script language="javascript" type="text/javascript">
    <!-- 
    //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;
          }
       }
     }
     // Create a function that will receive data 
     // sent from the server and will update
     // div section in the same page.
     ajaxRequest.onreadystatechange = function(){
       if(ajaxRequest.readyState == 4){
          var ajaxDisplay = document.getElementById('ajaxDiv');
          ajaxDisplay.value = ajaxRequest.responseText;
       }
     }
     // Now get the value from user and pass it to
     // server script.
     var x = document.getElementById('x').value;
     var y = document.getElementById('y').value;
    
     var queryString = "?x=" + x ;
     queryString +=  "&y=" + y;
     ajaxRequest.open("GET", "2.php" + 
                                  queryString, true);
     ajaxRequest.send(null); 
    }
    //-->
    </script>
    </head>
    <body>
    
    <form name='myForm'>
    
    <input name='x' />
    <input name='y' />
    
    <input type='button' onclick='ajaxFunction()' value='Query MySQL'/>
    							  
    </form>
    <br />
    <div id='ajaxDiv'>Your result will display here</div>
    
    </body>
    </html> 
    PHP:

    in 2.php I just put:

    <?PHP
    error_reporting(E_ERROR | E_WARNING | E_PARSE);
    
    $x=$_GET["x"];
    $y=$_GET["y"];
    
    echo $x;
    echo $y;
    
    ?> 
    PHP:
    What did I miss?
     
    roice, Jul 4, 2012 IP
  6. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #6
    Jquery has a very simple AJAX call with only a few lines of code. You may want to look into that as it already is supported by all major browsers and performs the necessary error handling and checks
     
    NetStar, Jul 4, 2012 IP