如何提交无错表格

时间:2015-08-20 19:25:00

标签: php

作为一名初学者,我正处于如何提交无错误表格的十字路口。参考下面的php文件,我使用了3个动作设置,结果如下: 方法1 action =“”
这表示同一页面上的错误是否存在错误。 方法2: 行动=“” 这表示同一页面上的错误是否存在错误。 方法3: action =“用户sch contact.php” 这将打开一个新页面(用户sch联系人),其中包含所有用户输入是否有错误。

现在我不希望在用户清除每个错误之前提交表单。请问我该怎么做到这一点?我在php,sql,Mysql中缺少哪些东西可以帮助我实现这个目标?

<!DOCTYPE html>
<html>
<head>
<title>PHP Exercise</title>
<style type="text/css">

body {font-family: Arial, Verdana, Helvetica,Sans-serif; padding: 0px;   
margin-left: 50px;}
input:hover {background-color: rgb(250,250,150);}
.btn {padding: 5px 5px 5px 5px; background-color: rgb(240,240,240); font-  
size: 12px; font-weight: bold; font-style: italic; border: 1px; }
.btn:hover {background-color: rgb(250,250,150);}
th {font-weight: bold; font-size: 14px; border: 1px; border-style: solid;   
margin: 0px; border-spacing: 0px;}
.cdtl {text-align: left;}
td {font-weight: Normal; font-size: 14px; border: 1px; border-style: solid;   
margin: 0px; border-spacing: 0px;}
.err {color: rgb(250,50,10); width: 200px; font-style: italic; border: 0px;   
font-size: 14px;}
</style>

<script type="text/javascript">
var debugScript = true;
</script>

</head>

   <?php
    $username = $useraddy1 = $useraddy2 = $userfone = $useremail = $userurl   
    = "";
    $firstgrade = $secondgrade = $thirdgrade = $fourthgrade = "";
    $usernameerr = $useraddy1err = $useraddy2err = $userfoneerr = 
    $useremailerr = $userurlerr = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["username"])) {
    $usernameerr = "Name is required";
    } else {
    $username = test_input($_POST["username"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z0-9 ]*$/",$username)) {
    $usernameerr = "Only alphanumerics and white space allowed";
    } else {$usernameerr = "";}
        }

    if (empty($_POST["useraddy1"])) {
    $useraddy1err = "Address is required";
    } else {
    $useraddy1 = test_input($_POST["useraddy1"]);
    if (!preg_match("/^[a-zA-Z0-9 ]*$/",$useraddy1)) {
    $useraddy1err = "Only alphanumerics and white space allowed";
    } else {$useraddy1err = "";}
        }

    if (empty($_POST["useraddy2"])) {
    $useraddy2err = "Address is required";
    } else {
    $useraddy2 = test_input($_POST["useraddy2"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$useraddy2)) {
    $useraddy2err = "Only letters and white space allowed";
    } else {$useraddy2err = "";}
        }

    if (empty($_POST["userfone"])) {
    $userfoneerr = "Address is required";
    } else {
    $userfone = test_input($_POST["userfone"]);
    if (!preg_match("/^[+0-9]*$/",$userfone)) {
    $userfoneerr = "Only + numbers and white space allowed";
    } else {$userfoneerr = "";}
        }

   if (empty($_POST["useremail"])) {
    $useremailerr = "Email is required";
    } else {
    $useremail = test_input($_POST["useremail"]);
    // check if e-mail address is well-formed
    if (!filter_var($useremail, FILTER_VALIDATE_EMAIL)) {
    $useremailerr = "Invalid email format"; 
        }
    }

    if (empty($_POST["userurl"])) {
    $userurl = "";
    } else {
    $userurl = test_input($_POST["userurl"]);
    // check if URL address syntax is valid (this regular expression also 
    allows dashes in the URL)
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?
    =~_|!:,.;]* [-a-z0-9+&@#\/%=~_|]/i",$userurl)) {
    $userurlerr = "Invalid URL"; 
            }
        }
    }
    function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
        }
    ?>

<h4>User Input</h4>
<form  id="setUp" method="post" action="<?php echo  
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table id="contact" class="userInfo">
<tbody>
<tr>
<th class="cdtl">User Name:</td>
<td><input type="text" name='username' value="<?php echo $username;?>" /> 
</td>
<td class='err'><?php echo $usernameerr;?></td>
</tr>
<tr>
<th class="cdtl">Address1:Number, Street, City</td>
<td><input type="text" name='useraddy1' value="<?php echo $useraddy1;?>" />    
</td>
<td class='err'><?php echo $useraddy1err;?></td>
</tr>
<tr>
<th class="cdtl">Address2:State, Country</td>
<td><input type="text" name='useraddy2' value="<?php echo $useraddy2;?>" />
</td>
<td class='err'><?php echo $useraddy2err;?></td>
</tr>
<tr>
<th class="cdtl">Telephone (International format)</td>
<td><input type="text" name='userfone' value="<?php echo $userfone;?>" />   
</td>
<td class='err'><?php echo $userfoneerr;?></td>
</tr>
<tr>
<th class="cdtl">e-mail Address</td>
<td><input type="email" name='useremail' value="<?php echo $useremail;?>" />   
</td>
<td class='err'><?php echo $useremailerr;?></td>

</tr>
<tr>
<th class="cdtl">Website (Optional)</td>
<td><input type="url" name='userurl' value="<?php echo $userurl;?>" /></td>
<td class='err'><?php echo $userurlerr;?></td>
</tr>
</tbody>
</table>
<br>
<input  class="btn" type="submit" value="Submit" />
</form>
</body>
</html>

0 个答案:

没有答案
相关问题