PHP - 两个按钮插入记录然后转到不同的页面

时间:2017-01-15 21:58:19

标签: php forms

我想在我的表单上有2个按钮。一个插入记录然后转到第1页,另一个插入记录并转到第2页。我的PHP知识不太好所以我可能犯了一个简单的错误。这是我的代码 - 提前谢谢!

<?php 
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
$insertSQL = sprintf("INSERT INTO content (title) VALUES (%s)",
                   GetSQLValueString($_POST['title'], "text"));

mysql_select_db($database_sql, $sql);
$Result1 = mysql_query($insertSQL, $sql) or die(mysql_error());

$insertGoTo = "page1.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}




$editFormAction2 = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction2 .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
$insertSQL = sprintf("INSERT INTO content (title) VALUES (%s)",
                   GetSQLValueString($_POST['title'], "text"));

mysql_select_db($database_sql, $sql);
$Result1 = mysql_query($insertSQL, $sql) or die(mysql_error());

$insertGoTo = "page2.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>

<html>
<body>
<form name="form" method="POST" id="form">
<input name="title" type="text" placeholder="Page Title"/>
<input type="submit" class="submit" value="Next Step" onClick="this.form.action='<?php echo $editFormAction; ?>';     this.form.submit()">
<input type="submit" class="save" value="Save & Exit" onClick="this.form.action='<?php echo $editFormAction2    ; ?>';     this.form.submit()">
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<html>
<body>
<div style="float: left;">
<form action="page1.php" name="form" method="POST" id="form">
    <input name="title" id="title" type="text" placeholder="Page Title"/>
    <input type="submit" class="submit" value="Next Step">

</form>
</div>
<div style="float: left;">
<form action="page2.php" name="form" method="POST" id="form">
    <input name="title2" id="title2" type="hidden" placeholder="Page Title"/>
    <input type="submit" class="save" value="Save & Exit">
</form>
</div>
</body>
</html>

<script>
    $(document).ready(function() {
        $("#title2").val($("#title").val());
    });
    $(document).change(function() {
        $("#title2").val($("#title").val());
    });
</script>