不应该使用警告框清除输入的字段在php中显示

时间:2017-12-20 05:38:10

标签: javascript php jquery

当我点击提交按钮时,如果输入的数量大于5000警告框或回显消息“输入平移卡号”,我试图显示一条消息。在我的代码中显示消息,但是已经输入数据的所有字段都被清除。我不希望输入的数据被清除。 这是代码。

    <?php

    include_once "db.php";

        if(isset($_POST['submit']))    {


                $amount  = mysql_real_escape_string($_POST["amount"]);
                $firstname=mysql_real_escape_string($_POST["firstname"]);

                $phone=mysql_real_escape_string($_POST["phone"]);
                $address=mysql_real_escape_string(stripslashes($_POST["address"]));

                $pan=mysql_real_escape_string(stripslashes($_POST["pan"]));
                $query= mysql_query("INSERT INTO payment (amount,selector1,firstname,lastname,email,phone,address,country,state,pan)VALUES('$amount','$selector1','$firstname','$lastname','$email','$phone','$address','$country','$state','$pan') ") or die(mysql_error()); 
                if($amount>="5000")
                {

    echo "Enter Pan Card No";       
                }           
                else {
                $id=mysql_insert_id();



    $_SESSION['sess_user'] = $id;
    $_SESSION['lastname'] = $lastname;
    $_SESSION['address']=$address;
    header("Location:successreg.php");          
            }
            }
    ?>  
<form action="" method="post" name="payment">
    <div class="w3l-user">

                    <input type="text" name="amount"  placeholder="&#x20B9; My Contribution" required="">
                </div>              



                        <input type="text"  name="firstname" placeholder="Firstname"   required="">

                        <input type="text"  name="pan" placeholder="Pan Card No" >
                </div>

                    <div class="btn">
                        <input type="submit" name="submit"   value="REGISTER"/>
                    </div>

                </div>
                <div class="clear"></div>

            </form>
        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

为了显示值,在提交之后,您应该从$_POST获取值并在输入值中使用它们:

$firstname = '';
if(isset($_POST['submit']))    {
// write below line after `if`
    $firstname = $_POST['firstname'];

现在,将您的HTML更改为:

<input type="text" name="firstname" placeholder="Firstname" required="" value="<?php echo $firstname; ?>">

对于单选按钮:

$firstRadio = $secondRadio = '';
if(isset($_POST['submit']))    {
// if first radio is selected which you will know from $_POST['firstRadio']
    $firstRadio = 'selected';

在HTML中:

<input type='radio' <?php echo $firstRadio;?> value=1 name='firstRadio' />