473,395 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ajax in php Problem

18
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...
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function loadXMLDoc(str)
  5. {
  6.     if (str=="")
  7.   {
  8.   document.getElementById("myDiv").innerHTML="";
  9.   return;
  10.   }
  11. if (window.XMLHttpRequest)
  12.   {// code for IE7+, Firefox, Chrome, Opera, Safari
  13.   xmlhttp=new XMLHttpRequest();
  14.   }
  15. else
  16.   {// code for IE6, IE5
  17.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  18.   }
  19. xmlhttp.onreadystatechange=function()
  20.   {
  21.   if (xmlhttp.readyState==4 && xmlhttp.status==200)
  22.     {
  23.     document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  24.     }
  25.   }
  26. xmlhttp.open("GET","test.php?q="+str,true);//calling the same page.
  27. xmlhttp.send();
  28. }
  29. </script>
  30. </head>
  31. </head>
  32. <body>
  33. <form action="test.php" method="post">
  34. <table border="1" align="center" id="customers" style="width: 90%">
  35. <tr>
  36.   <th>S.no</th>
  37.   <th>Imageid</th>
  38.   <th>Name</th>
  39.   <th>Size</th>
  40.   <th>Type</th>
  41.   <th>Assigned to</th>
  42. </tr>
  43. <?php
  44. include 'connection.php';
  45. $query= mysql_query("SELECT id,imageid,name,size,type from images where uploadeddate='07/21/2010'");
  46. mysql_error();
  47. $num=mysql_num_rows($query);
  48. $i=0;
  49.     while ($i < $num)
  50.     {
  51.         $f1=mysql_result($query,$i,"id");
  52.         $f2=mysql_result($query,$i,"imageid");
  53.         $f3=mysql_result($query,$i,"name");
  54.         $f4=mysql_result($query,$i,"size");
  55.         $f5=mysql_result($query,$i,"type");
  56.         ?>
  57.         <tr>
  58.                 <td><input type="text" name="sno" value="<?php echo $f1;?>"></td>
  59.                 <td><input type="text" name="imageid<?php echo $i;?>" value="<?php echo $f2;?>"></td>
  60.                 <td><input type="text" name="name<?php echo $i;?>" value="<?php echo $f3?>"></td>
  61.                 <td><input type="text" name="size<?php echo $i;?>" value="<?php echo $f4;?>"></td>
  62.                 <td><input type="text" name="type<?php echo $i;?>" value="<?php echo $f5;?>"></td>
  63.                 <td><input type="text" name="assignedto<?php echo $i;?>" value="<?php echo assignedto;?>"></td>
  64.             </tr>
  65.             <?php
  66.     $i++;
  67.     }
  68. ?>
  69. <div id="myDiv"><h2></h2></div>
  70. <input type="button" name="submit" value="submit" onclick="loadXMLDoc(this.value)">
  71. </form>
  72. </body>
  73. </html>
  74. <?php 
  75.  
  76.     $q=$_GET["q"];
  77.     if($q=='submit')
  78.     {
  79.     //echo $num;
  80.     $i=0;
  81.     while($i<$num)
  82.     {
  83.       echo $_POST['imageid'];
  84.        $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]."')");    
  85.     mysql_error();
  86.     //$num=mysql_num_rows($sql);    
  87.     $i++;
  88.     }
  89. }
  90. ?>
  91.  
Aug 10 '10 #1
9 1547
Dormilich
8,658 Expert Mod 8TB
what do you expect the AJAX request to return?

besides, if you do a GET request, where should the POST data come from?
Aug 10 '10 #2
niths
18
to run the insert query which is in line 84
Aug 10 '10 #3
Dormilich
8,658 Expert Mod 8TB
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?
Aug 10 '10 #4
niths
18
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..
Aug 10 '10 #5
Dormilich
8,658 Expert Mod 8TB
i replaced $_POST with $_GET even then i am not getting the table values..
that’s because you submit exactly 1 value "q" => "submit".
Aug 10 '10 #6
Aimee Bailey
197 Expert 100+
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:
Expand|Select|Wrap|Line Numbers
  1. var params = "q="+str;
  2. xmlhttp.open("POST","test.php",true);
  3. xmlhttp.send(params);
  4.  
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...

Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['imageid']))
  2. {
  3.  
  4. }
  5.  
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.
Aug 10 '10 #7
Dormilich
8,658 Expert Mod 8TB
AJAX only supports either post OR get arguments,
nope. or to be exact, you can use GET along with POST, though not vice versa.

Expand|Select|Wrap|Line Numbers
  1. // $_GET['param'] & $_POST['data'] available
  2. XHR.open("POST", "file.php?param=value", true);
  3. 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)
Aug 10 '10 #8
Aimee Bailey
197 Expert 100+
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.
Aug 10 '10 #9
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).
Aug 25 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Joel Byrd | last post by:
I am making an AJAX call when any one of a number of inputs in a form (search criteria) are clicked, checked, changed, etc. When the user clicks, checks, whatever, I am trying to display a...
9
by: Eric Wallstedt | last post by:
I have a page that "logs" changes made to input fields using ajax to pass data to a cgi. I use POST and it works fine most of the time (all the time in IE). But it fails when I get the data from...
1
by: monudjn | last post by:
Hi I am implementing ajax in portal. Using ajax i am able to updating the content of portlets asynchronously. However i am facing a problem The Problem: While submitting the form i am getting...
2
by: K. | last post by:
Hello! I have the following problem. I have form div which is replaced by ajax event. Unofrtunately all the ajax inputs are null after posting the form (method="post") in Firefox, but on...
1
by: JohnnieTech | last post by:
I am using some javascript/ajax to load content into a main div. The problem I am running into is that it will work in IE but not in FF. In FF I don't get any sort of load at all. I have a 1...
5
by: Eyeinstyin | last post by:
Basic Problem is Every scroll of scrollbar makes an ajax call.So if user play with scrollbar say 100 times 100 ajax calls go and server starts processing 100 calls.So abort won't help.The last time...
1
by: mazdotnet | last post by:
Hi guys, On my work computer I had VisualStudio 2005, AJAX 1.0 and AJAX toolkit installed. 2 days ago I installed VisualStudio 2008 and every AJAX test that I do (IIS), does a postback. When I...
1
by: violinandviola | last post by:
I have just put 4 different ajax bits on this page: http://jimpix.co.uk/default-ajax.asp The ajax spits out chunks of images / news content, and users can view the chunks via next / prev links....
4
by: sheldonlg | last post by:
I haven't received an answer with my other post, so I am rephrasing it here. In php I have a 2D array which I "print". The headers force it to be a file on the user's system. The user receives...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.