0

会社にメールを送信する前に、このフォームを検証しようとしています。フォームは検証されず、誰かが[送信]をクリックするたびに送信されます。検証は行われていません。これについてどうしたらよいかわかりません。

これがフォームの一部です

<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{
}
}
}
?>
4

7 に答える 7

3

PHP がフォーム データを読み取るために必要なことがいくつかあります。まず、Waygood が提案したように、$_POST 値を変更して、渡された変数名を一重引用符で囲むことをお勧めします。

//For example:
$_POST[address]; 

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

次に、ジョンが答えたように、渡された送信された値への参照はhtml フォームにありません。最初のステートメントを作成するために、値 1 を保持する非表示の入力をフォーム (タグ間の任意の場所) に追加する必要があります。残りの php コードを実行するには true にします。

<!-- 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

    }

?>
于 2012-08-07T16:31:12.233 に答える
1

名前の付いたフォームフィールドが表示されません。フォームフィールドがsubmittedないと、PHP検証コードはトリガーされません。変更してみてください:

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

このようなより一般的なものに:

if ('POST' === $_SERVER['REQUEST_METHOD']) {
于 2012-08-07T16:16:47.920 に答える
1

OK @キバハイトフィールド

配列要素への参照を次のように変更します。

$_POST[first_name]

に:

$_POST['first_name']
于 2012-08-07T16:19:56.847 に答える
1
  1. if はありません

    else{ if ($errormsg){ //既にエラーがある場合は、次のエラーを追加 $errormsg = $errormsg; }そうしないと{

    }
    

    }

  2. $errormsg = $errormsg; 何の意味もありません。

  3. Errormsg が割り当てられますが、使用されることはありません。echo $errormsg; を使用します。
  4. $_POST[first_name] は $_POST['first_name'] にする必要があります
  5. $_POST['submitted'] の代わりに if($_POST['field89'] == 'Submit') を使用します
  6. 小さなフォームで試してみて、機能し始めたら拡張してみてはいかがでしょうか。
于 2012-08-07T16:56:24.460 に答える
0

または、この方法で簡単に確認できます

if($_POST){
 //form is posted do your work here
}
于 2012-08-07T16:18:56.263 に答える
0

フォーム(「GET」、「POST」)のメソッドを指定しましたか?

于 2012-08-07T16:37:37.997 に答える
0

エラーを表示する方法がないようです。これを php コードの最後に追加してみてください:

echo $errormsg; die();

それ以外の

$errormsg = $errormsg;

何が起こるか教えてください。

于 2012-08-07T16:26:46.443 に答える