为什么 php 不适用于我的表单


Why does the php not work for my form?

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

这是一些形式

<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{
}
}
}
?>
为了让

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

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

其次,正如 John 所回答的那样,html 表单中没有引用传递的提交值,您需要在表单中添加一个隐藏的输入(标签之间的任何位置),该输入保存值为 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
    }
?>

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

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

像这样更通用的东西:

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

OK @KibaHeitfield

从以下位置更改对数组元素的引用:

$_POST[first_name]

自:

$_POST['first_name']
  1. 没有如果可以遵循其他

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

    }
    

    }

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

  3. 错误消息已分配,但从未使用。使用回声$errormsg;
  4. $_POST[first_name] 应为 $_POST['first_name']
  5. 代替 $_POST['submitted'] 使用 if($_POST['field89'] == '提交')
  6. 您为什么不尝试一个小窗体并在它开始工作时展开。

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

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

您似乎无法显示错误。尝试将其添加到 php 代码的底部:

echo $errormsg; die();

而不是

$errormsg = $errormsg;

让我们知道会发生什么。

您是否为表单("GET","POST")提供了方法?