2011-08-18 2 views
-3
<form method="post" action="" id="infoform"> 
<label for="first"> 
    First Name</label> 
<input type="text" name="first" id="first" class="field validate[required]"> 
</br> 
<label for="surname"> 
    Last Name</label> 
<input type="text" name="surname" id="surname" class="field validate[required]"> </br> 
    <label for="email"> 
     Email</label> 
    <input type="text" name="email" id="email" class=" field email validate[required,custom[email]]"> 
     </br> 
     <div class="radios"> 
      <input id="test2" checked="checked" type="radio" name="message" value="register" /> 
     Register for Updates</input> 
    <input id="test1" type="radio" name="message" value="msgs" /> 
    Send a Message</input> 
</div> 
<div class="msg"> 
    <label for="message" id="messagelabel"> 
     Message</label> 
    <textarea type="text" name="message" id="message"></textarea> 
</div> 
<input id="submit" type="submit" value=""><div class="test3"> 
    <a class="sbmt">Register</a></div> 
    <div class="test4"> 
     <a class="sbmt">Send</a></div> 
</input> 
</form> 

'test1'라디오 박스가 선택되면 div msg가 나타납니다. 이것은 다음 이메일 PHP 스크립트로 아약스를 통해 전송됩니다 라디오 체크 박스가 다른 이메일 필드를 전송하는 경우

<?php 
    $EmailTo = "[email protected]"; 
    $adminSubject = "Civitas Message"; 

    $firstName = Trim(stripslashes($_POST['first'])); 
    $Surname = Trim(stripslashes($_POST['surname'])); 
    $Email = Trim(stripslashes($_POST['email'])); 
    $Message = Trim(stripslashes($_POST['message'])); 

    $adminheaders = 'From: "Civitas website" <[email protected]>'; 

    $adminBody .= "First Name: "; 
    $adminBody .= $firstName; 
    $adminBody .= "\n"; 
    $adminBody .= "Surname: "; 
    $adminBody .= $Surname; 
    $adminBody .= "\n"; 
    $adminBody .= "Email: "; 
    $adminBody .= $Email; 
    $adminBody .= "\n"; 
    $adminBody .= "Message: "; 
    $adminBody .= $Message; 
    $adminBody .= "\n"; 


    $adminMail = mail($EmailTo, $adminSubject, $adminBody, $adminheaders); 


    if ($adminMail){ 
     echo "true"; 
    } 
    else{ 
     echo "false"; 
    } 
?> 

그러나 나는 단지 메시지 상자가 표시되고있는 경우 전자 메일의 "메시지"부분은 전송하고자합니다. 어떻게해야합니까?

+1

어디에서 "메시지"부분을 표시 하시겠습니까? 당신은 질문이 오히려 불분명합니다. 대답을 얻기 위해 편집해야합니다. – JMax

답변

0

PHP는 POST 또는 GET 방법 보낼 때 그 대신, 그것은 name 특성에서 보이는 요소의 id 보지 않는다. 작성한 코드에는 두 개의 입력 요소 인 textarearadio input이 있으며 "message"라는 이름이 있습니다. 둘 다 전송되면 $_POST 변수가 배열이되어 name 속성을 제출 된 값의 색인으로 사용하고 PHP 배열에 중복 색인이 없어 질 수 있으므로이 문제를 해결하려면 이름을 바꾸십시오. radio input의 경우 name 속성을 type으로 설정하는 것이 좋습니다.

<?php 
    $send   = "[email protected]"; 
    $subject  = "Civitas Message"; 
    $details  = array (
     "first"  => @$_POST["first"], 
     "surname" => @$_POST["surname"], 
     "email"  => @$_POST["email"], 
     "message" => @$_POST["message"] 
    ); 
    foreach ($details as $index => $value) { 
     $details[$index] = trim (stripslashes ($value)); 
    } 
    if (@$_POST["type"] == "register") { 
     // Whatever happens when they select "register" 
    } else if (@$_POST["type"] == "msgs") { 
     $header  = "From 'Civitas website' <[email protected]>"; 
     $body  = $details["first"] . " " . $details["surname"] . " " 
        . "<" . $details["email"] . ">" 
        . "Message: \n" 
        . $details["message"]; 

     $mail  = mail ($send, $subject, $body, $header); 
     if ($mail) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
?> 

또한 코드가 내 눈을 아프므로 다시 작성했습니다.

+0

Brilliant는 매력처럼 작동합니다! 고마워요. – user812493

+0

문제 없어요. 투표를하고 받아 들일 것을 잊지 마세요. D – escproxy

0

당신은이 작업을 수행 할 수 있습니다 변수가 존재하는 경우 네는 true을 반환하는 경우

$adminBody .= "First Name: "; 
$adminBody .= $firstName; 
$adminBody .= "\n"; 
$adminBody .= "Surname: "; 
$adminBody .= $Surname; 
$adminBody .= "\n"; 
$adminBody .= "Email: "; 
$adminBody .= $Email; 
$adminBody .= "\n"; 
if (isset($_POST['message'])) { 
    $adminBody .= "Message: "; 
    $adminBody .= $Message; 
} 
$adminBody .= "\n"; 

isset()는 확인합니다.

관련 문제