2014-12-03 216 views
2

我創建了Err代碼,因此必須檢查名稱和廣播,否則它不能移動到確認頁面並在該字段旁邊發送錯誤消息。如果我錯過任何代碼,請幫助!PHP驗證失敗

<?php 
$nameErr = $charityErr = ""; 
$name = $charity = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["name"])) { 
     $nameErr = "Name is missing"; 
    } 
    else { 
     $name = $_POST["name"]; 
    } 
    if (!isset($_POST["charity"])) { 
     $charityErr = "You must select 1 option"; 
    } 
    else { 
     $charity = $_POST["charity"]; 
    } 
} 
?> 
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> 
    <link type="text/css" href="testStyle.css" rel="stylesheet"/> 
<title>Survey</title> 
</head> 

<body> 
<div><!--start form--> 
    <form method="post" action="submit.php"> 
    <input type="radio" name="charity" <?php if (isset($charity) && $charity == "1") echo "checked"; ?> value="1">1 
<input type="radio" name="charity" <?php if (isset($charity) && $charity == "2") echo "checked"; ?> value="2">2 
<input type="radio" name="charity" <?php if (isset($charity) && $charity == "3") echo "checked"; ?> value="3">3 
<span class="error"><?php echo $charityErr;?></span> 
    <input type="text" name="name" placeholder="ENTER YOUR COMPANY NAME"> 
    <span class="error"><?php echo $nameErr;?></span> 
    <input type="submit" name="submit" value="Submit"/>  
</form> 
</div><!--end form--> 

</body> 
</html> 

我submit.php說:

/* Subject and Email Variables */ 
    $emailSubject = 'Survey!'; 
    $webMaster = '[email protected]'; 

/* Gathering Data Variables */ 
    $name = $_POST ['name']; 
    $charity = $_POST ['charity']; 

    //create a new variable called $body line break, say email and variable, don't leave any space next to EOD - new variable $body 3 arrows less than greater than, beyond EOD no spaces 
    $body = <<<EOD 
<br><hr><br> 
Company Name: $name <br> 
Charity: $charity <br> 
EOD; 

    //be able to tell who it's from 
    $headers = "From: $email\r\n"; 
    $headers .= "Content-type: text/html\r\n"; 
    $success = mail($webMaster, $emailSubject, $body, $headers); 


/* Results rendered as HTML */ 
    $theResults = <<<EOD 
<html> 
blah blah 
</html> 

這重定向罰款submit.php頁面除了我的驗證不起作用,並將空白數據。

表格上面的代碼是:

<form method="post" action="submit.php"> 

    <input type="radio" name="charity" 
<?php if (isset($charity) && $charity == "1") echo "checked"; ?> 
value="1">1 
<input type="radio" name="charity" 
<?php if (isset($charity) && $charity == "2") echo "checked"; ?> 
value="2">2 
<input type="radio" name="charity" 
<?php if (isset($charity) && $charity == "3") echo "checked"; ?> 
value="3">3 
<span class="error"><?php echo $charityErr;?></span> 

    <input type="text" name="name" placeholder="ENTER YOUR COMPANY NAME"> 
    <span class="error"><?php echo $nameErr;?></span> 
    <input type="submit" name="submit" value="Submit"/> 

</form> 
+0

很肯定收音機的問世略有不同。什麼'<?php print $ charity; ?>一旦你發佈了表單? – 2014-12-03 23:41:22

+1

你可以輸出'echo'

';print_r($_POST);echo '
';'在submit.php? – skobaljic 2014-12-03 23:45:02

+0

你如何收發電子郵件的結果? – Violin 2014-12-03 23:46:49

回答

1

你有一個語法錯誤的位置:

<?php if (isset($charity) && charity == "3") echo "checked"; ?> 

你錯過$慈善VAR:

<?php if (isset($charity) && $charity == "3") echo "checked"; ?> 

關於你的第二個問題,我認爲你的表格有點混亂。 您可以使用同一個頁面的形式,驗證,錯誤管理和proccesing,採用這種結構:

  • 捕獲瓦爾

  • 驗證

  • proccesing

  • 顯示錯誤如果有的話或成功msg

  • render form如果錯誤或不發送

嘗試一些類似這樣的:

<?php 

//Capture POST/GET vars 
$charity = $_REQUEST['charity']; 
$name = $_REQUEST['name']; 
$step = $_REQUEST['step']; 

//You can add some sanitizing to the vars here 


//Form sent if step == 1 
if ($step == 1){ 

    /* 
    * Validate form 
    */ 

    //Initialize error's array 
    $error = array(); 

    //No charity value error 
    if (!$charity){ 
     $error[] = 'You must select 1 option'; 
    } 

    //Missing name error 
    if (!$name){ 
     $error[] = 'Name is missing'; 
    } 

    //Add any other validation here 



    /* 
    * Process form if not error 
    */ 

    if (!$error){ 

     //Send eMail 
     $subject = "Your subject here"; 
     $toemail = "<[email protected]>"; 
     $bounce = "<[email protected]>"; 
     $message = " 
        Company Name: $name<br> 
        Charity: $charity <br>"; 
     $subject = '=?UTF-8?B?'.base64_encode(utf8_encode($subject)).'?='; 
     $headers = "From: <[email protected]>" . "\r\n" . 
     "Reply-To: <[email protected]>" . "\r\n" . 
     "X-Mailer: PHP/" . phpversion(); 
     mail($toemail, $subject, $message, $headers, "-f $bounce"); 

     //Add any other proccesing here, like database writting 

    } 

    /* 
    * Show errors || succsess msg on the top of the form 
    */ 

    if ($error){ 
     unset($step); //if error, show the form 
     echo '<div style="color:yellow;background-color:red;">ERROR:<br>'; 
     foreach ($error as $e){ 
      echo '- '.$e.'<br>'; 
     } 
     echo '</div><br>'; 
    }else{ 
     echo '<div>Form succesfully sent</div><br>'; 

    } 



} 


/* 
* Form rendering 
*/ 

if (!$step){ 
?> 

<form method="post" action=""> 
    <input type="radio" name="charity" value="1" <?php echo ($charity == "1") ? ' checked':''; ?>>1 
    <input type="radio" name="charity" value="2" <?php echo ($charity == "3") ? ' checked':''; ?>>2 
    <input type="radio" name="charity" value="3" <?php echo ($charity == "3") ? ' checked':''; ?>>3 
    <input type="text" name="name" placeholder="ENTER YOUR COMPANY NAME"> 
    <input type="hidden" name="step" value="1"> 
    <input type="submit" name="submit" value="Submit"/>  
</form> 


<?php 
} 
+0

你是鷹派!我沒有看到這一切。現在電子郵件作品非常感謝你soooooo!我的下一個擔心是驗證,我已經放置了Err代碼,但是當用戶點擊空名稱時,它仍然發送空白數據並進入「submit.php」頁面。我錯過了哪部分代碼? – Violin 2014-12-04 01:59:51

+0

當表單提交正在加載另一個頁面時,除非使用JavaScript,否則無法避免空表單提交。如果值未經驗證,您可以使用條件解決它提交到相同的表單頁面以停止表單處理。此外,您可以在下一頁和後退按鈕或鏈接中顯示錯誤,但不那麼花哨。 – skeptic 2014-12-04 13:09:03