469,271 Members | 1,600 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Selecting Table Rows for update, edit or delete

27
Hi!

Have been hitting my head on the wall for two weeks now. am a php newbie. have a form that on submitting to a MySQL DB instantly generates a HTML table on the same page. what i need now is way to be able to select rows from this table. specific selected row info sent back to text-box in my multi-button form for further processing depending on which button i click. have been trying to study javascript onclcik event handlers to see if this could be the solution. my scripts for multi-button form and generated table below:

1. Form:
Expand|Select|Wrap|Line Numbers
  1. <table id = "MAINFORM">
  2. <form name = "complaint_settings" method = "POST" action = "php_complaint_mysql.php">
  3.  
  4. <td>
  5. <!--<div align = "left"></div> -->
  6. <table border = "3" bordercolor = "gold" bgcolor = "00008B">
  7. <tr><td>
  8.  
  9. <p style = "color:gold">Complaint: <br>
  10. <input type = "text" size = "50" maxlength = "50" name = "complaint"></td></tr><br>
  11. <tr><td>
  12. <input type = "submit" name = "submit_c" value = "submit"> <br>
  13. <input type = "submit" name = "submit_c" value = "update"> <br>
  14. <input type = "submit" name = "submit_c" value = "delete" onClick = "return confirm('Are you sure you want to delete record?')">
  15.  
  16. </td></tr>
  17.  
  18. 2. Generated Table based on submission from above form:
  19.  
  20. //initializing extraction of complaint parameters from mysql
  21. $complaintTable = mysql_query("SELECT * FROM complaint") or die (mysql_error());
  22.  
  23. //store complaint records into array $complaint_row &&
  24. //create table of mysql complaint array
  25.                         echo "<table>";
  26.                         echo "<td><div>";"</div></td>";
  27.                         echo "<table id = \"table_c\" border = \"4\" bordercolor = 00008B>";
  28.                         echo "<tr><th>Complaint</th>";
  29.                         while ($comp_rec= mysql_fetch_array($complaintTable))
  30.                         {echo "<tr><td>"; echo $comp_rec['complaint']; echo "</td>";}
  31.                         echo "</tr></table>";
[Please use CODE tags when posting source code. Thanks! --pbmods]

THANKS IN ADVANCE.
Jul 5 '07 #1
8 30343
epots9
1,351 Expert 1GB
do u know AJAX? Tutorial

that way using your javascript u can call php files. (different php files for delete, update, etc.)
Jul 5 '07 #2
prosad
27
do u know AJAX? Tutorial

that way using your javascript u can call php files. (different php files for delete, update, etc.)
sorry, dont know AJAX yet.
just starting out with PHP, MySQL, Firebird and it seems am now being forced to learn javascritp just for this purpose on recovering text item from my HTML table.
Jul 5 '07 #3
code green
1,726 Expert 1GB
Give these buttons different names then you can test which one was clicked to submit the form
Expand|Select|Wrap|Line Numbers
  1. <input type = "submit" name = "select" value = "select"> <br>
  2. <input type = "submit" name = "update" value = "update"> <br>
  3. <input type = "submit" name = "delete" value = "delete"
AJAX is for wimps. Then in the script called by "action" [PHP]//php_complaint_mysql.php
if(isset($_POST['select']))
//do lots of select stuff
if(isset($_POST['delete']))
//do lots of delete stuff
if(isset($_POST['update']))
//do lots of update stuff [/PHP] Bit more tidying up to do but this is the idea
Jul 5 '07 #4
prosad
27
Give these buttons different names then you can test which one was clicked to submit the form
Expand|Select|Wrap|Line Numbers
  1. <input type = "submit" name = "select" value = "select"> <br>
  2. <input type = "submit" name = "update" value = "update"> <br>
  3. <input type = "submit" name = "delete" value = "delete"
AJAX is for wimps. Then in the script called by "action" [PHP]//php_complaint_mysql.php
if(isset($_POST['select']))
//do lots of select stuff
if(isset($_POST['delete']))
//do lots of delete stuff
if(isset($_POST['update']))
//do lots of update stuff [/PHP] Bit more tidying up to do but this is the idea
thanks for the tip. my different buttons work fine.
the BIG Question though i have is that how do i retrieve exactly already submitted (posted) data that is being generated and displayed in my html table immediately after posting. the rows on my table are not clickable. i wish to click on any row on my table and data/item on that row will be returned to the form (text field) it was originally submitted from.
html form and php, mysql generated table all shown in my source code. i wish to use form for submit, update, delete functions on my MySQL database through this single form and table. table to be used for just reselcting already posted data. form to be used for processing data (retrieved from my table).
Jul 5 '07 #5
code green
1,726 Expert 1GB
i wish to click on any row on my table and data/item on that row will be returned to the form (text field) it was originally submitted from
PHP can handle this very well. You need to wrap your data display line in a hyperlink, with the link returning the same page and your data being passed as a URL variable.
I assume this is your data line
[PHP]{echo "<tr><td>"; echo $comp_rec['complaint']; echo [/PHP]
Please check my syntax because this is of the top of my head.
Please note that I only use single quotes, because PHP parses them much faster than double.
And I have inserted extra spaces for clarity only. Remove them.
[PHP]{echo '<tr><td><a href=" '.$_SERVER['PHP_SELF'].'?comp='.$comp_rec['complaint'].' " '.comp_rec['complaint']. '</a>';[/PHP]
Jul 6 '07 #6
prosad
27
PHP can handle this very well. You need to wrap your data display line in a hyperlink, with the link returning the same page and your data being passed as a URL variable.
I assume this is your data line
[PHP]{echo "<tr><td>"; echo $comp_rec['complaint']; echo [/PHP]
Please check my syntax because this is of the top of my head.
Please note that I only use single quotes, because PHP parses them much faster than double.
And I have inserted extra spaces for clarity only. Remove them.
[PHP]{echo '<tr><td><a href=" '.$_SERVER['PHP_SELF'].'?comp='.$comp_rec['complaint'].' " '.comp_rec['complaint']. '</a>';[/PHP]
ur script:
[PHP]{echo '<tr><td><a href="'.$_SERVER['PHP_SELF'].'?comp='.$comp_rec['complaint'].' "'.$comp_rec['complaint'].'</a>';}[/PHP]

thanks for the effort. though does not work cos i submit to a script that validates then inserts into a MySQL DB. as soon as database insert is carried out the data row is generated with the new data item just inserted into DB. using your script it does not return any data to my "complaint" table for display on page even though it has succesfully inserted data item in MySQL DB. my Database submit script is given below and as u can see it returns thro header to my submit page so user stays on same page. after submitting thro form - given above (form name: "complaint_settings") - Now the submit page (csu_settings.php) will have a single difference. HTML table displayed below form includes newly submited item fetched from database as new row. THE QUESTION??? HOW DO I SELECT THESE ITEMS ANY OF MY TABLE ROWS SUCH THAT ON CLICKING ANY CELL DATA IT RETURNS INTO MY FORM (INPUT TYPE = TEXT) THAT IS DISPLAYED ABOVE THE TABLE FOR OTHER ACTION SUCH AS UPDATE, DELETE . DIFFERENT BUTTONS ON FORM DETERMINES APPROPRIATE SCRIPT TO SEND DATA ITEM FOR PROCESSING UPDATE, DELETE ETC.

[PHP]//inserting record in complaint table
mysql_select_db("CSU", $con);
$sql="INSERT INTO complaint (complaint) VALUES ('$_POST[complaint]')";
if (!mysql_query($sql,$con)) {die('Error: ' . mysql_error()); }

mysql_close($con)

;};}

header('location:http://localhost/CSU_Project/csu_settings.php'); [/PHP]

[PHP]//initializing extraction of complaint parameters from mysql
$complaintTable = mysql_query("SELECT * FROM complaint") or die (mysql_error());

//store complaint records into array $complaint_row &&
//create table of mysql complaint array that is displayed under submit form on csu_settings.php page.

echo "<table>";
echo "<td><div>";"</div></td>";
echo "<table id = \"table_c\" border = \"4\" bordercolor = 00008B>";
echo "<tr><th>Complaint</th>";
while ($comp_rec= mysql_fetch_array($complaintTable))[/PHP]
Jul 6 '07 #7
code green
1,726 Expert 1GB
I suggesting passing the variables as URL variables
Expand|Select|Wrap|Line Numbers
  1. echo '<tr><td><a href=" '.$_SERVER['PHP_SELF'].'?comp='.$comp_rec['complaint'].' " '.comp_rec['complaint']. '</a>';
The following does not pass anything anywhere
Expand|Select|Wrap|Line Numbers
  1. (header 'location:http://localhost/CSU_Project/csu_settings.php');
using your script it does not return any data to my "complaint" table
You must read the URL variables using GET and not POST
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO complaint (complaint) VALUES ('$_POST[complaint]')";
Jul 11 '07 #8
prosad
27
I suggesting passing the variables as URL variables
Expand|Select|Wrap|Line Numbers
  1. echo '<tr><td><a href=" '.$_SERVER['PHP_SELF'].'?comp='.$comp_rec['complaint'].' " '.comp_rec['complaint']. '</a>';
The following does not pass anything anywhere
Expand|Select|Wrap|Line Numbers
  1. (header 'location:http://localhost/CSU_Project/csu_settings.php');
You must read the URL variables using GET and not POST
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO complaint (complaint) VALUES ('$_POST[complaint]')";
thanks code green,
solved my problem using Javascript onclick, can click on any cell in a dynamic table and it will pass the innerText object value to my form text field. parts of code given below:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function select_cell(obj)
  3. {
  4.     document.getElementById('trial').value = obj.innerText; 
  5.     document.getElementById('complaint').value = obj.innerText;
  6.     document.getElementById('selected_form');
  7. }
[PHP]echo "<tr><th>Complaint</th>";
while ($comp_rec= mysql_fetch_array($complaintTable))
{echo "<tr><td id = 'complaint' onclick='select_cell(this);'>"; echo $comp_rec['complaint'];
echo "</td>";[/PHP]

[HTML]<table id = "MAINFORM">
<form id = "selected_form" name = "complaint_settings" method = "POST" action = "php_complaint_mysql.php">
<td>
<table border = "3" bordercolor = "gold" bgcolor = "00008B">
<tr><td>
<p style = "color:gold">Complaint: <br>
<input type = "text" size = "50" maxlength = "50" name = "complaint">
</td></tr><br>
<tr><td>
<input type = "submit" name = "submit_c" value = "submit"> <br>
<input type = "submit" name = "submit_c" value = "update"> <br>
<input type = "submit" name = "submit_c" value = "delete" onClick = "return confirm('Are you sure you want to delete record?')">[/HTML]

my problem now is how to pass along my primary key which is the unique id of each $comp_rec to my form as a hidden variable so i can pass $_POST['complaint'] to php_complaint_mysql.php for update or delete. As it is now the table i use onclick only shows a single column(complaint) and even if i add another cell (id) the two cells are not carried and placed in separate fields on the same form. Any idea on populating form fields by clicking a row record with each attribute being passed to appropriate field? Attribute ID not presently on table. wish to keep hidden. But need to pass ID so i can use to update mysql. let me go wander around javascript forum.
thanks.
Jul 12 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by webhigh | last post: by
9 posts views Thread by Dejan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.