1

I wonder whether someone may be able to help me please.

I've put together this form which, if you scroll to the bottom of the page, has multiple submission buttons. i.e 'Submit', 'Deleted selected image' and 'View Selected Image'.

I posted a query on this site yesterday here, about about how to go about dealing with multiple 'submission' buttons and received some great advice.

I've tried to implement the advice I was given, but I just can't seem to get this to work. As the guidance suggested, I've added a name to each button and tried to call that through the PHP script, but all that happens is the page refreshes as if submitting the whole page, rather, than for example, being able to view the selected file.

I just wondered whether someone could perhaps take a look at this please and let me know where I'm going wrong.

Please find my PHP code & Form script below

 <?php
    $db_host = 'host'; 
    $db_user = 'username';
    $db_pwd = 'password';

    $database = 'databasename';
    $table = 'images';
    // use the same name as SQL table

    if (!mysql_connect($db_host, $db_user, $db_pwd)) 
die("Can't connect to database"); 

if (!mysql_select_db($database)) 
die("Can't select database"); 

// This function makes usage of 
// $_GET, $_POST, etc... variables 
// completly safe in SQL queries 
function sql_safe($s) 
{ 
if (get_magic_quotes_gpc()) 
$s = stripslashes($s); 

return mysql_real_escape_string($s); 
} 

// If user pressed submit in one of the forms 
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
if (!isset($_POST["action"])) 
{ 
// cleaning title field 
$title = trim(sql_safe($_POST['title'])); 

if ($title == '') // if title is not set 
$title = '(No Title Provided)';// use (empty title) string 

//print_r($_FILES);exit; 

if($_FILES["photo"]["error"] >= 4)  {          
$msg = '<b>Error!</b> - You <b> must </b> select a file before clicking the <b> "Upload This Image" </b> button. Please try again.';         
} 
else

if (isset($_FILES['photo'])) 
{ 
list($width, $height, $imtype, $attr) = getimagesize($_FILES['photo']['tmp_name']);

if ($imtype == 3) // cheking image type 
$ext="png"; // to use it later in HTTP headers 
elseif ($imtype == 2) 
$ext="jpeg"; 
elseif ($imtype == 1) 
$ext="gif"; 
else 
$msg = '<b> Error! </b> - The image that you attempted to upload is not in the correct format. The file format <b> must </b> be one of the following: <b> "gif", "jpeg" </b> or <b> "png" </b>. Please try again.'; 

if($_FILES["photo"]["size"]/1150000 >= 1)  {          
$msg = '<b> Error! </b> - The file that you are attempting to upload is greater than the prescribed <b> 1MB </b> limit. Please try again.';         
} 

if (!isset($msg)) // If there was no error 
{ 
$data = file_get_contents($_FILES['photo']['tmp_name']); 
$data = mysql_real_escape_string($data); 
// Preparing data to be used in MySQL query 

mysql_query("INSERT INTO {$table} 
SET ext='$ext', title='$title', 
data='$data'"); 

$msg = '<b> Success! </b> - Your image has been uploaded'; 
} 
} 
elseif (isset($_GET['title'])) // isset(..title) needed 
$msg = 'Error: file not loaded';// to make sure we've using 
// upload form, not form 
// for deletion 

if (isset($_POST['deleteimage'])) // If used selected some photo to delete 
{ // in 'uploaded images form'; 
$imageid = intval($_POST['del']); 
mysql_query("DELETE FROM {$table} WHERE imageid=$imageid"); 
$msg = 'The image which you selected has now been deleted!'; 
} 

if (isset($_POST['viewimage'])) // If used selected some photo to delete 
{ // in 'uploaded images form'; 
$imageid = intval($_POST['view']); 
mysql_query("SELECT ext, data FROM {$table} WHERE imageid=$imageid"); 

if(mysql_num_rows($result) == 1) 
{ 
$image = $row['myimage']; 
header("Content-type: image/gif"); // or whatever 
print $image; 
exit; 
} 
} 
} 
else 
{ 
$imageid = intval($_POST['del']); 

if ($_POST["action"] == "view") 
{ 
$result = mysql_query("SELECT ext, UNIX_TIMESTAMP(imagetime), data 
FROM {$table} 
WHERE imageid=$imageid LIMIT 1"); 

if (mysql_num_rows($result) == 0) 
die('no image'); 

list($ext, $imagetime, $data) = mysql_fetch_row($result); 

$send_304 = false; 
if (php_sapi_name() == 'apache') { 
// if our web server is apache 
// we get check HTTP 
// If-Modified-Since header 
// and do not send image 
// if there is a cached version 

$ar = apache_request_headers(); 
if (isset($ar['If-Modified-Since']) && // If-Modified-Since should exists 
($ar['If-Modified-Since'] != '') && // not empty 
(strtotime($ar['If-Modified-Since']) >= $imagetime)) // and grater than 
$send_304 = true; // imagetime 
} 

if ($send_304) 
{ 
// Sending 304 response to browser 
// "Browser, your cached version of image is OK 
// we're not sending anything new to you" 
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $ts).' GMT', true, 304); 

exit(); // bye-bye 
} 

// outputing HTTP headers 
header('Content-Length: '.strlen($data)); 
header("Content-type: image/{$ext}"); 

// outputing image 
echo $data; 
exit(); 
} 
else if ($_POST["action"] == "delete") 
{ 
$imageid = intval($_POST['del']); 
mysql_query("DELETE FROM {$table} WHERE imageid=$imageid"); 
$msg = 'The image which you selected has now been deleted!'; 
} 
} 
} 
?> 
    <form action="<?=$PHP_SELF?>" method="post" enctype="multipart/form-data">
      <div align="left">
        <!-- This form is used for image deletion -->
        <?php 
$result = mysql_query("SELECT imageid, imagetime, title FROM {$table} ORDER BY imageid DESC"); 
if (mysql_num_rows($result) == 0) // table is empty 
echo '<ul><li>You have no images loaded</li></ul>'; 
else 
{ 
echo '<ul>'; 
while(list($imageid, $imagetime, $title) = mysql_fetch_row($result)) 
{ 
// outputing list 
echo "<li><input type='radio' name='del' title, value='{$imageid}' />"; 
echo "&nbsp;<small>{$title}</small>&nbsp;&nbsp"; 
echo "<small>{$imagetime}</small></li>"; 
} 

echo '</ul>'; 

echo '<input type="submit" value="Delete Selected Image" onclick="document.getElementById(\'action\').value=\'delete\'" />'; 

echo '<input type="submit" value="View Selected Image" onclick="document.getElementById(\'action\').value=\'view\'" />'; 
} 
?>
        <input type="hidden" name="action" id="action" />
        </div>
    </form>

Many thanks and kind regards

Community
  • 1
  • 1
IRHM
  • 1,326
  • 11
  • 77
  • 130
  • @matino, many thanks for taking a look at my post. I've now added the PHP and form script to my orignal post. Kind regards – IRHM Dec 22 '11 at 15:17

1 Answers1

2

Where you're checking the $_POST action, you need to do this:

if ($_POST["viewimage"] == "View Selected Image") { // Do stuff }
if ($_POST["deleteimage"] == "Delete Selected Image") { // Do stuff }

Basically, you need to check $_POST['name'] == 'value'

Indranil
  • 2,451
  • 1
  • 23
  • 31
  • Hi, many thanks for taking the time to reply to my post. I've amended my code to include your suggestion, but unfortunately I receive the following error: Parse error: syntax error, unexpected T_IS_EQUAL, expecting ',' or ')' in /homepages/2/d333603417/htdocs/development/test.php on line 337, which points to this line in my code: if (isset($_POST["deleteimage"] == "Delete Selected Image")) // If used selected some photo to delete. Could you perhaps tell me please what I've done wrong? Kind regards – IRHM Dec 22 '11 at 15:42
  • you needn't be using `isset` in there along with the `$_POST` value check. Just this will work : `if ($_POST["deleteimage"] == "Delete Selected Image")` Try that? – Indranil Dec 22 '11 at 15:55
  • Hi, many thanks for this. I've made the changes, but I still have the original problem in that the whole page refreshes rather than the individual action taking place, i.e 'viewing the image'. Kind regards – IRHM Dec 22 '11 at 16:03
  • When you submit a form, the page *will* refresh. If you want to get the values without refreshing the page, you need to use javascript and ajax to call the php script and output the result. – Indranil Dec 22 '11 at 16:07
  • being relatively new to PHP, could you tell me please is this something that is easy to achieved? Regards – IRHM Dec 22 '11 at 16:08
  • Well, that will require knowledge of javascript. I suggest you check out [jQuery](http://jquery.com) and their `$.post()` function. Split your php into 2 files, one which processes the form and one to show the form. Now, when you submit the form, use jquery to submit the form and get the results and show them. There are many easy to follow tutorials for this. – Indranil Dec 22 '11 at 16:13
  • many thanks for all your help, I'll start to look at this. Kind regards. – IRHM Dec 22 '11 at 16:14