Page 1 of 1

POST values of html table row filled via PHP

Posted: Mon Nov 14, 2011 10:59 am
by edprog
Hello,

I am kind of new to PHP, and I am having issues with this set up. Basically I have a html table that is filled with data from mysql and using PHP. Now the table has a column with a button 'update'. And what I want to do is when the Update button is clicked, i want to post the values of that row to the same page again to update it after a if(isset ($_POST['update'])){}. I am just not sure how to do this... I do not know ajax or json which was one of the alternatives.. Is there an easier way to do this?

I also tried adding a form to each row but did not work

Code: Select all

    print("<table id='results'><tr><th>ID</th><th>Image</th><th>Name</th><th>Price</th><th>Price Label</th><th>Description</th><th>Update</th></tr>");
    
         while($row2 = mysql_fetch_array( $result2 )){ 
         	 $client_id = $row2["client_id"];
             $client_name = $row2["client_name"];
             $client_disc = $row2["client_disc"];
             $client_price = $row2["client_price"];
             $client_image = $row2["client_image"];
             $client_price_label = $row2["client_price_label"];
                 
             print("<tr>");   
             print("<td>");
             print("<p style='font-size:14px; color:blue; padding:0;'>$client_id</p>");
             print("</td>");          
             print("<td>");
             print("<img class='custom_rate' alt='' src='$client_image' />");
             print("</td>");
             print("<td width='100px'>");
             print("<input type='text' value='$client_name' name='clientname'/>");
             print("</td>");
             print("<td>");
             print("<input type='text' value='$client_price' name='clientprice'/>");
             print("</td>");
             print("<td width='100px'>");
             print("<input type='text' value='$client_price_label' name='clientpricelabel'/>");
             print("</td>");
             print("<td width='100px'>");
             print("<textarea cols='15' rows='2' name='description'>$client_desc</textarea>");
             print("</td>");
             print("<td width='100px'>");
             print("<input type='submit' value='Update' name='update'/>");
             print("</td>");
             print("</tr>");
        }
        
        print("</table>");

Re: POST values of html table row filled via PHP

Posted: Mon Nov 14, 2011 3:34 pm
by social_experiment
edprog wrote: i want to post the values of that row to the same page again to update it after a if(isset ($_POST['update'])){}.
To call the page on itself you can leave the form's action attribute blank

Code: Select all

<form action="" method="post" >
On your page you would then have the following

Code: Select all

<?php
 // code that creates the table and data inside

 if (isset($_POST['yourSubmitBtn'])) {
  // process data
 }
?>

Re: POST values of html table row filled via PHP

Posted: Mon Nov 14, 2011 3:47 pm
by edprog
Thank you, but this is not my issue, my issue is how to post the values of the 'row' where i pressed the 'update' button.

Re: POST values of html table row filled via PHP

Posted: Mon Nov 14, 2011 3:55 pm
by social_experiment
edprog wrote:I also tried adding a form to each row but did not work
You'll have to do this; it's easier than submitting the whole page and picking out select $_POST values. The thing to remember here is to make each form post a unique value; you can do this by using a hidden field

Code: Select all

<?php
 // print("<p style='font-size:14px; color:blue; padding:0;'>$client_id</p>");
 print('<input type="hidden" name="userID" value="' . $client_id . '" />');
 // since all the forms contain the same fields, the rest is uncomplicated
 // ----
 if (isset($_POST['submitBtn'])) {
    $id = $_POST['userID'];
    // get rest of your data

    $sql = "UPDATE table SET field = $value WHERE id = $id";
?>
This is a simple example without any security features which you should include (security features that is) in your final script

Re: POST values of html table row filled via PHP

Posted: Mon Nov 14, 2011 4:00 pm
by edprog
Great, one last question.. This file is a file that is included on another page, and that other page already has a <form> and this code goes below it so how can i have so many <forms> in a page>

Re: POST values of html table row filled via PHP

Posted: Tue Nov 15, 2011 12:29 am
by social_experiment
As long as you close the form tags properly and don't have a form inside a form there shouldn't be a problem. Each submit button works within it's specified form.