2013-06-14 3 views
0

PHP 언어의 초보자로서 저는 자바 스크립트 유효성 검사 코드와 함께 HTML 코드로 PHP 문의 양식 (contact.php)을 설정했습니다. 완료되면 모든 일들이 제출되지 않고 주어진 이메일 ID로 전송됩니다. 정말로 문제가 발생한 코드를 파악할 수 없었습니다. 아래 코드를 확인하고이를 해결할 수 있도록 도와 주시겠습니까? 양식 시작PHP 양식 제출 오류를 수정하는 방법?

<?php if($form_complete === FALSE): ?> 
             <form name="contact_form" method="post" id="contact_form" action="contacts.php" onsubmit="return defaultagree(this)"><table width="430" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td valign="middle" align="right" width="130" class="offerformtext"><span>*</span> Name:</td> 
    <td valign="top" align="left" width="300" class="mbox"><input name="fullname" id="fullname" type="text" class="offer_input_box" value="<?php echo isset($_POST['fullname'])? $_POST['fullname'] : ''; ?>" /><?php if(in_array('fullname', $validation)): ?><span class="error"><?php echo $error_messages['fullname']; ?></span><?php endif; ?></td> 
    </tr> 
</table> 
</td> 
    </tr> 
    <tr> 
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td valign="middle" align="right" width="130" class="offerformtext"><span>*</span> Email:</td> 
    <td valign="top" align="left" width="300" class="mbox"><input name="email" id="email" type="text" class="offer_input_box" value="<?php echo isset($_POST['email'])? $_POST['email'] : ''; ?>" /><?php if(in_array('email', $validation)): ?><span class="error"><?php echo $error_messages['email']; ?></span><?php endif; ?></td> 
    </tr> 
</table> 
    </td> 
    </tr> 
    <tr> 
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td valign="middle" align="right" width="130" class="offerformtext">Phone:</td> 
    <td valign="top" align="left" width="300"><input name="phone" id="phone" type="text" value="" class="offer_input_box" /></td> 
    </tr> 
</table></td> 
    </tr> 
    <tr> 
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td valign="middle" align="right" width="130" class="offerformtext"><span>*</span> Country:</td> 
    <td valign="top" align="left" width="300" class="mbox"><input name="country" id="country" type="text" class="offer_input_box" value="<?php echo isset($_POST['country'])? $_POST['country'] : ''; ?>" /></textarea><?php if(in_array('country', $validation)): ?><span class="error"><?php echo $error_messages['country']; ?></span><?php endif; ?></td> 
    </tr> 
</table></td> 
    </tr> 
    <tr> 
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td valign="top" align="right" width="130" class="offerformtext"><span>*</span> Message:</td> 
    <td valign="top" align="left" width="300" class="mmbox"><textarea name="message" id="message" class="offer_message_box" value="<?php echo isset($_POST['message'])? $_POST['message'] : ''; ?>" /></textarea><?php if(in_array('message', $validation)): ?><span class="error"><?php echo $error_messages['message']; ?></span><?php endif; ?></td> 
    </tr> 
</table></td> 
    </tr> 
    <tr> 
    <td valign="bottom" align="center" class="subdown"><input name="submit" type="Submit" class="offer_submit_button" value=""/><br /><br /></td> 
    </tr> 
    <tr> 
    <td><input type="hidden" name="ipaddress" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" /></td> 
    </tr> 
</table> 
</form> 
<?php else: ?> 
               <h2 style="font-family:'Times New Roman', Times, serif; font-size:24px; color:#6d6d6d; font-weight:bold;">Contact Form Successfully Submitted</h2> 
<p style="font-family:'Times New Roman', Times, serif; font-size:18px; color:#255E67; margin-left:25px; margin-top:15px;">Thank you for your get in touch with us! We would get back to you regarding this information soon.</p> 
<script type="text/javascript"> 
setTimeout('ourRedirect()', 8000) 
function ourRedirect(){ 
    location.href='contacts.php' 
} 
</script> 
<?php endif; ?> 
            <script> 
document.forms.contact_form.agreecheck.checked=false 
</script> 

양식 끝

<?php 

// Set email variables 
$email_to = '[email protected]'; 
$email_subject = 'New Contact Form Submission'; 

/* Gathering Data Variables */ 

    $fullname = $_POST['fullname']; 
    $email = $_POST['email']; 
    $phone = $_POST['phone']; 
    $country = $_POST['country']; 
    $message = $_POST['message']; 
    $ipaddress = $_POST['ipaddress']; 

    $body = <<<EOD 
<br><hr><br> 
Full Name: $fullname <br> 
Email: $email <br> 
Phone: $phone <br> 
Country: $country <br> 
Message: $message <br> 
Submitted By IP: $ipaddress <br> 
EOD; 

    $headers = "From: $fullname\r\n"; 
    $headers .= "Content-type: text/html\r\n"; 
    $success = mail($webMaster, $emailSubject, $body, 
$headers); 

// Set required fields 
$required_fields = array('fullname','email','country','message'); 

// set error messages 
$error_messages = array(
    'fullname' => 'Please enter Your Name to proceed.', 
    'email' => 'Please enter a valid Email Address to contact.', 
    'country' => 'Please enter your country of residence.', 
    'message' => 'Please enter your message.' 
); 

// Set form status 
$form_complete = FALSE; 

// configure validation array 
$validation = array(); 

// check form submittal 
if(!empty($_POST)) { 
    // Sanitise POST array 
    foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value)); 

    // Loop into required fields and make sure they match our needs 
    foreach($required_fields as $field) {  
     // the field has been submitted? 
     if(!array_key_exists($field, $_POST)) array_push($validation, $field); 

     // check there is information in the field? 
     if($_POST[$field] == '') array_push($validation, $field); 

     // validate the email address supplied 
     if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field); 
    } 

    // basic validation result 
    if(count($validation) == 0) { 
     // Prepare our content string 
     $email_content = 'Contact Form Submission Received: ' . "\n\n"; 

     // simple email content 
     foreach($_POST as $key => $value) { 
      if($key != 'submit') $email_content .= $key . ': ' . $value . "\n"; 
     } 

     // if validation passed ok then send the email 
     mail($email_to, $email_subject, $email_content); 

     // Update form switch 
     $form_complete = TRUE; 
    } 
} 

function validate_email_address($email = FALSE) { 
    return (preg_match('/^[^@\s][email protected]([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE; 
} 

function remove_email_injection($field = FALSE) { 
    return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field)); 
} 

?> 

이 내가 사용했던 코드는,이 문제에 대해 저를 도와주세요하십시오. 미리 감사드립니다.

+0

제어 코드가'mail()'코드 줄에 도달합니까? –

+0

감사합니다. Hanky.하지만 내 이메일에는 아무 것도 없습니다. 도와주세요. – Sumu

+0

@Sumu'$ webMaster' 변수는 어디에도 설정되어 있지 않으므로 송신 할 곳을 알지 못합니다. 'it = form_message' ;-) 당신은 또한 아래의 'amigura's' 대답을 사용할 수 있습니다. –

답변

0

약간 코드가 변경되었습니다. 그것을 검증하기 전에 이메일을 작성하는 것 같습니다. 두 개가 아닌 한 파일에 모든 코드를 포함 할 수 있습니다.

<?php 

    // Set email variables 
$email_to = '[email protected]'; 
$email_subject = 'New Contact Form Submission'; 
$ipaddress=$_SERVER['REMOTE_ADDR']; 

// Set required fields 
$required_fields = array('fullname','email','country','message'); 

// set error messages 
$error_messages = array(
    'fullname' => 'Please enter Your Name to proceed.', 
    'email' => 'Please enter a valid Email Address to contact.', 
    'country' => 'Please enter your country of residence.', 
    'message' => 'Please enter your message.' 
); 



// check form submittal 
if(!empty($_POST)) { 


// Sanitise POST array 
foreach($_POST as $key=>$value){ 

if($key=="fullname"){$fullname=str_replace('[at]','@', $value);} 
if($key=="country"){$country=str_replace('[at]','@', $value);} 
if($key=="message"){$message=str_replace('[at]','@', $value);} 
if($key=="phone"){$phone=str_replace('[at]','@', $value);} 

if($key=="email"){ 
if(!filter_var($value, FILTER_VALIDATE_EMAIL)) { $error_out[$key]=$error_messages[$key];} else{$email=$value;} 
    } 

// check required fields 
     if(in_array($key, $required_fields) and empty($value)) 
     { 
      $error_out[$key]=$error_messages[$key]; // errors for required 
     } 

if($key=="amigura"){$amigura='';}else{$amigura='';} 

} 

$body = <<<EOD 
<br><hr><br> 
Full Name: $fullname <br> 
Email: $email <br> 
Phone: $phone <br> 
Country: $country <br> 
Message: $message <br> 
Submitted By IP: $ipaddress <br> 
EOD; 

//echo $body; 

    // send mail 
    if(!$error_out){ 

$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "From: $fullname <$email> \r\n"; 
$headers .= "Content-type: text/html;charset=UTF-8 \r\n"; 

    if(mail($email_to, $emailSubject, $body,$headers)){$form_complete='sendme';} 

     } 
     else 
     {$form_complete='';} 

} 


?> 

<?php if($form_complete != 'sendme'): ?> 
             <form name="contact_form" method="post" id="contact_form" action="" onsubmit="return defaultagree(this)"><table width="430" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td valign="middle" align="right" width="130" class="offerformtext"><span>*</span> Name:</td> 
    <td valign="top" align="left" width="300" class="mbox"><input name="fullname" id="fullname" type="text" class="offer_input_box" value="<?php echo htmlspecialchars($_POST['fullname'], ENT_QUOTES); ?>" /><?php if($error_out['fullname']): ?><span class="error"><?php echo $error_out['fullname']; ?></span><?php endif; ?></td> 
    </tr> 
</table> 
</td> 
    </tr> 
    <tr> 
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td valign="middle" align="right" width="130" class="offerformtext"><span>*</span> Email:</td> 
    <td valign="top" align="left" width="300" class="mbox"><input name="email" id="email" type="text" class="offer_input_box" value="<?php echo htmlspecialchars($_POST['email'] , ENT_QUOTES); ?>" /><?php if($error_out['email']): ?><span class="error"><?php echo $error_out['email']; ?></span><?php endif; ?></td> 
    </tr> 
</table> 
    </td> 
    </tr> 
    <tr> 
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td valign="middle" align="right" width="130" class="offerformtext">Phone:</td> 
    <td valign="top" align="left" width="300"><input name="phone" id="phone" type="text" value="<?php echo htmlspecialchars($_POST['phone'] , ENT_QUOTES); ?>" class="offer_input_box" /></td> 
    </tr> 
</table></td> 
    </tr> 
    <tr> 
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td valign="middle" align="right" width="130" class="offerformtext"><span>*</span> Country:</td> 
    <td valign="top" align="left" width="300" class="mbox"><input name="country" id="country" type="text" class="offer_input_box" value="<?php echo htmlspecialchars($_POST['country'], ENT_QUOTES); ?>" /></textarea><?php if($error_out['country']): ?><span class="error"><?php echo $error_out['country']; ?></span><?php endif; ?></td> 
    </tr> 
</table></td> 
    </tr> 
    <tr> 
    <td valign="top" align="left"><table width="430" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td valign="top" align="right" width="130" class="offerformtext"><span>*</span> Message:</td> 
    <td valign="top" align="left" width="300" class="mmbox"><textarea name="message" id="message" class="offer_message_box" value="<?php echo htmlspecialchars($_POST['message'], ENT_QUOTES); ?>" /></textarea><?php if($error_out['message']): ?><span class="error"><?php echo $error_out['message']; ?></span><?php endif; ?></td> 
    </tr> 
</table></td> 
    </tr> 
    <tr> 
    <td valign="bottom" align="center" class="subdown"><input name="submit" type="Submit" class="offer_submit_button" value=""/><br /><br /></td> 
    </tr> 
</table> 
</form> 
<?php else: ?> 
               <h2 style="font-family:'Times New Roman', Times, serif; font-size:24px; color:#6d6d6d; font-weight:bold;">Contact Form Successfully Submitted</h2> 
<p style="font-family:'Times New Roman', Times, serif; font-size:18px; color:#255E67; margin-left:25px; margin-top:15px;">Thank you for your get in touch with us! We would get back to you regarding this information soon.</p> 
<script type="text/javascript"> 
setTimeout('ourRedirect()', 8000) 
function ourRedirect(){ 
    location.href='contacts.php' 
} 
</script> 
<?php endif; ?> 

<script> 
//document.forms.contact_form.agreecheck.checked=false 
</script> 
+0

감사합니다. @amigura, $ web_aster를 $ email_to (으)로 변경했습니다. 이메일이 오지만 성공적인 메시지 페이지로 리디렉션되지 않으며, 제출하지 않고 제출 버튼을 클릭하면 빈 양식이 제출됩니다. 데이터를 입력란에 입력하십시오. 도와주세요. – Sumu

+0

감사합니다. 이제 이메일을 받았지만 "보낸 사람"필드에 전자 메일 (양식 제출자)이 표시되지 않고 "[email protected]"과 같은 서버 기본 전자 메일이 표시됩니다. 어떻게하면 바꿀 수 있습니까? 감사합니다. – Sumu

+0

@Sumu는 예제 2를 보았습니다. [php mail] (http://php.net/manual/en/function.mail.php) – amigura