【问题标题】:Why does the php not work for my form? [closed]为什么 php 不适用于我的表单? [关闭]
【发布时间】:2012-08-07 16:14:29
【问题描述】:

在向我的公司发送电子邮件之前,我一直在尝试验证此表单。它不会验证表单,它只会在有人点击提交时发送。没有验证发生,我不知道该怎么做。

这是一个小表格

<tr>
<td><label for="reason3">Reason for Leaving?</label></td>
<td><input type="text" id="reason3" name="reason3" class="c3"></td>
</tr>
<tr>
<td colspan="2">
<hr size="1.5" color="#000000" noshade></td>
</tr>
<tr>
<td><label for="maywe">May we contact the employers listed above?<br>
If not, indicate which you do not wish us to contact:</label></td>
<td><input type="text" id="maywe" name="maywe" class="c3"></td>
</tr>
<tr>
<td><label for="additional">Any additonal information you would like us to<br>
consider when reviewing your application?</label></td>
<td><input type="text" id="additional" name="additional" class="c3"></td>
</tr>
<tr>
<td colspan="2">
<hr size="1.5" color="#000000" noshade></td>
</tr>
<tr>
<td colspan="2">
<div class="c2">
<hr size="1.5" color="#000000" noshade>
        The above information is true and complete to the best of my knowledge. Should I become<br>
employed by <a href="http://www.creativeurethanes.com/">Creative Urethanes, Inc.,</a> any false statements on this application may be<br>
considered sufficient cause for dismissal.<br>
        I understand that filling out this application does not indicate that there are necessarily any<br>
vacant positions at this time, nor does it in any way obligate this Company.<br>
        I acknowledge that I have read Creative Urethanes' <a href="Memo.html">Memo on Right to Know</a>. <input type="checkbox" name="Yes" value="Yes"><span class="c1"><sup><i>*</i></sup></span><br>
<br></div>
</td>
</tr>
<tr>
<td colspan="2">
<hr size="1.5" color="#000000" noshade></td>
</tr>
<tr>
<td colspan="2">Sign electronically with your full name and SSN.</td>
</tr>
<tr>
<td><label for="electronicsignfn"><u>Full</u> <u>Name</u><span class="c1"><sup><i>*</i></sup></span></label></td>
<td><input type="text" id="electronicsignfn" name="electonicsignfn" class="c3"></td>
</tr>
<tr>
<td><label for="electronicsignssn"><u>S.S.N.</u><span class="c1"><sup><i>*</i></sup></span></label></td>
<td><input type="text" id="electronicsignssn" name="electronicsignssn" class="c3"></td>
</tr>
<tr>
<td><label for="emailaddress"><u>Email</u> <u>Address</u><span class="c1"><sup><i>*</i></sup></span></label></td>
<td><input type="text" id="emailaddress" name="emailaddress"></td>
</tr>
<tr>
<td colspan="2">
<hr size="1.5" color="#000000" noshade></td>
</tr>
<tr>
<td><input type="submit" id="field89" name="field89" class="c3" value="Submit"></td>
<td><input type="reset" id="field90" name="field90" class="c3" value="Reset"></td>
</tr>
</table>
</form>
</body>
</html>

这里是php

<?php
if ($_POST['submitted']==1) {
$errormsg = "";
if ($_POST[first_name]){
$first_name = $_POST[first_name];
}
else{
$errormsg = "Please enter your first name";
}
if ($_POST[last_name]){
$last_name = $_POST[last_name];
}
else{
$errormsg = "Please enter your last name";
}
if ($_POST[ssn]){
$ssn = $_POST[ssn];
}
else{
$errormsg = "Please enter your Social Security Number";
}
if ($_POST[phone]){
$phone = $_POST[phone];
}
else{
$errormsg = "Please enter your phone number";
}
if ($_POST[address]){
$address = $_POST[address];
}
else{
$errormsg = "Please enter your address";
}
if ($_POST[city]){
$city = $_POST[city];
}
else{
$errormsg = "Please enter your city";
}
if ($_POST[state]){
$state = $_POST[state];
}
else{
$errormsg = "Please select your state";
}
if ($_POST[zip]){
$zip = $_POST[zip];
}
else{
$errormsg = "Please enter your 5 digit zip code";
}
if ($_POST[position]){
$position = $_POST[position];
}
else{
$errormsg = "Please enter N/A if open for discussion";
}
if ($_POST[wage]){
$wage = $_POST[wage];
}
else{
$errormsg = "Please enter 0.00 if open for discussion";
}
if ($_POST[Emergencycontactname]){
$Emergencycontactname = $_POST[Emergencycontactname];
}
else{
$errormsg = "Please enter your emergency contact's name";
}
if ($_POST[Emergencycontactaddress]){
$Emergencycontactaddress = $_POST[address];
}
else{
$errormsg = "Please enter your emergency contact's address";
}
if ($_POST[Emergencycontactcity]){
$Emergencycontactcity = $_POST[Emergencycontactcity];
}
else{
$errormsg = "Please enter your emergency contact's city";
}
if ($_POST[Emergencycontactstate]){
$Emergencycontactstate = $_POST[Emergencycontactstate];
}
else{
$errormsg = "Please select your Emergency contact's state";
}
if ($_POST[Emergencycontactzip]){
$Emergencycontactzip = $_POST[Emergencycontactzip];
}
else{
$errormsg = "Please enter your emergency contact's 5 digit zip code";
}
if ($_POST[Emergencycontactphone]){
$Emergencycontactphone = $_POST[Emergencycontactphone];
}
else{
$errormsg = "Please enter your emergency contact's phone number";
}
if ($_POST[hightest]){
$highest = $_POST[highest];
}
else{
$errormsg = "Please enter your highest level of education";
}
if ($_POST[degree]){
$degree = $_POST[degree];
}
else{
$errormsg = "Please enter N/A if none";
}
if ($_POST[age]){
$age = $_POST[age];
}
else{
$errormsg = "Please enter yes or no";
}
if ($_POST[problems]){
$problems = $_POST[problems];
}
else{
$errormsg = "Please enter yes or no";
}
if ($_POST[shiftwork]){
$shiftwork = $_POST[shiftwork];
}
else{
$errormsg = "Please enter yes or no";
}
if ($_POST[apprenticeship]){
$apprenticeship = $_POST[apprenticeship];
}
else{
$errormsg = "Please enter yes or no";
}
if ($_POST[disabilities]){
$disabilities = $_POST[disabilities];
}
else{
$errormsg = "Please enter yes or no";
}
if ($_POST[ifyes]){
$ifyes = $_POST[ifyes];
}
else{
$errormsg = "if you answered no to above question, put N/A";
}
if ($_POST[accommodate]){
$accommodate = $_POST[accommodate];
}
else{
$errormsg = "if you answered N/A to above question, put N/A";
}
if ($_POST[experience]){
$experience = $_POST[experience];
}
else{
$errormsg = "Please enter yes or no";
}
if ($_POST[ifyes2]){
$ifyes2 = $_POST[ifyes2];
}
else{
$errormsg = "if you answered no to above question, put N/A";
}
if ($_POST[cname]){
$cname = $_POST[cname];
}
else{
$errormsg = "Please enter company name";
}
if ($_POST[caddress]){
$caddress = $_POST[caddress];
}
else{
$errormsg = "Please enter company address";
}
if ($_POST[cphone]){
$cphone = $_POST[cphone];
}
else{
$errormsg = "Please enter company phone number";
}
if ($_POST[ccperson]){
$ccperson = $_POST[ccperson];
}
else{
$errormsg = "Please enter contact person's name";
}
if ($_POST[ctype]){
$ctype = $_POST[ctype];
}
else{
$errormsg = "Please enter type of work";
}
if ($_POST[cwage]){
$cwage = $_POST[cwage];
}
else{
$errormsg = "Please enter wage earned";
}
if ($_POST[cstart]){
$cstart = $_POST[cstart];
}
else{
$errormsg = "Please enter date started";
}
if ($_POST[cend]){
$cend = $_POST[cend];
}
else{
$errormsg = "Please enter date ended";
}
if ($_POST[creason]){
$creason = $_POST[creason];
}
else{
$errormsg = "Please enter reason for leaving";
}
if ($_POST[cname2]){
$cname2 = $_POST[cname2];
}
else{
$errormsg = "Please enter company name, or type N/A";
}
if ($_POST[caddress2]){
$caddress2 = $_POST[caddress2];
}
else{
$errormsg = "Please enter company address, or type N/A";
}
if ($_POST[cphone2]){
$cphone2 = $_POST[cphone2];
}
else{
$errormsg = "Please enter company phone number, or type N/A";
}
if ($_POST[ccperson2]){
$ccperson2 = $_POST[ccperson2];
}
else{
$errormsg = "Please enter contact person's name, or type N/A";
}
if ($_POST[ctype2]){
$ctype2 = $_POST[ctype2];
}
else{
$errormsg = "Please enter type of work, or type N/A";
}
if ($_POST[cwage2]){
$cwage2 = $_POST[cwage2];
}
else{
$errormsg = "Please enter wage earned, or type N/A";
}
if ($_POST[cstart2]){
$cstart2 = $_POST[cstart2];
}
else{
$errormsg = "Please enter date started, or type N/A";
}
if ($_POST[cend2]){
$cend2 = $_POST[cend2];
}
else{
$errormsg = "Please enter date ended, or type N/A";
}
if ($_POST[creason2]){
$creason2 = $_POST[creason2];
}
else{
$errormsg = "Please enter reason for leaving, or type N/A";
}
if ($_POST[cname3]){
$cname3 = $_POST[cname3];
}
else{
$errormsg = "Please enter company name, or type N/A";
}
if ($_POST[caddress3]){
$caddress3 = $_POST[caddress3];
}
else{
$errormsg = "Please enter company address, or type N/A";
}
if ($_POST[cphone3]){
$cphone3 = $_POST[cphone3];
}
else{
$errormsg = "Please enter company phone number, or type N/A";
}
if ($_POST[ccperson3]){
$ccperson3 = $_POST[ccperson3];
}
else{
$errormsg = "Please enter contact person's name, or type N/A";
}
if ($_POST[ctype3]){
$ctype3 = $_POST[ctype3];
}
else{
$errormsg = "Please enter type of work, or type N/A";
}
if ($_POST[cwage3]){
$cwage3 = $_POST[cwage3];
}
else{
$errormsg = "Please enter wage earned, or type N/A";
}
if ($_POST[cstart3]){
$cstart3 = $_POST[cstart3];
}
else{
$errormsg = "Please enter date started, or type N/A";
}
if ($_POST[cend3]){
$cend3 = $_POST[cend3];
}
else{
$errormsg = "Please enter date ended, or type N/A";
}
if ($_POST[creason3]){
$creason3 = $_POST[creason3];
}
else{
$errormsg = "Please enter reason for leaving, or type N/A";
}
if ($_POST[maywe]){
$maywe = $_POST[maywe];
}
else{
$errormsg = "Type names of companies you do not want us to contact, or type, ok all";
}
if ($_POST[additional]){
$additional = $_POST[additional];
}
else{
$errormsg = "Type N/A if you do not have any additional information";
}
if ($_POST[electronicsignfn]){
$electronicsignfn = $_POST[electronicsignfn];
## Heading ##    }
else{
$errormsg = "Please enter your full name";
}
if ($_POST[electronicsignssn]){
$electronicsignssn = $_POST[electronicsignssn];
}
else{
$errormsg = "Please enter your Social Security Number";
}
if ($_POST[emailaddress]){
$emailaddress = $_POST[emailaddress];
}
else{
$errormsg = "Please enter your email address";
}
else{
if ($errormsg){ //if there is already an error, add next error
$errormsg = $errormsg;
}else{
}
}
}
?>

【问题讨论】:

  • 将所有数组引用,如 $_POST[phone] 更改为 $_POST['phone']
  • 在检查每个表单变量时,您可能还应该使用连接运算符而不是赋值运算符,否则您最终会覆盖以前的错误消息。
  • 没错,@andrewsi,但它不会阻止脚本工作。
  • @Shomz - 这就是为什么它没有被添加为答案:)
  • :) 我在那里给了你+1...但我不知道为什么@Waygood 不会回答...

标签: php html forms validation


【解决方案1】:

为了让您的 PHP 读取表单数据,需要做一些事情。首先,正如 Waygood 建议的那样,最好将您的 $_POST 值更改为在传递的变量名称周围加上单引号。

//For example:
$_POST[address]; 

// Should change to:
$_POST['address']; //Notice the single quotes around the word address

其次,正如 John 所回答的,在您的 html 表单中没有对传递的 submitted 值的引用,您需要在表单中添加一个隐藏的输入(在您的标签之间的任何位置)保存值 1 以使您的第一条语句为真,以便执行其余的 php 代码。

<!-- Put this input tag anywhere between your opening and closing form tags in your html -->
<form method="POST" action="your_submission_page.php">
    <input type="hidden" name="submitted" value="1" />
</form>

最后,良好的做法通知用户表单中的所有错误,这样他们就不会在提交表单之前遇到一个又一个错误。您应该做的是构建一个动态的错误消息数组,检查是否有任何错误,然后将它们全部显示出来。

<?php
    // Create a dynamic array for the error messages and validate everything before sending them back to the form that shows them

    // Here is a sample of your code with the dynamic array built in
    $error_msgs = array ();   
    $error = 0; //Error flag     

    if ($_POST['cwage2']){
        $cwage2 = $_POST['cwage2'];
    } else {
        $error_msgs[] = "Please enter wage earned, or type N/A";
        $errors++; //Flag to let the code know that there are errors, used in every case
    }

    if ($_POST['cstart2']){
        $cstart2 = $_POST['cstart2'];
    } else {
        $error_msgs[] = "Please enter date started, or type N/A";
        $errors++; //Flag to let the code know that there are errors, used in every case
    }

    // Finally at the end, check to see if there are any errors and display them all
    if ($errors > 0) {
        //Display the amount of errors first
        echo "There were " . $errors . " errors in your submission, please correct them.<br/>";

        //Then display your error array
        foreach ($error_msgs as &$msg) {                
            echo $msg . "<br/>"; 
        }
    } else {
        // Add code here to submit the form however you'd like

    }

?>

【讨论】:

  • +1 我会 $errors=true;而不是 1
  • @Tony Mancini,我应该把
  • 开始
    标记和结束
    标记之间的任何位置。
  • 很好,我为 $errors 标志添加了不同的视角,而不是将其用作计数器,但是,使用 true 而不是 1 是更好的做法。
  • @TonyMancini,让我测试一下
【解决方案2】:

我没有看到名为 submitted 的表单字段,如果没有它,您的 PHP 验证代码将不会被触发。尝试改变:

if ($_POST['submitted']==1) {

像这样更通用的东西:

if ('POST' === $_SERVER['REQUEST_METHOD']) {

【讨论】:

  • 试过了,我完全忘记了“提交”的 lolz。虽然没有帮助。我有一个提交按钮,但它不会启动验证。我不擅长 php,并且使用了很多其他人使用过的类似示例,只是更改了一些字段等。除了写基本的 html 代码,这甚至不被认为是编程,我就像一个 2 岁的孩子试图做代数
【解决方案3】:

好的@KibaHeitfield

更改对数组元素的引用:

$_POST[first_name]

到:

$_POST['first_name']

【讨论】:

  • @Waygood,添加 'first_name' 没有帮助。
  • 没有。它只是防止 PHP 警告。
  • 如果 first_name 被声明为常量怎么办????你将如何调试你的代码? php.net/manual/en/language.types.array.php
【解决方案4】:
  1. 没有if可以跟随else

    否则{ if ($errormsg){ //如果已经有错误,添加下一个错误 $错误消息 = $错误消息; }其他{

    }
    

    }

  2. $errormsg = $errormsg;没有任何意义。

  3. Errormsg 已分配但从未使用过。使用 echo $errormsg;
  4. $_POST[first_name] 应该是 $_POST['first_name']
  5. 而不是 $_POST['submitted'] 使用 if($_POST['field89'] == 'Submit')
  6. 您为什么不尝试一个小表单并在它开始工作时展开。

【讨论】:

    【解决方案5】:

    或者你可以简单地检查这种方式

    if($_POST){
     //form is posted do your work here
    }
    

    【讨论】:

    • 理论上页面上可能存在不止一种形式。表单必须有像 $_POST['form1']['field1'] 这样的前缀,而且我们必须检查 if (!empty($_POST['form1']))
    【解决方案6】:

    您似乎无法显示错误。尝试将此添加到您的 php 代码的脚下:

    echo $errormsg; die();
    

    而不是

    $errormsg = $errormsg;
    

    让我们知道会发生什么。

    【讨论】:

    • 仍然没有验证,它所做的只是转到我的“谢谢”页面
    【解决方案7】:

    您是否为表单提供了方法(“GET”、“POST”)?

    【讨论】:

    • 我使用了“POST”方法
    猜你喜欢
    • 1970-01-01
    • 2019-03-31
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多