Board index   FAQ   Search  
Register  Login
Board index php forum :: HTML JavaScript

html table and php

Javascript coding ..

Moderators: macek, egami, gesf

html table and php

Postby gigi82 » Thu Nov 22, 2012 4:12 pm

Hi to all.

I'm quite new in php.

I have an HTML table with two button for each row. When I press one of these buttons in a specific row, I want to edit the row correspondent and take all the values related to that row.

This is the code:

Code: Select all

<html>
<body>
<?php

//CONNECTION TO DB

?>


<form id="view_admin" method="post">


<table id="my_table" class= "sample" >

<tr>
<td align="center"><strong><font face="Arial, Helvetica, sans-serif">Id</font></strong></td>
<td align="center"><strong><font face="Arial, Helvetica, sans-serif">Column1</font></strong></td>
<td align="center"><strong><font face="Arial, Helvetica, sans-serif">Status</font></td>
<td align="center"><strong><font face="Arial, Helvetica, sans-serif">Edit Status</font></td>
</tr>

<?php

$i=0;
while ($i < $num) {

$f0=mysql_result($result,$i,"id");
$f1=mysql_result($result,$i,"column1");
$f2=mysql_result($result,$i,"status");

?>

<tr>
<td align="center"><font face="Arial, Helvetica, sans-serif"><?php echo $f0; ?></font></td>
<td align="center"><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td align="center"><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td align="center"><input type="submit" name="approved" value="approve"> <input type="submit" name="refused" value="refuse"> </td>
</tr>

<?php
if (isset($_POST['approved'])) {
$query_update = "UPDATE main SET status='APPROVED' WHERE id ='$f0'";
$result_update=mysql_query($query_update);}
else if (isset($_POST['refused'])) {
$query_update = "UPDATE main SET status='REFUSED' WHERE id ='$f0'";
$result_update=mysql_query($query_update);
}
$i++;
}

mysql_close();


?>

</table>
</form>
</body>
</html>



If I press a button in a row, now I update all the rows and not the selected row.
I'm thinking to assign a progressive number (variable $i php)to each row and then use a javascript function to detect the selected row. How can I do this?

Thank you.
gigi82
New php-forum User
New php-forum User
 
Posts: 1
Joined: Thu Nov 22, 2012 3:54 pm

Re: html table and php

Postby seandisanti » Tue Nov 27, 2012 11:35 am

You don't want to use javascript for that, keep all of your logic and processing server side. The following code isn't tested because i didn't want to create a table, but it should work as is.
Code: Select all
<?php 
 
// assume connection already made
 
    
if (! isset($_POST)) { //not responding to form, must draw it
        
echo '<form id="view_admin" action="' $_SERVER['SCRIPT_NAME'] . '" method="post">';
        echo 
'<table>';
        echo 
'<tr>';
        echo 
'<th>ID</th>';
        echo 
'<th>Column 1</th>';
        echo 
'<th>Status</th>';
        echo 
'<th>Edit</th>';
        while (
$row=mysql_fetch_assoc($result)){
            
$theRow '<tr>';
            
$theRow .= '<td><input type="text" disabled="disabled" value="' $row['id'] . '" name="id"></td>';
            
$theRow .= '<td><input type="text" disabled="disabled" value="' $row['column1'] . '" name="column1"></td>';
            
$theRow .= '<td><input type="text" disabled="disabled" value="' $row['status'] . '" name="status"></td>';
            
$theRow .= '<td><input type="submit" name="reject" /><br /><input type="submit" name="approve" /></td>';
            
$theRow .= '</tr>';
            echo 
$theRow;
        }
        echo 
'</table></form>'
    
} else { //responding to form
        
if (isset($_POST['approve'])){ //it's an approval
            
$sql "UPDATE main SET status='APPROVED' WHERE id =" $_POST['id'];
        } else {
            
$sql "UPDATE main SET status='REFUSED' WHERE id =" $_POST['id'];
        }
    
mysql_query($sql);
    echo 
mysql_affected_rows() . ' rows updated.';
    }
 
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to JavaScript

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.