gpt4 book ai didi

javascript - PHP基于正则表达式显示弹出消息

转载 作者:行者123 更新时间:2023-12-03 12:32:35 28 4
gpt4 key购买 nike

目前我创建了用于预订的表单。问题是,如果用户错误地填写了表单,整个页面就会变成错误消息和重新输入表单的链接。然后表格重置回空。

这是部分代码

    if (!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $_POST['dor']))
{
echo 'Input your Date of Reservation correctly!';
?>
Click <a href="reservation.php">here</a> to try again!
<?php
exit();
}

else if (!preg_match('/^[a-z A-Z]{3,30}$/', $_POST['fullname']))
{
echo 'Input your full name correctly!!';
?>
</br>
Click <a href="reservation.php">here</a> to try again!
<?php
exit();
}

else if (!preg_match('/^[0-9]\d{9}$/', $_POST['contactno']))
{

echo 'Enter your contact number correctly!!';
?>
</br>
Click <a href="reservation.php">here</a> to try again!
<?php
exit();
}

else if (!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $_POST['dco']))
{
echo 'Input your Check-out date correctly!';
?>
Click <a href="reservation.php">here</a> to try again!
<?php
exit();
}


else if (strtotime($_POST['dor']) < time())
{
echo 'Date of reservation cannot be in the past!';
?>
</br>
Click <a href="reservation.php">here</a> to try again!
<?php
exit();
}

else if (strtotime($_POST['dco']) < strtotime($_POST['dor']))
{
echo 'Check-out date cannot be before Date of Reservation!';
?>
</br>
Click <a href="reservation.php">here</a> to try again!
<?php
exit();
}

我刚刚尝试在 echo 部分使用此代码

    else if (!preg_match('/^[0-9]\d{9}$/', $_POST['contactno'])) 
{
echo "<script type='text/javascript'>alert('Enter your contact number correctly')</script>";

exit();
}

弹出窗口显示,但页面变成空白。现在我想更改为显示弹出消息而不将表单重置为空

正确的代码是什么?

已更新

我正在使用 iframe,弹出窗口可以工作,但是一旦我正确输入数据,名为 view_reservation() 的函数就不起作用。所以数据已成功录入数据库,但无法查看预订摘要

这是完整的代码

    <?php 
include 'core/init.php';

protect_page();
include 'includes/overall/header.php' ;



//if form is being submitted
if(empty($_POST)=== false)
{
//to validate whether user enters smtg or not otherwise no point continue to do the next validation
//create an array
$required_fields = array ('user_id','fullname','contactno','passport','dor','dco');
foreach($_POST as $key=>$value)
{

//if the key (value) in array of $required_fields is true which is empty
if(empty($value) && in_array ($key, $required_fields) === true )
{
$errors[] = 'You must filled up all of the fields';
//the validation can happen to more than 1 field
break 1;
}
}

if(empty($errors) === true)

{
if (!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $_POST['dor']))
{
?>
<script>alert('Input date of reservation correctly');</script>
<?php
exit();
}

else if (!preg_match('/^[a-z A-Z]{3,30}$/', $_POST['fullname']))
{
?>
<script>alert('Input your full name correctly');</script>
<?php
exit();
}

else if (!preg_match('/^[0-9]\d{9}$/', $_POST['contactno']))
{

?>
<script>alert('Enter Contactno correctly');</script>
<?php
exit();
}

else if (!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $_POST['dco']))
{
?>
<script>alert('Input your check-out date correctly');</script>
<?php
exit();
}


else if (strtotime($_POST['dor']) < time())
{
?>
<script>alert('Reservation date cannot in the past!!');</script>
<?php
exit();
}

else if (strtotime($_POST['dco']) < strtotime($_POST['dor']))
{
?>
<script>alert('Check-out date cannot before the reservation date!!');</script>
<?php
exit();
}

}
}
//what does this line does is that to check whether success is in the end of the URL
if(isset($_GET['success']) && empty($_GET['success']))
{

view_reservation();

}
else
{//if there are no errors
if (empty($_POST) === false && empty($errors) === true)
{
check_availability();
}

//
else if (empty($errors) === false)
{
echo output_errors($errors);
}

?>
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
<script type="text/javascript" src="js/jsDatePick.min.1.3.js"></script>

<script type="text/javascript">
window.onload = function(){
new JsDatePick({
useMode:2,
target:"dor",
dateFormat:"%Y-%m-%d"

});

new JsDatePick({
useMode:2,
target:"dco",
dateFormat:"%Y-%m-%d"

});
};


</script>



<h1 style="font-family: 'Gill Sans', 'Gill Sans MT', 'Myriad Pro', 'DejaVu Sans Condensed', Helvetica, Arial, sans-serif">RESERVATION </h1>

<span style="font-family: 'Gill Sans', 'Gill Sans MT', 'Myriad Pro', 'DejaVu Sans Condensed', Helvetica, Arial, sans-serif">

<form action="" method="post" target="ifr">
<fieldset>
</span>
<legend>
<span style="font-family: 'Gill Sans', 'Gill Sans MT', 'Myriad Pro', 'DejaVu Sans Condensed', Helvetica, Arial, sans-serif"><font size="6">Please input your information correctly</font> </span></legend>

<p>
<iframe name="ifr" id="ifr" style="display:none"></iframe>
<form action="" method="post" >
<ul>
<li>
Full name*: <br>
<input type="text" name="fullname">
</li>
<li>
Contact No.: <br>
<input type="text" name="contactno">
</li>
<li>
IC/Passport*: <br>
<input type="text" name="passport">
</li>
<li>
Room Type*: <br>
<select name="roomtype" id="roomtype">
<option value="">Select</option>
<option value="Single">Single (RM 100)</option>
<option value="Superior">Superior (RM 200)</option>
<option value="Deluxe">Deluxe (RM 300)</option>
</select>
</li>
<br>
<li>
Date of reservation*: <br>
<input type="text" size="12" id= "dor" name="dor"/>
</li>
<li>
Check-out Date*: <br>
<input type="text" size="12" id= "dco" name="dco"/>
</li>

<input type="submit" value="Submit">
<input type="reset" value="Clear" >
<li>
<br>
<br>
<br>
<br>
<br>
<br>
</ul>
</form>

<?php
}
include 'includes/overall/footer.php' ;
?>

最佳答案

如果您不想在提交表单后丢失表单数据,那么有很多方法可以做到这一点。其中之一是使用 iframe

只需将其放入您的 html 代码

<iframe name="ifr" id="ifr" style="display:none">

并在您的表单标记中放置如下所示的目标

<form ...your code... target="ifr">

现在提交表单后您不会丢失表单数据

更新

OP请求的示例代码

<iframe name="ifr" id="ifr" style="display:none"></iframe>
<form name="form1" method="POST" target="ifr">
<input type="text" name="email">
<input type="submit" name="submit" value="submit">

<?php
if(isset($_POST['submit']))
{
if(empty($_POST['email']))
{
?>
<script>alert('Enter Email');</script>
<?php
}
}
?>

工作(已测试)

<小时/>

更新2

你的代码是不同的,很抱歉这个 iframe 可能不会帮助你,所以使用其他简单的替代方案并回滚你所做的事情

session_start(); 放在表单的起始行

然后在你的表单元素中这样做

例如,如果您有这样的输入元素

<input type="text" name="contact">

然后将 session 作为其值放入其中,如下所示

<input type="text" name="contact" value="<?=$_SESSION['contact']?>">

并在你的 test.php 页面中也将 session_start(); 放在开头,然后这样做

$_SESSION = $_POST;

现在够了,提交表单后您不会丢失表单数据

关于javascript - PHP基于正则表达式显示弹出消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23865717/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com