hi all,
i had a php page which contains a list of images in a table.
When i click on submit button i those values should be inserted into database. I need this by using ajax. I had tried something but i am not getting values inserted into database.
so can any one help me.
thank you,
here is the code i had tried.

<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);
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)
    {
       $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++;
    }
}
?>

you need set POST

xmlhttp.open("GET","test.php?q="+str,true);

You are in any case receive a response.

this.value = "submit" you realy need it?

<input type="button" name="submit" value="submit" onclick="loadXMLDoc(this.value); return false;">

If I understand you correctly...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="<?php echo get_page_language($language); ?>" xml:lang="<?php echo get_page_language($language); ?>">

<head>
</head>
<body>

<?php

$res= mysql_query("SELECT id,imageid,name,size,type from images where uploadeddate='07/21/2010' LIMIT 2");
$i = 0;
while( $row = mysql_fetch_array( $res ) )
{
	if( $i == 0 ) echo "<table>";
	$i++;
	echo "
	<tr>
		<td><a href=\"#\" onclick=\"loadXMLDoc(".$row['id']."); ?>)\">".$row['name']."</a></td>
		<td><a href=\"#\" onclick=\"loadXMLDoc(".$row['id']."); ?>)\">Description: ".$row['type']."</a></td>
	</tr>";
}
mysql_freeresult($res);
mysql_close();

if( $i > 0 )
	echo "</table>";
?>

</body>
</html>

yes, i need this.value="submit" because i am having many button values. if i press submit button then i need all the table values inserted into database.
I am posting the code to the same page(test.php).Everything is in the same page...

ok, where code of test.php? or you need this code?

hi all,
i had a php page which contains a list of images in a table.
When i click on submit button i those values should be inserted into database. I need this by using ajax. I had tried something but i am not getting values inserted into database.
so can any one help me.
thank you,
here is the code i had tried.

<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);
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)
    {
       $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++;
    }
}
?>

here is the your code that i have modified hope it will help

<?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;?>


<?php if(!isset($_GET['ajax'])){ //if condition added so that the same page is not returned here sam-i-am

?> 
<html>
<head>

<script type="text/javascript" src="jquery.js" ></script><!-- sam-i-am add the jquery -->
<script type="text/javascript">
function loadXMLDoc(str)
{
	//function rewritten using jquery
	 if (str=="")
	  {
		  $('#myDiv').html('');
	  		return;
	  }
	$.ajax(
			{ 
				url: "test.php?ajax=1&q="+str, 
				success: function(html){
        		$('#myDiv').html(html);
      }});

	
}
</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

    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 } //if condition ends here sam-i-am ?> 
<?php 

    $q=$_GET["q"];
    if($q=='submit')
    {
    //echo $num;
    $i=0;
    while($i<$num)
    {
       $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++;
    }
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.