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... - <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++;
-
}
-
}
-
?>
-
9 1547
what do you expect the AJAX request to return?
besides, if you do a GET request, where should the POST data come from?
to run the insert query which is in line 84
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?
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..
i replaced $_POST with $_GET even then i am not getting the table values..
that’s because you submit exactly 1 value "q" => "submit".
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: -
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... -
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.
AJAX only supports either post OR get arguments,
nope. or to be exact, you can use GET along with POST, though not vice versa. - // $_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)
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.
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).
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |