简体   繁体   English

一种形式的两种不同动作

[英]Two different actions in one form

I have a form with two different submit buttons. 我有一个带有两个不同的提交按钮的表单。 One is to delete the corresponding row in the MySQL, which is done on the same page, the other is to save the row to another database, which is done on a separate page. 一种是删除MySQL中的相应行,这是在同一页面上完成的,另一种是将行保存到另一个数据库中,这是在单独的页面上完成的。 The delete action works fine, but I cannot figure out how to send the save form data to the other page without putting it in the "action" value. 删除操作可以正常工作,但是我无法弄清楚如何将保存表单数据发送到另一页而不将其置于“操作”值中。 I've tried JavaScript, but haven't had much luck. 我已经尝试过JavaScript,但是运气还不太好。 It's a bit of a lengthy post, but I figure the more information I give, the easier it will be for a solution to be found. 这是一篇冗长的文章,但是我认为我提供的信息越多,找到解决方案的难度就越大。

Here's just the save submit button: 这只是保存提交按钮:

   echo "<td><input type=\"submit\" name=\"save\" value=\"Save\" onclick=\"this.form.action='publishmod.php'\"></td>";

Here's the form code (main page): 这是表单代码(主页):

   echo "<form name=\"editmod\" method=\"post\">";
   echo "<tr class='modlist'>";
   echo "<td>".$row['id']."</td>";
   echo "<td><div class=\"edit\" id=\"div_1\">".$row['title']."</div></td>";
   echo "<td><div class=\"edit\" id=\"div_2\"><a href=".$row['mod_url'].">".$row['mod_url']."</a></div></td>";
   echo "<td><div class=\"edit\" id=\"div_3\">".$row['developer']."</div></td>";
   echo "<td><div class=\"edit\" id=\"div_4\">".$row['type']."</div></td>";
   echo "<td><div class=\"edit\" id=\"div_5\">".$v162."$nbsp".$v164."$nbsp".$v172."</div></td>";
   echo "<td><div class=\"edit\" id=\"div_6\">".$row['title'].",$nbsp".$row['developer']."</div></td>";
   echo "<td><input type=\"submit\" name=\"save\" value=\"Save\" onclick=\"this.form.action='publishmod.php'\"></td>";
   echo "<td><input type=\"submit\" name=\"delete\" value=\"Delete\"></td>";
   echo "</tr>";
   echo '<input type="hidden" name="id" value="', htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8'), '" />';
   echo '<input type="hidden" name="id" value="', htmlspecialchars($row['mod_url'], ENT_QUOTES, 'UTF-8'), '" />';
   echo '<input type="hidden" name="id" value="', htmlspecialchars($row['developer'], ENT_QUOTES, 'UTF-8'), '" />';
   echo '<input type="hidden" name="id" value="', htmlspecialchars($row['type'], ENT_QUOTES, 'UTF-8'), '" />';
   echo '<input type="hidden" name="id" value="', htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'), '" />';
   echo "</form>";
   }

Publish code (page that the save action is supposed to redirect to): 发布代码(保存操作应重定向到的页面):

Variable sanitation omitted for Stack Overflow purposes. 出于堆栈溢出的目的,省略了可变卫生措施。

$name = $_GET['title'];
$desc = "Installation tutorial for '.$name.'";
$url = ereg("^[A-Za-z_\-]+$", $name) + ".php";
$keywords = "'.$name.', '.$dev.'";
$type = $_GET['type'];
$link = $_GET['mod_url'];
$dev = $_GET['developer'];
$id = $_GET['id'];

// Query

$savequery = "INSERT INTO search (title, description, url, keywords, type, mod_url, developer, v162, v164, v172)
            VALUES ('$name', '$desc', '$url', '$keywords', '$type', '$link', '$dev', '$v162', '$v164', '$v172')";

// Mod file creation

$file = ereg("^[A-Za-z_\-]+$", $name) + ".php"; 
$handle = fopen($file, 'w');
$data = '<?php
    $title = "'. $name. '";
    $keywords = "'. $name. ','. $developer. '";
    $description = "How to install '. $name. ' for PC and Mac.";

    include("templates/modheader.php");
    include("templates/modfooter.php");
    ?>';

$save = $dbsave->query($savequery) or die(mysqli_error($dbsave));

// Run creation

echo $save;
fwrite($handle, $data); 
print " mod file created!"; 
fclose($handle);
?>

Don't send it to another page. 不要将其发送到另一个页面。 Use one page and branch with an if (or switch or whatever) statement. 使用一页并使用if (或switch或其他)语句进行分支。

eg 例如

if (isset($_POST['save'])) {
    include('save.php');
} elseif (isset($_POST['delete'])) {
    include('delete.php');
} else {
    # logic for when the page is arrived at without the form being submitted with a submit button
}

As an aside. 作为旁白。 A <form> may not be the parent element of a <tr> , and no element that may have a <td> as a child element may also have an <input> as a child element, you will find some browsers will move elements around to make them acceptable in a way that is going to break your form. <form>可能不是<tr>的父元素,并且可能没有<td>作为子元素的元素也可能没有<input>作为子元素,您会发现某些浏览器会移动元素使它们以一种会破坏您的形式的方式被接受。 Write valid HTML. 编写有效的 HTML。

I Think you should change type of submit to button for both delete and save and the use following jquery code: 我认为您应该将提交到按钮的类型更改为删除和保存以及使用以下jquery代码:

$("#savebutton_id").onclick(function(){
$("#formid").attr('action','Your save Page');
$("#formid").submit();
});
$("#deletebutton_id").onclick(function(){
$("#formid").attr('action','Your delete Page');
$("#formid").submit();
});

you can send data to other pages using hyperlink 您可以使用超链接将数据发送到其他页面

eg: <a href="page.php?category=1">Shoes</a> 例如: <a href="page.php?category=1">Shoes</a>

we can get the category variable in the next page by using 我们可以通过使用下一页获取类别变量

$_GET['category'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM