试图在PHP中验证用户输入

时间:2010-02-28 13:44:56

标签: php html

我正在尝试验证php中的用户输入。此代码将检查值是否为null。如果为null,则需要用户输入null值。当html表单中的所有文本框都出现在此之前。此代码将显示提交按钮,该提交按钮将输入的数据保存到mysql数据库中。但问题是保存的值是零和零,这可能是什么原因?

    <html>
<head>
<title>Admission Information Sheet</title>
    <meta http-equiv="Content-Type" content="text/html; Western (ISO-8859-1)">
    <meta name="author" content=" ">
<title>
<style>
input { font-size: 16px;}
</style>
<?php include('header.php'); ?>
<div id="main_content">

</div>
<?php include('footer.php'); ?>

<table border="1" width="900" border="0" align="left" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1.1" method="POST" action="aisaction.php">

<?php
 $NURSE = $_POST[nurse];
     $TELNUM = $_POST[telnum];
     $HOSPNUM = $_POST[hnum];
     $ROOMNUM = $_POST[rnum];
     $LASTNAME = $_POST[lname];
     $FIRSTNAME = $_POST[fname];
     $MIDNAME = $_POST[mname];
     $AD = $_POST[ad];


     $ADATE = $_POST[adate];
     $ADTIME = $_POST[adtime];
     $CSTAT = $_POST[cs];
     $AGE = $_POST[age];
     $BDAY = $_POST[bday];
     $SEX = $_POST[sex];


     ?>
<td>
<table width="100%" border="0" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="12" style="background:#9ACD32; color:white; border:white 1px solid; text-align: center"><strong><font size="3">ADMISSION INFORMATION SHEET</strong></td>
</tr>
<tr>


</td><br>     
<td width="54"><font size="3">Hospital #</td>
<td width="3">:</td>
<td width="168"><input type="display" name="hnum" disabled="true" value= "<?php print "$HOSPNUM";?>"><br>
         <font color="red">     
     <?php   
        if(empty($HOSPNUM))
            print "* Hospital Number required!<br>";
    ?>
</td>

<td width="41"><font size="3">Room #</td>
<td width="3">:</td>
<td width="168"><input type="display" name="rnum" disabled="true" value= "<?php print "$ROOMNUM";?>"><br>
        <font color="red">      
     <?php
            if(empty($ROOMNUM))
            print "* Room Number required!<br>";
     ?>
</td>

<td width="67"><font size="3">Admission Date</td>
<td width="3">:</td>
<td width="168"><input type="display" name="adate" disabled="true" value= "<?php print "$ADATE";?>"><br>
        <font color="red">      
     <?php
            if(empty($ADATE))
            print "* Admission Date required!<br>";
     ?>
</td>

</tr>
<tr>
<td><font size="3">Last Name</td>
<td>:</td>
<td><input type="display" name="lname" disabled="true" value= "<?php print "$LASTNAME";?>"><br>
        <font color="red">      
     <?php
            if(empty($LASTNAME))
            print "* Last Name required!<br>";
     ?>
</td>
<td><font size="3">First Name</td>
<td>:</td>
<td><input type="display" name="fname" disabled="true" value= "<?php print "$FIRSTNAME";?>"><br>
        <font color="red">      
     <?php
            if(empty($FIRSTNAME))
            print "* First Name required!<br>";
     ?>
</td>
<td><font size="3">Middle Name</td>
<td>:</td>
<td><input type="display" name="mname" disabled="true" value= "<?php print "$MIDNAME";?>"><br>
        <font color="red">      
     <?php
            if(empty($MIDNAME))
            print "* Middle Name required!<br>";
     ?>
</td>
<td><font size="3">Admit time</td>
<td>:</td>
<td><input type="display" name="mname" disabled="true" value= "<?php print "$ADTIME";?>"><br>
        <font color="red">      
     <?php
            if(empty($ADTIME))
            print "* Adtime  required!<br>";
     ?>
</td>
</tr>



<tr>
<td><font size="3">Civil Status</td>
<td>:</td>
<td><input type="display" name="cs" disabled="true" value= "<?php print "$CSTAT";?>"><br>
        <font color="red">      
     <?php
            if(empty($CSTAT))
            print "* Civil Status required!<br>";
     ?>
</td>
<td><font size="3">Age</td>
<td>:</td>
<td><input type="display" name="age" disabled="true" value= "<?php print "$AGE";?>"><br>
        <font color="red">      
     <?php
            if(empty($AGE))
            print "* Age required!<br>";
     ?>
</td>
<td><font size="3">Birthday</td>
<td>:</td>
<td><input type="display" name="bday" disabled="true" value= "<?php print "$BDAY";?>"><br>
        <font color="red">      
     <?php
            if(empty($BDAY))
            print "* Birthday required!<br>";
     ?>
</td>

</tr>

<tr>
<td><font size="3">Address</td>
<td>:</td>
<td><input type="display" name="address" disabled="true" value= "<?php print "$AD";?>"><br>
        <font color="red">      
     <?php
            if(empty($AD))
            print "* Address required!<br>";
     ?>
</td>
<td><font size="3">Telephone #</td>
<td>:</td>
<td><input type="display" name="telnum" disabled="true" value= "<?php print "$TELNUM";?>"></td>

<td width="23"><font size="3">Sex</td>
<td width="3">:</td>
<td width="174"><input type="display" name="sex" disabled="true" value= "<?php print "$SEX";?>"><br>
        <font color="red">      
     <?php
            if(empty($SEX))
            print "* Gender required!<br>";
     ?>
</td>
</tr>

<tr>
<td><font size="3">Pls. Check</td>
<td>:</td>
<td><input name="stats1" type="checkbox" id="SSS" value="SSS">SSS</td>
<td><font size="3"></td>
<td>:</td>
<td><input name="stats1" type="checkbox" id="nonmed" value="NonMedicare">Non Medicare</td>

<td><font size="3"></td>
<td>:</td>
<td><input name="stats1" type="checkbox" id="sh" value="stockholder">Stockholder</td>
</tr>


<tr>
<td><font size="3"></td>
<td></td>
<td><input name="stats1" type="checkbox" id="gsis" value="GSIS">GSIS</td>
<td><font size="3"></td>
<td></td>
<td><input name="stats1" type="checkbox" id="senior" value="seniorcitizen">Senior-Citizen</td>


<tr>
<td><font size="3"></td>
<td></td>
<td><input name="stats1" type="checkbox" id="dep" value="dependent">Dependent</td>
<td><font size="3"></td>
<td></td>
<td><input name="stats1" type="checkbox" id="emp" value="employee">Employee</td>

</tr>

<tr>
<td><font size="3">Attending Nurse</td>
<td>:</td>
<td><input type="display" name="nurse" disabled="true" value= "<?php print "$NURSE";?>"><br>
        <font color="red">      
     <?php
            if(empty($NURSE))
            print "* Admitting/Attending Nurse required!<br>";
     ?>
</td>


</tr>


<tr>

<td>&nbsp;</td>
<td>&nbsp;</td>

<td><input type="button" value="Back" onClick="history.go(-1);return true;">
<?php
        $val1 = $_POST['NURSE'];
        if($_POST['NURSE'] !="")
        {
    ?>
    <form action="aisaction.php" method="POST" target="_window">
            <input type="hidden" name="submit" value="yes">
            <input type="submit" value="submit">
    </form>
    <?php
        }
    ?>

</td>
</td>

</tr>
</table>
</td>


</form>
</tr>
</table>
</head>
</html>

3 个答案:

答案 0 :(得分:0)

更好的方法是使用类似“isset()”的方法实际检查$ _POST数组中的值。

e.g。

if(!isset($_POST['whatever']))

此外,如果您的预期值均不为零,则可以随时添加

if(empty($WHATEVER)||$WHATEVER==0)

*** P.S。正如Gordon所提到的,如果您提供用于将信息插入数据库的代码,并了解您的数据库架构,那将极大地帮助我们为您提供更准确的答案

祝你好运!

答案 1 :(得分:0)

最简单,最好的方法是对表单使用jQuery Validation,它会在将表单数据发送到服务器之前自动验证表单数据。我更喜欢使用它然后验证服务器中的东西

答案 2 :(得分:0)

这就是我通常在PHP中处理表单和验证的方式。我不打算重新格式化你的完整代码,所以我只是展示了这个方法。

实例:http://kopli.pri.ee/stackoverflow/2351193.php

完整版代码:*

<?php

function is_valid_email ($email) {
    if(!preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])' . '(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i', $email)) {
        return true;
    } else {
        return false;
    }
}

if ($_POST['trigger']) {
    $p_name = $_POST['name'];
    $p_email = $_POST['email'];

    if (empty($p_name)) {$posting_errors[] = 'Name is missing!';}
    if ($p_name and strlen($p_name) < 3) {$posting_errors[] = 'Name is too short!';}
    if (empty($p_email)) {$posting_errors[] = 'Email is missing!';}
    elseif (is_valid_email($p_email)) {$posting_errors[] = 'Email is incorrect!';}

    if ($posting_errors) {
        $posting_message = '<div class="error_message">' . implode('<br />', $posting_errors) . '</div>';
    } else {
        $posting_message = '<div class="success_message">Great success!</div>';
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>trying to validate user input in php - Kalle H. Väravas</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <style>
        html, body {margin: 0px; padding: 0px; background: #CCFFFF;}
        label {font-weight: bold; width: 140px; display: inline-block; padding: 5px;}
        .success_message,
        .error_message {display: inline-block; padding: 2px 5px; font-weight: bold; margin-bottom: 5px;}
        .success_message {background: #A9F5AB;}
        .error_message {background: #FF8080;}
        #global_container {margin: 20px auto; width: 500px; padding: 20px; background: #FFFFFF;}
    </style>
</head>
<body>
    <div id="global_container">
        <?php echo $posting_message; ?>
        <form method="post">
            <input name="trigger" type="hidden" value="1" />
            <label>Name:</label><input name="name" type="text" value="<?php echo ($_POST['name'] ? $_POST['name'] : '') ?>" /><br />
            <label>Email:</label><input name="email" type="text" value="<?php echo ($_POST['email'] ? $_POST['email'] : '') ?>" /><br />
            <input type="submit" value="Submit" />
        </form>
    </div>
</body>
</html>