所以我在index.php中有一个表单方法帖子,其中将数据从文本框发送到另一个print.php页面。
现在我要做的是,如果index.php中的文本框为null,它将不会重定向到print.php,或者如果它重定向到print.php,它将被重定向回index.php。
index.php格式
<form action="print.php" method="post" target="_blank">
<input type="text" name="faidf" id="faidf" size="25" value="" maxlength="25"/></td>
<input type ="submit" value="Print">
print.php
<?php
$faidf = $_POST['faidf'];
if(isset($_POST['faidf'])) {
echo "<td><font size=2>FAID:$faidf</td><td></font></td>";
}
else {
echo "FAID is missing";
}
?>
而不是FAID丢失我可以将其重定向到家,因为我有大约10多个PHP,其中需要$ faidf的变量,所以如果文本框是空白的话,整个printd.php完全没用。谢谢
答案 0 :(得分:0)
您可以通过两种方式实现这一目标:
在表单提交时使用JQuery / JavaScript进行表单验证过程。只有在文本框中找到数据时才会重定向。
检查提供的帖子数据的长度,如果长度小于1,则将其返回上一页。
我建议您使用JavaScript / JQuery,因为它将在所有跨浏览器上运行,易于实现和轻松更改
答案 1 :(得分:0)
首先在文本框中添加onchange功能
<input type="text" onchange="myFunction(this)" name="faidf" id="faidf" size="25" value="" maxlength="25"/>
并为按钮添加ID
<input type ="submit" value="Print" id="btn" />
并在页面中添加脚本标记:
function myFunction(e)
{
var x=document.getElementById("btn");
if (e.value == ''){
x.setAttribute('disabled','disabled');
}else{
x.removeAttribute('disabled');
}
}
而不是print.php上的echo添加位置标题
header("Location: index.php");