![](/img/trans.png)
[英]How can i execute a function after a form submission causes a page refresh?
[英]I have to refresh my php page after form submission to execute the code and display output
表格提交碼
<form action="search.php" method="POST" id="form2">
<input type="text" name="Sr_no" required="true" placeholder="Enter Serial Number"><br>
<input type="submit" value="Check Status" name="ss">
</form>
我的search.php
<?php
$connect=mysqli_connect("localhost","root", "" ,"mydatabase");
$Sr_no = $_POST["Sr_no"];
$status_check = "SELECT verified FROM r_concession_forms WHERE id = '$Sr_id' LIMIT 1";
$s_check = $connect->query($status_check);
$s_value = $row = $s_check->fetch_assoc();
if($s_value["verified"] == "0")
{
echo "<script>
alert('Not Issued/Verified');
window.location.href='index.php';
</script>" ;
}
elseif($s_value["verified"] == "1"){
echo "<script>
alert('Issued and Verified');
window.location.href='index.php';
</script>";
}
else{
echo "<script>
alert('No Such Form Exists');
window.location.href='index.php';
</script>";
}
?>
輸出:我的輸入首先檢查數據庫中是否存在“ id”
沒有預期的警告框,但URL顯示search.php,但未顯示警告框
刷新頁面后,警報框開始工作
頁面的設計是通過JavaScript
我認為您在查詢中使用的是未定義的變量$ Sr_id。 還要首先檢查表單是否已過帳。
刷新頁面時,該代碼可以正常工作,因為刷新頁面時沒有發布數據,因此查詢返回空值,並且顯示“不存在這種形式”。 但是,當您發布頁面時,我猜您在查詢中給出的發布值是錯誤的。
<?php
if(!empty($_POST['ss'])) {
$connect=mysqli_connect("localhost","root", "" ,"mydatabase");
if(!empty($_POST['Sr_no'])) {
$Sr_no = $_POST["Sr_no"];
$status_check = 'select verified FROM r_concession_forms WHERE id = '.$Sr_no.' LIMIT 1';
$s_check = $connect->query($status_check);
$s_value = $row = $s_check->fetch_assoc();
if($s_value["verified"] == "0")
{
echo "<script>
alert('Not Issued/Verified');
window.location.href='index.php';
</script>" ;
}
elseif($s_value["verified"] == "1"){
echo "<script>
alert('Issued and Verified');
window.location.href='index.php';
</script>";
}
else{
echo "<script>
alert('No Such Form Exists');
window.location.href='index.php';
</script>";
}
}
}
?>
同樣,我個人不喜歡您提到的在PHP之間使用腳本代碼。 嘗試將代碼分開以更好地理解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.