POST values of html table row filled via PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
edprog
Forum Newbie
Posts: 3
Joined: Mon Nov 14, 2011 10:51 am

POST values of html table row filled via PHP

Post 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>");
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: POST values of html table row filled via PHP

Post 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
 }
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
edprog
Forum Newbie
Posts: 3
Joined: Mon Nov 14, 2011 10:51 am

Re: POST values of html table row filled via PHP

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: POST values of html table row filled via PHP

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
edprog
Forum Newbie
Posts: 3
Joined: Mon Nov 14, 2011 10:51 am

Re: POST values of html table row filled via PHP

Post 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>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: POST values of html table row filled via PHP

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply