2012-08-16 2 views
0

연락처 양식을 설정하고 전자 메일 계정에 응답을 전자 메일로 보내도록 설정했습니다. 폼의 일부는 일련의 체크 박스이며, 이메일로 목록에 표시하려면이 확인란을 선택해야합니다. 아래 코드는 체크 상자의 값 대신에 'Array'를 반환합니다. 어떤 제안?PHP를 통해 HTML 양식의 여러 확인란 응답을 보내는 방법은 무엇입니까?

는 HTML :

<?php 
    if (isset($_POST['service'])) { 
    $service = $_POST['service']; 
    // $service is an array of selected values 
} 
$formcontent= "From: $name \n Service(s) required: $service \n"; 
$recipient = "[email protected]"; 
$subject = "You have a new message from $name"; 
$mailheader = "From: $email \r\n"; 
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); 
echo "Thank You! We will get back to you as soon as we can."; 
?> 

감사합니다,

제이슨

+1

foreach를 사용하여 서비스 배열을 통과해야합니다. –

답변

4

당신은에 가입해야합니다 (예를 들어 ','와 내파) 배열 요소 : 여기

<h3>Service required:</h3> 
<input type="text" id="name" name="name" placeholder="Name" required /> 
<input type="email" id="email" name="email" placeholder="Email" required /> 
<input class="check-box styled" type="checkbox" name="service[]" value="Service/repairs" /><label> Service/repairs</label> 
<input class="check-box styled" type="checkbox" name="service[]" value="MOT" /><label> MOT</label> 
<input class="check-box styled" type="checkbox" name="service[]" value="Cars for sale" /><label> Cars for sale</label> 

는 PHP의 문자열로.

<?php 
$formcontent= "From: $name \n Service(s) required: ".implode(", " ,$service)." \n"; 
?> 
+0

감사합니다. 완벽하게 작동했습니다. – jasonbradberry

1

원하는 결과를 문자열로 가져 오지 않는 이유는 무엇입니까? 그런 다음 쉼표의 출력을 얻을 것이다

if (isset($_POST['service'])) { 
    $service = $_POST['service']; 
    // $service is an array of selected values 
    $service_string = ""; 
    for($i=0;$i<count($service);$i++) 
    { 
     if($i!=0) 
     { 
      $service_string = $service_string . ", "; 
     } 
     $service_string = $service_string . $service[$i]; 
    } 
} 

는 service_string $ 각 쳤다 항목의 목록을 분리했다.

1

$_POST['service']에 여러 개의 체크 박스가 저장되어 있기 때문에 어레이 자체이며 2 차원으로되어 있습니다. 다른 색인은 $_POST['service'][0]과 같이 액세스 할 수 있습니다.

$_POST['service'] 뭔가를하려면, 당신은 모든 인덱스를 통해 루프 foreach 문을 사용할 수 있습니다
foreach($_POST['service'] as $post){ 
    //Do stuff here 
} 

는 다른 방법으로, 단순히 모든 인덱스를 연결하는 implode()를 사용합니다.

0

입력 유형 checkbix에는 고유 한 이름이 있어야합니다. 그렇지 않으면 $ _POST에 마지막 체크 박스가 있습니다. 또는 위에 설명 된대로 반복 할 수 있습니다. 이메일 html 형식을 만들고 $ formcontent에 html 문자열을 작성하십시오. 예 :

$formcontent = "<html><head></head><body>"; 
$formcontent .= "<ul><li>".$_POST["checkbox1"]."</li>"; 
$formcontent .= "<li>".$_POST["checkbox2"]."</li>"; 
$formcontent .= "</ul></body></html>"; 

이메일을 HTML 형식으로 작성하려면 이메일 웹 사이트 기능을 참조하십시오.

관련 문제