How to create submit button on each row ? If i press the button on that row, the information on that row will be confirmed.


What is the problem?

;<input name="<?php echo $row['id']; ?>" type="submit" id="<?php echo $row['id']; ?>" value="Confrim">&

The whole code

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="test_mysql1"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>


<form id="form" name="form" method="post" action="confirm.php"><table border='1' cellspacing='0' width='612'>
<tr>
<th bgcolor='green'><font color='white'>#</font></th>
<th bgcolor='green'><font color='white'>Id</font></th>
<th bgcolor='green'><font color='white'>Name</font></th>
<th bgcolor='green'><font color='white'>Lastname</font></th>
<th bgcolor='green'><font color='white'>Email</font></th>
</tr>
               
<?php
$i = 0; 

$number = 0;
while($row = mysql_fetch_array($result)){

$number++;

?>
   
<?php
$i++;

if($i%2)
{
$bg_color = "#EEEEEE";
}
else {
$bg_color = "#E0E0E0";
}
?> 
   
   <tr bgcolor='". $bg_color ."'>
   <td><center><Strong><font color='red'><?php echo $row['id']; ?></font></Strong></center></td>
  <td><center><Strong><?php echo $row['name']; ?></font></Strong></center></td>
  <td><center><Strong><?php echo $row['lastname']; ?></Strong></center></td>
  <td><center><Strong><?php echo $row['email'];  ?></Strong> &nbsp;&nbsp;&nbsp;<input name="<?php echo $row['id']; ?>" type="submit" id="<?php echo $row['id']; ?>" value="Confrim">&nbsp;</center></td>
  </tr>
<?php } ?>
            </table>
            <div class="select"><strong>Other Pages: </strong>
               <select>
                  <option>1</option>
               </select>
           </div>
             
               <label>



                </label></form>

confirm.php

if(isset($_POST['accept'])) {       
               
      mysql_query("UPDATE $tbl_name SET Status = 'Confirmed' WHERE id = $id");
                                 
    }

Use loops for shwoing button on each row

Can you suggest the code or the source? I don't know how to write it? Thanks!

can you specify what u want to do?...there are many solutions of it. You can have onlu button instead of a submit button. Or if you want to submit the data of the whole table, this can be done easily with javascript.

Waiting for your reply...

I just want a button on each row, so that i can update relevant informaiton on that row into sql. Can be any methods !

Here is the code for button on each row

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql1"; // Table name
 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
 
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
 
 
<form id="form" name="form" method="post" action="confirm.php">
<table border='1' cellspacing='0' width='612' id='yourTbl'>
  <tr>
    <th bgcolor='green'><font color='white'>#</font></th>
    <th bgcolor='green'><font color='white'>Id</font></th>
    <th bgcolor='green'><font color='white'>Name</font></th>
    <th bgcolor='green'><font color='white'>Lastname</font></th>
    <th bgcolor='green'><font color='white'>Email</font></th>
  </tr>
  <?php
      $i = 0;
      $number = 0;
      while($row = mysql_fetch_array($result))
      {
 
        $number++; 
        $i++;
        if($i%2)
        {
            $bg_color = "#EEEEEE";
        }
        else 
        {
             $bg_color = "#E0E0E0";
        }   
   ?>
        <tr bgcolor=<?php echo $bg_color; ?> >  
            <td><center><Strong><font color='red'><?php echo $row['id']; ?></font></Strong></center></td>
            <td><center><Strong><?php echo $row['name']; ?></Strong></center></td>
            <td><center><Strong><?php echo $row['lastname']; ?></Strong></center></td>
            <td><center><Strong><?php echo $row['email']; ?></Strong> &nbsp;&nbsp;&nbsp;
            <td><input name="btn" type="button" id="<?php echo $row['id']; ?>" value="Confrim" onlclick="yourEvent(this);">&nbsp;</center></td>
        </tr>
<?php 
      } 
 ?>
</table>
</form>

<script type="text/javascript">
function yourEvent(btnClick)
{
   var table = document.getElementById('yourTbl');
   var rowCount = table.rows.length; 
   var data;
   for(var i=0; i<rowCount; i++) 
   {
		 var row = table.rows[i];
		 var chkbox = row.cells[4].childNodes[0];
		 if(chkbox == btnClick)
                 {
		       data = table.rows[i].cells[1].innerHTML;	
                       break;
                 }
   } 
   alert(data);
}
</script>

Suppose you clikced for 3rd row. This will pop up the data of 2nd colum of 3rd row.
Give me a reply whether it helped you or not.

also check this

<?php
  header("Cache-Control: no-cache, no-store,must-revalidate");
  $server="localhost";
  $user="root";
  $pass="root";
  $database="jaydb";
?>
<script type="text/javascript">
    function doit() {
        s="";
        v="";
 
        for(i=0;i<f2.length;i++) {
          if(f2[i].delcheck!=null) {
             if(f2[i].delcheck.checked==true) {
                 v=f2[i].delcheck.value;
                if(s.length==0)
                   s=s + v;
                else
                   s= s + "," + v;
              }
            }
        }
      open("delit.php?id=" + s,"_self");
    }
</script>
<?  
   $cmd=$_POST["cmd"];
   $eno=$_POST["eno"];
   $ename=$_POST["ename"];
   $edate=$_POST["edate"];
    $Longitude=$_POST["Longitude"];
	$Lat=$_POST["Lat"];
	$Address=$_POST["Address"];
	$Region=$_POST["Region"];
	$City=$_POST["City"];
	$MaintainedBy=$_POST["MaintainedBy"];
	$StatusNopatPat=$_POST["StatusNopatPat"];
	$Priority=$_POST["Priority"];
 
   # Connect to database
   $cn=mysql_connect($server,$user,$pass);
   if(!$cn) {
       die ("<br/>DB not connnected : " . mysql_error()); 
    }
   # Select a database	
   $db=mysql_select_db($database,$cn);
	if(!$db)
	   die(mysql_error() . "  " . mysql_errno());
 
 if(isset($cmd,$eno))
     {
# --- When edit button press --------
         if($cmd=="Edit") {
 
	  $res = mysql_query("SELECT * FROM Info
WHERE SiteId='$eno' ");
 
	  $r=mysql_fetch_row($res);
print<<<TTT
<form method="post" name='f1' action="editdelete.php">
<br/>SiteID <input type="text" name="eno"  value="$r[0]"  />
<br/>Alias <input type="text" name="ename" value="$r[1]"/>
<br/>Code <input type="text" name="edate" value="$r[2]"/>
<br/>Long <input type="text" name="Longitude" value="$r[3]"/>
<br/>Lat <input type="text" name="Lat" value="$r[4]"/>
<br/>Address <input type="text" name="Address" value="$r[5]"/>
<br/>Region <input type="text" name="Region" value="$r[6]"/>
<br/>City <input type="text" name="City" value="$r[7]"/>
<br/>Maintained By<input type="text" name="MaintainedBy" value="$r[9]"/>
<br/>PAT Status<input type="text" name="StatusNopatPat" value="$r[10]"/>
<br/>Priority<input type="text" name="Priority" value="$r[15]"/>
<br/><input type="submit" name="cmd" value="Update"/>
<input type="submit" name="cmd" value="Cancel"/>
<hr/>
</form>     
TTT;
}
 # -- Delete a record --
   if($cmd=="Delete") 
   {  
     foreach($_POST["delcheck"] as $delid) {
       $sql ="delete FROM Info WHERE SiteId='$delid'";
       mysql_query($sql);
      }
    }
 
 # -- Update a record --
  if($cmd=="Update") {
 /* $sql = mysql_query("UPDATE Info SET 
 
Alias='$ename',Code='$edate',Longitude='$Longitude',Lat='$Lat' where  
               SiteID='$eno' ");*/
$query=mysql_query("update Info set 
 
alias='$ename',code='$edate',Longitude='$Longitude',Lat='$Lat',Address='$Addr
 
ess',Region='$Region',City='$City',MaintainedBy='$MaintainedBy',StatusNopatPa
 
t='$StatusNopatPat',Priority='$Priority' where siteid='$eno'");
 
           echo "Record updated";
    }
}    
 
   # -- Show the list  - Grid
	$sql="select * from Info";
	$res=mysql_query($sql,$cn);
	if(!$res)
	   die ("<br/>" . mysql_error());
 
	 print "<table cellpadding=1 cellspacing=1 width=100% 
 
style=font-size:12px>";
print "<tr><th bgcolor=#5D9BCC>SiteID</th bgcolor=#5D9BCC><th 
 
bgcolor=#5D9BCC>Alias</th><th bgcolor=#5D9BCC>Code</th><th 
 
bgcolor=#5D9BCC>Long</th><th bgcolor=#5D9BCC>Lat</th><th 
 
bgcolor=#5D9BCC>Address</th><th bgcolor=#5D9BCC>Region</th><th 
 
bgcolor=#5D9BCC>City</th><th bgcolor=#5D9BCC>Maintained By</th><th 
 
bgcolor=#5D9BCC>PAT Status</th><th bgcolor=#5D9BCC>Priority</th><th 
 
bgcolor=#5D9BCC>Action</th></tr>";
 
	 while($r=mysql_fetch_array($res))
	   {
	     print "<tr bgcolor=#FEE9A9><form method='post' 
 
action='editdelete.php' name='f2' >";
	     print 
 
"<td>$r[0]</td><td>$r[1]</td><td>$r[2]</td><td>$r[3]</td><td>$r[4]</td><td>$r
 
[5]</td><td>$r[6]</td><td>$r[7]</td><td>$r[9]</td><td>$r[10]</td><td>$r[15]</
 
td><td>";
print "<input type='checkbox'
               name='delcheck'
               value='$r[0]'/>";
		 print "<input type='hidden' value='$r[0]' name='eno'/>";
		 print "<input type='submit' name='cmd' value='Edit'/>";
        print "<input type='hidden' name='cmd' value=''/>";
      print "</td></form></tr>";				 
	   }
	 print "</table>";  
      mysql_close($cn);
?>	
 
 
<button onclick="doit()">Delete</button>

Thanks all, but the code are too difficult to me and i don't know any javascript!

<td width ='7%'><center><font size='2' face='arial'>&nbsp;<?php echo $row['Status']; ?></font>&nbsp;&nbsp;<input name="accept" type="submit" id="<?php echo $row['id']; ?>" value="Accept"></center></td>

I still want to keep using the submit button! after pressing the submit button (on every row), then go to confirm.php. Then the submit buton can be identified. e.g. id='12' and then update the database!

if(isset($_POST['accept'])) {       
               
      mysql_query("UPDATE table SET Status = 'Confirmed' WHERE id = $id");
                                 
    }

I just don't know how to write the submit button with the unique id or name, so that i can update the database!

You can't set anything to a submit button. You have to put the id in an actual input field. If you don't want to change it, put it in a hidden field.

<input type="hidden" name="id" value="<?php echo $id; ?>" />

That mean no solution?

Sorry i don't know how to use!

Anyone can help? Thanks!

One way to do this, using strictly PHP is to loop through the POST vars.
I am assuming you don't want to use a simple href (href + query string and you can provide any info you want to your script)

Wrap your table in a form.
Say, for instance, the process you want to do is a deletion.
As you are putting your rows together, enter your input something like so:
echo('<input name="Delete' . $Row[ID] . '" type="submit" value="Delete" /> etc ...
If you are submitting to the same page (such as action="<?=$_SERVER[PHP_SELF]?>", at the bottom of your form, create a hidden field named "Process".
Then, when you submit your form, you can check for if(!empty($_POST) and then do this:

foreach($_POST as $key => $value)
{
   if(substr($key,0,6) == "Delete")
   {
      $ID = substr($key,6);
      break;
   }

}
Thus, you have you ID for processing, without having to place it in the value of the button.

i have a similar problem and i want to do it entirely with php. I can't figure how to combine what happens after i press a button, say "edit row", with the rest of the table holding the info from the db...
In other words, I have a form - user writes some data and then it is submitted to the db. Then all the data is printed out in a table each row having a button "edit row". When this button is pressed, the table cells become editable, user changes the name or something and saves the new data (button "edit row" becomes "save").

Hi guys i had a problem with my code saying unexpected end of line
what could be the problem with my code?

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.