ajax in php Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • niths
    New Member
    • Mar 2010
    • 18

    #1

    ajax in php Problem

    hi all,
    i am having a page(test.php). so in that i am having a table and submit button. so when i click on submit button all the rows in the table should be inserted into the database(by using ajax). I had tried this but table row values are not coming. so can any one help me please...
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function loadXMLDoc(str)
    {
        if (str=="")
      {
      document.getElementById("myDiv").innerHTML="";
      return;
      }
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","test.php?q="+str,true);//calling the same page.
    xmlhttp.send();
    }
    </script>
    </head>
    </head>
    <body>
    <form action="test.php" method="post">
    <table border="1" align="center" id="customers" style="width: 90%">
    <tr>
      <th>S.no</th>
      <th>Imageid</th>
      <th>Name</th>
      <th>Size</th>
      <th>Type</th>
      <th>Assigned to</th>
    </tr>
    <?php
    include 'connection.php';
    $query= mysql_query("SELECT id,imageid,name,size,type from images where uploadeddate='07/21/2010'");
    mysql_error();
    $num=mysql_num_rows($query);
    $i=0;
        while ($i < $num)
        {
            $f1=mysql_result($query,$i,"id");
            $f2=mysql_result($query,$i,"imageid");
            $f3=mysql_result($query,$i,"name");
            $f4=mysql_result($query,$i,"size");
            $f5=mysql_result($query,$i,"type");
            ?>
            <tr>
                    <td><input type="text" name="sno" value="<?php echo $f1;?>"></td>
                    <td><input type="text" name="imageid<?php echo $i;?>" value="<?php echo $f2;?>"></td>
                    <td><input type="text" name="name<?php echo $i;?>" value="<?php echo $f3?>"></td>
                    <td><input type="text" name="size<?php echo $i;?>" value="<?php echo $f4;?>"></td>
                    <td><input type="text" name="type<?php echo $i;?>" value="<?php echo $f5;?>"></td>
                    <td><input type="text" name="assignedto<?php echo $i;?>" value="<?php echo assignedto;?>"></td>
                </tr>
                <?php
        $i++;
        }
    ?>
    <div id="myDiv"><h2></h2></div>
    <input type="button" name="submit" value="submit" onclick="loadXMLDoc(this.value)">
    </form>
    </body>
    </html>
    <?php 
    
        $q=$_GET["q"];
        if($q=='submit')
        {
        //echo $num;
        $i=0;
        while($i<$num)
        {
          echo $_POST['imageid'];
           $sql=mysql_query("insert into test (imageid,name,size,type,assignedto) values ('".$_POST['imageid'.$i]."','".$_POST['name'.$i]."','".$_POST['size'.$i]."','".$_POST['type'.$i]."','".$_POST['assignedto'.$i]."')");    
        mysql_error();
        //$num=mysql_num_rows($sql);    
        $i++;
        }
    }
    ?>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    what do you expect the AJAX request to return?

    besides, if you do a GET request, where should the POST data come from?

    Comment

    • niths
      New Member
      • Mar 2010
      • 18

      #3
      to run the insert query which is in line 84

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        then
        a) why do you return (and use!) the whole HTML page?
        b) where do the POST data come from?
        c) have you verified, that the code does what it should?

        Comment

        • niths
          New Member
          • Mar 2010
          • 18

          #5
          i verified this, my html is running twice..
          In insert query i am not getting the values to insert but it is incrementing the id with no values in it. i replaced $_POST with $_GET even then i am not getting the table values..

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            i replaced $_POST with $_GET even then i am not getting the table values..
            that’s because you submit exactly 1 value "q" => "submit".

            Comment

            • Aimee Bailey
              Recognized Expert New Member
              • Apr 2010
              • 197

              #7
              The issue is that everything works as it should, however your loadXMLDoc() function only uses GET arguments, and does not ever send POST arguments. Therefore line 83 and 84 will always fail, as the $_POST arguments are never populated.

              The W3C schools example you have been viewing doesnt really cover POST args, so here's a quick overview. AJAX only supports either post OR get arguments, this means you have to refine your project to cope with only one when adding data to the database. to send arguments using the post method, you need to alter your code as follows...

              On line 26-27:
              Code:
              var params = "q="+str;
              xmlhttp.open("POST","test.php",true);
              xmlhttp.send(params);
              Be advised also, to maintain reliability in your code, you should catch the possible error that a post argument does not exist, so around line 83, you should provide something like the following...

              Code:
              if(isset($_POST['imageid']))
              {
              
              }
              This way if it doesnt exist, you wont get an unhandled exception.

              Last note, i reccomend seporating GET args for navigation and population uses, and POST exclusively for updating your database, that way you can clearly seporate the different methodologies.

              All the best!

              Aimee.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                AJAX only supports either post OR get arguments,
                nope. or to be exact, you can use GET along with POST, though not vice versa.

                Code:
                // $_GET['param'] & $_POST['data'] available
                XHR.open("POST", "file.php?param=value", true);
                XHR.send("data=something");
                note: I have no idea, why so many people think, they have to put the data in the open() method exclusively …


                and another recommendation: there is no need for an AJAX call to query the website, it’s executed from. normally you call a script, that returns something you want (that may be the number of inserted data, a HTML chunk or even nothing, though not a whole web page)
                Last edited by Dormilich; Aug 10 '10, 08:41 AM.

                Comment

                • Aimee Bailey
                  Recognized Expert New Member
                  • Apr 2010
                  • 197

                  #9
                  Barking what is right and wrong is not going to help the person asking the question, i said OR because i believe it's safer and cleaner to only use one or the other, and for someone leraning this stuff, i believe its even more prevelant.

                  Comment

                  • pradeepkr13
                    New Member
                    • Aug 2010
                    • 43

                    #10
                    1) separate your code which returns value after DB insertion(ie table rows). Create a different small php which does this task.
                    2) Submitting to the same php will result into nested form tags in your code.
                    3) Use single request method to be easy to understand. May be post.
                    4) Do you have a single table in DB or more than one(as I can see)?
                    5) What exactly do you want (in detail).

                    Comment

                    Working...