我有2个表格。一个提交只是一个提交/添加,第二个是选择/重定向。现在两者都重定向。如何防止顶部提交重定向?
<?php
if($_POST){
//store as session variable
$_SESSION['id'] = $_POST['bolredir'];
//forward browser
die(header("Location: add-job.php"));
}
?>
<!DOCTYPE html>
<body>
<div class="">
<form name="addcust" method="post" action="index.php">
<label> Name:</label>
<input type="text" id="cust_name" name="cust_name"/> <br />
<input type ="submit" name="addcust" value="Submit" /> <br /> <br />
</form>
</div>
<?php
try
{
$conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);
$stmt = $conn->prepare("SELECT * FROM customer");
$stmt->execute();
$result = $stmt->fetchAll();
?>
<form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST">
<select name="bolredir">
<option></option>
<?php
foreach ($result as $row)
{
echo "<option value=\"{$row['id']}\">{$row['cust_name']}</option>";
//$_SESSION['id'] = $row['cust_name'];
}
}
?>
</select>
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
答案 0 :(得分:0)
您可以在表单提交上返回false。给第二个表单一个名称,例如“dontsubmit”,然后阻止它提交并导致表单重定向。
document.forms["dontsubmit"].onsubmit = function(){
return false;
}
答案 1 :(得分:0)
如果您不想通过邮寄或获取提交表单,请尝试此操作。并希望以另一种方式提交
<form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST" onsubmit="return false;">
答案 2 :(得分:0)
给第二个表单提交一个不同的名称
<input type="submit" name="SubmitForRedirect" value="Submit" />
现在在你的php中使用这段代码
if(isset($_POST['SubmitForRedirect'])) {
//store as session variable
$_SESSION['id'] = $_POST['bolredir'];
//forward browser
die(header("Location: add-job.php"));
}
您也可以将第二种形式直接重定向到add-job.php
将<?= $_SERVER['PHP_SELF']; ?>
替换为add-job.php
<form action="add-job.php" method="POST">