2012-07-30 10 views
0

我有一個複選框的問題,讓它工作。 如果沒有選擇任何複選框,它會給出一個錯誤消息,這工作正常! 也提交所有其他信息。不能讓數組在表單中工作php

我只是想現在的表單提交已經檢查到確認郵件的複選框。我現在只能得到:array。

的contact.php

<?php 
/* Set e-mail recipient */ 
$myemail = "[email protected]"; 

/* Check all form inputs using check_input function */ 
$yourname = check_input($_POST['yourname'], "Vul uw naam in aub"); 

$email = check_input($_POST['email']); 
$telephone = check_input($_POST['telephone']); 
$comments = check_input($_POST['comments'], "Write your comments"); 

$formCampagne = check_input($_POST['formCampagne']); 
foreach($formCampagne as $option) { 
    print $option."\n"; 
} 

/* If e-mail is not valid show error message */ 
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) 
{ 
show_error("Dit e-mail adres is niet juist, voer een juist e-mailadres in."); 
} 
/* If telephone is not valid show error message */ 
if (!preg_match("/[0-9\(\)+.\- ]/s", $telephone)) 
{ 
show_error("Voer een juist telefoon nummer in"); 
} 
/* If verification code is not valid show error message */ 

if (strtolower($_POST['code']) != 'mycode') {die('Voer aub de juiste code in, in  hoofdletters.');} 

/* If no campaign mode is selected, show error message */ 
if(empty($formCampagne)) 
{ 
    show_error ("U heeft geen selectie gemaakt uit de campagne opties, selecteer minimaal een van de opties."); 
} 

/* Let's prepare the message for the e-mail */ 
$message = "Hi Rick, 

Je hebt weer een offerte aanvraag ontvangen voor Limburg Media! :) 

Name: $yourname 
E-mail: $email 
Telefoon: $telephone 

Offerte aanvraag? $formCampagne 

Comments: $comments 

End of message 
"; 

/* Send the message using mail() function */ 
mail($myemail, $subject, $message); 

/* Redirect visitor to the thank you page */ 
header('Location: thanks.htm'); 
exit(); 

/* Functions we used */ 
function check_input($data, $problem='') 
{ 
$data = trim($data); 
$data = stripslashes($data); 
$data = htmlspecialchars($data); 
if ($problem && strlen($data) == 0) 
{ 
    show_error($problem); 
} 
return $data; 
} 

function show_error($myError) 
{ 
?> 
<html> 
<body> 

<b>Gelieve de onderstaande foutmelding door te nemen om uw gegevens correct aan te leveren:</b><br /> 
<?php echo $myError; ?> 

</body> 
</html> 
<?php exit();} 
?> } 

的Contact.htm其中訪問者在他/她填寫表單:

<html> 
<body> 

<p>Benodigde velden zijn <b>vetgedrukt</b>.</p> 

<form action="contact.php" method="post"> 
<p><b>Uw naam:</b> <input type="text" name="yourname" /><br /> 
<b>E-mail:</b> <input type="text" name="email" /></p> 
<b>Telefoonnummer:</b> <input type="text" name="telephone" /></p> 

<p>Welk soort campagne wilt u informatie over ?<br /> 
<input type="checkbox" name="formCampagne[]" value="sandwichborden driehoeksborden" />sandwichborden/driehoeksborden<br /> 
<input type="checkbox" name="formCampagne[]" value="drukwerk banners" />drukwerk/banners<br /> 
<input type="checkbox" name="formCampagne[]" value="evenementen outdoor" />outdoor promotie/evenemente<br /> 
<input type="checkbox" name="formCampagne[]" value="internet website social media" />internet/websites/social media <br /> 
<input type="checkbox" name="formCampagne[]" value="artwork video" />artwork/videopromotie <br /> 
<input type="checkbox" name="formCampagne[]" value="promo gadgets sampling" />promoteams/gadgets/sampling <br /> 
<input type="checkbox" name="formCampagne[]" value="mobiele reclame reclame frames" /> mobiele reclame/reclame frames <br /> </p> 


<p><b>Your comments:</b><br /> 
<textarea name="comments" rows="10" cols="40"></textarea></p> 
<p>Validatie code: <input type="text" name="code" /><br /> 
Vul de tekst <b>MYCODE</b> hierboven in. </p> 
<p><input type="submit" value="Send it!"></p> 


</form> 

</body> 
</html> 

的thanks.htm頁面只包含標準的文本,說,好。 .. 謝謝。 :)

任何人都可以幫我在這裏嗎?

編輯:

所以這將是正確的代碼:

if(isset($formCampagne)) { 
echo ".implode(',', $formCampagne)."; // this is the output in the confirmation mail 
} else { 
echo "U heeft geen selectie gemaakt uit de campagne opties, selecteer minimaal een van de opties."; 

但是,在我把它?在「Offerte aanvraag?」旁邊,因爲我已經在$ message字段中,所以會出現錯誤。 對不起,我還沒有使用isset函數。

回答

0

嘗試更換

$message = "Hi Rick, 

Je hebt weer een offerte aanvraag ontvangen voor Limburg Media! :) 

Name: $yourname 
E-mail: $email 
Telefoon: $telephone 

Offerte aanvraag? $formCampagne 

Comments: $comments 

End of message 
"; 

$message = "Hi Rick, 

Je hebt weer een offerte aanvraag ontvangen voor Limburg Media! :) 

Name: $yourname 
E-mail: $email 
Telefoon: $telephone 

Offerte aanvraag? ".implode(',', $formCampagne)." 

Comments: $comments 

End of message 
"; 

請注意,我已經改變了普通$formCampagneimplode(',', $formCampagne),「事業,它實際上是一個數組,你必須做出它是一個字符串,如果你想把它放在你的電子郵件中。

還要注意,如果沒有複選框被選中,$formCampagneNULL,所以你也應該檢查是否isset($formCampagne)

編輯:

在我的例子

$formCampagne實際上應該是$_POST['formCampagne'],我必須承認我從來沒有使用過check_input()函數,也不知道它在陣列上的表現如何。

你也可以嘗試var_dump()兩個$formCampagne$_POST['formCampagne']

+0

如果我嘗試此操作,則郵件不包含任何內容。即使選擇了項目,它也會返回空白。 – Mukie 2012-07-30 13:08:22

+0

剛編輯我的答案更清晰 – 2012-07-30 14:16:24

0

如果返回false,您可以檢查isset($ formCampagne),這意味着沒有選中該複選框,否則它會爲您提供數組值。相反$ formCampagne的

+0

我已經試過這個內容,請參閱編輯部分。但我不知道該把它放在哪裏。之前從未使用過此功能。 – Mukie 2012-07-30 13:08:58

0
$message = "Hi Rick, 

Je hebt weer een offerte aanvraag ontvangen voor Limburg Media! :) 

Name: $yourname 
E-mail: $email 
Telefoon: $telephone 

Offerte aanvraag? $formCampagne 

Comments: $comments 

End of message 
"; 

,你應該嘗試這樣的事:

$message = "Hi Rick, 

Je hebt weer een offerte aanvraag ontvangen voor Limburg Media! :) 

Name: $yourname 
E-mail: $email 
Telefoon: $telephone 

Offerte aanvraag? ".implode(', ', $formCampagne)." 

Comments: $comments 

End of message 
"; 

這應該給所有用逗號分隔的選擇複選框值的列表。

+0

如果我嘗試這個,郵件什麼也沒有。即使選擇了項目,它也會返回空白。 – Mukie 2012-07-30 13:09:14