形式困难的多个提交按钮

时间:2011-12-22 15:01:58

标签: php javascript

我想知道是否有人可以帮助我。

我已将this表单放在一起,如果您滚动到页面底部,则会有多个提交按钮。即“提交”,“删除所选图像”和“查看所选图像”。

昨天我在这个网站上发布了一个查询here,关于如何处理多个“提交”按钮,并收到一些很好的建议。

我试图实施我给出的建议,但我似乎无法让它发挥作用。正如指导建议的那样,我为每个按钮添加了一个名称并尝试通过PHP脚本调用它,但所有发生的事情都是页面刷新,就像提交整个页面一样,而不是例如能够查看选定的文件。

我只是想知道是否有人可以看看这个,让我知道我哪里出错了。

请找到我的PHP代码&下面的表单脚本

 <?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>

非常感谢和亲切的问候

1 个答案:

答案 0 :(得分:2)

如果您要检查$_POST操作,则需要执行以下操作:

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

基本上,您需要检查$_POST['name'] == 'value'