PHP用于显示基于正则表达式的弹出消息

时间:2014-05-26 08:16:50

标签: javascript php regex

目前我创建了预订表单。问题是,如果用户填写表单不正确,它会将整个页面更改为错误消息和重新输入表单的链接。然后表单重置为空。

这是部分代码

    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' ;
    ?>

1 个答案:

答案 0 :(得分:1)

如果您不想丢失表单数据,在提交表单后,有很多方法可以执行此操作。其中一个人正在使用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
        }
    }
?>

工作(测试)


<强> UPDATE2

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

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

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

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

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

然后将session作为其值放入其中,如下面的

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

并且在你的test.php页面中也将session_start();放在首发位置并按照这样做

$_SESSION = $_POST;

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