0

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);
?>
swiftsly
  • 811
  • 4
  • 16
  • 29
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Dec 21 '13 at 16:36

3 Answers3

2

Don't send it to another page. Use one page and branch with an if (or switch or whatever) statement.

e.g.

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

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();
});
undefined_variable
  • 6,180
  • 2
  • 22
  • 37
-2

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'];

Andrew
  • 15,357
  • 6
  • 66
  • 101
Shinto Joseph
  • 1,081
  • 8
  • 8