2014-12-03 7 views
2

이름과 라디오를 확인해야하므로 오류 코드가 작성되었습니다. 그렇지 않으면 확인 페이지로 이동하여 필드 옆에 오류 메시지를 보낼 수 없습니다. 코드를 놓친 경우 도움주세요.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는 말한다 : 이것은 내 유효성 검사가 작동하지 않습니다 제외하고 페이지를 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> 

. 당신은 여기에 구문 오류가

<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

확신 라디오의 약간 다른 나와. ''양식을 게시 한 후에 출력 하시겠습니까? –

+1

'echo '

';print_r($_POST);echo '
';'submit.php를 출력 할 수 있습니까? – skobaljic

+0

어떻게 라디오 결과를 이메일로 보내나요? – Violin

답변

1

:

양식 코드로 위입니다

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

당신이 그리워을 자선 VAR에서 $ : 약

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

당신의 두 번째 질문은 귀하의 양식이 조금 엉망이라고 생각합니다.

  • 캡처

  • 바르

  • 을 확인하는

  • 쇼 오류를 proccesing : 이 구조를 사용하여, 형태, 검증, 오류 관리 및 proccesing에 같은 페이지를 사용할 수 있습니다 또는 성공 메시지라면

  • 렌더링 형식 오류 또는이

    를 전송하지 않을 경우

는 다음과 같이 몇 가지를 시도해보십시오

<?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에게 많이 감사합니다! 내 다음 관심사는 유효성 검사 중이며 이미 오류 코드가 있지만 사용자가 빈 이름을 클릭하면 빈 데이터가 전송되고 "submit.php"페이지로 이동합니다. 어떤 부분 코드가 빠졌습니까? – Violin

+0

양식 제출이 다른 페이지를로드하기 때문에 javascript를 사용하지 않으면 빈 양식 제출을 피할 수 없습니다. 값이 유효하지 않은 경우 양식 처리를 중지하기 위해 조건부를 사용하여 동일한 양식 페이지로 제출하여 수정할 수 있습니다. 또한 다음 페이지와 뒤로 버튼 또는 링크에서 오류를 표시 할 수 있지만 덜 공상적입니다. – skeptic