www.webdeveloper.com
Results 1 to 8 of 8

Thread: insert data into database with ajax

  1. #1
    Join Date
    Oct 2010
    Posts
    14

    insert data into database with ajax

    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

  2. #2
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,111
    You'll need to use a server side language such as PHP to insert data into the database. AJAX is simply used to call/pass information to the server script.
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  3. #3
    Join Date
    Oct 2010
    Posts
    14
    Thanks, I knew that
    Can you please send me one simple example ?

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    See for example this page...

  5. #5
    Join Date
    Oct 2010
    Posts
    14
    OK, so I built this code, but I don't get any result:

    PHP Code:
    <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 document.getElementById('x').value;
     var 
    document.getElementById('y').value;

     var 
    queryString "?x=" ;
     
    queryString +=  "&y=" y;
     
    ajaxRequest.open("GET""2.php" 
                                  
    queryStringtrue);
     
    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> 

    in 2.php I just put:

    PHP Code:
    <?PHP
    error_reporting
    (E_ERROR E_WARNING E_PARSE);

    $x=$_GET["x"];
    $y=$_GET["y"];

    echo 
    $x;
    echo 
    $y;

    ?>
    What did I miss?

  6. #6
    Join Date
    Mar 2012
    Location
    Saint-Petersburg, Russia
    Posts
    97
    but I don't get any result
    Explain what you mean by this. What is "no result" in your opinion?

    Which server and php-interpreter you used?

  7. #7
    Join Date
    Oct 2010
    Posts
    14
    well, DIV:
    <div id='ajaxDiv'>Your result will display here</div>

    supposed to print the input of var $x and $y
    PHP Code:
    $x=$_GET["x"];
    $y=$_GET["y"];

    echo 
    $x;
    echo 
    $y
    but i get nothing...

    about your secon question - I didn't understand it...

  8. #8
    Join Date
    Mar 2012
    Location
    Saint-Petersburg, Russia
    Posts
    97
    As for my second question, you could not expect PHP script to be executed and produce any meaningful result if you do not use php-processor on server side (for example if you are loading your files into browser from the drive of your local machine).

    However even without php-script being processed, you should at least see your request being send and response being received. You should see it either in debugger (like FireBug or built-in Chrome) or by adding alert to block where you are processing response.

    There are a lot of ways to fail - you may incorrectly place your files or this method of sending request does not work with your browser (why not using jquery.ajax?) However you need to debug processing of javascript to understand what is happening and then if you want, ask for more hints... &#37

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles