Jump to content

Generating dynamic table with submit button how to get which button is pressed


arya2k10

Recommended Posts

Friends

  I am new to php and i have to submit my coursework in php by 3rd dec, I stuck at one place where i have to upload multiple photo and one can see all the photo he has uploaded and can edit or delete that photo so i have done uploading now i am showing those pics in table by running loop and generating tr and td but now i have two buttons with each row edit and delete now when i clicked on one delete or edit that pic should be delete or give text box to edit description of pic, Please help me how to do that.......

while($i<$num)

{

echo "<tr>";

$myimage=  mysql_result($result,$i,"imagename");

$photoID=  mysql_result($result,$i,"PhotoID");

$des=  mysql_result($result,$i,"des");

if ( preg_match('/(\.gif|\.GIF|\.PNG|\.png|\.JPG|\.jpg|\.JPEG|\.jpeg)$/',$myimage))

{

$filename = 'Photos/' . mysql_result($result,$i,"imagename");

$imagesize = getImageSize($filename);

echo "<td><img id=\"myimg\" src=\"$filename\" alt=\"$myimage\"/><br/>$des<br/>";

echo "<input type=\"submit\" name='SUBMIT' value=\"Edit\"><input type=\"submit\" name='SUBMIT' value=\"Delete\"></td>";

}

}

One way I can think of is to make the submit buttons an array. Then in the processing script, if you use:

$id = array_flip($_POST['submit'])

 

the value you need will reside in:

$id['submit'].

 

One problem I can see happening is that the behavior may be unpredictable if the form is submitted with the enter key rather than a submit button.

 

echo "<input type=\"submit\" name=\"submit[$photoID]\" value=\"Submit\"><br>";

Paste this into a file so you can see how it works, and it should be pretty obvious how to implement it.

 

<?php
// processing
foreach( $_POST['submit'] as $k => $v ) {
   $action = $v;
   $id = $k;
}
echo "The file with id# $id will undergo this action: $action";

// Added a range of id's just to test with . . .
$id = range(100, 110);
echo "<form method=\"post\" action=\"\">";
foreach( $is as $photoID ) {
echo
"$photoID <input type=\"submit\" name=\"submit[$photoID]\" value=\"Edit\">
<input type=\"submit\" name=\"submit[$photoID]\" value=\"Delete\"><br>";
}
echo "</form>";
?>

 

Added

 . . . 

tags.

Dear Friend

 

  Thanks for reply, Its working but not fully, I have checked its providing proper ID but not action action is remain same whether you click on any button.

 

if ($_SERVER['REQUEST_METHOD'] == 'POST')

{

if ($_POST['SUBMIT']=='Search')

{

$searchtxt= $_REQUEST['search'];

header("location:search.php?search=$searchtxt&page=0");

exit;

}

if ($_POST['SUBMIT']=='Edit Image')

{

$myimage="getImage.php?ID=$ID";

$html1="<img id='myimg' src=$myimage alt='Welcome'/><br/><input size='2' type='file' name='myimage' id='myimage'/><div id='lblerr'>" . $err . "</div> <input type='SUBMIT' value='Update Image' name='SUBMIT'/>";

}

}

 

i have got multiple submit buttons like search and edit image edit profile etc and i am getting those back on post back as per above code, I want to get this also as per above so that i can perform activity on that like delete that pic is click on delete and provide textarea if edit is clicked t edit description of pic

 

Regards

Parveen Kumar

The logic of that code isn't even close to what I posted, so it isn't any surprise it doesn't work.

 

Pay attention to how I have each button named as an array using $photoID, and that the value= attribute is the action that will be performed.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.