简体   繁体   中英

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

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. Write valid HTML.

I Think you should change type of submit to button for both delete and save and the use following jquery code:

$("#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>

we can get the category variable in the next page by using

$_GET['category'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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