【问题标题】:Can't get the array to work in form php无法让数组以 php 形式工作
【发布时间】:2012-07-30 12:09:30
【问题描述】:

我对复选框有疑问,无法使其正常工作。 如果没有选中任何复选框,则会给出错误消息,这很好用! 还要提交所有其他信息。

我现在只想让表单提交已签入确认电子邮件的复选框。我现在只得到:数组。

contact.php

<?php
/* Set e-mail recipient */
$myemail  = "mukies@gmail.com";

/* 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 函数。

【问题讨论】:

    标签: php arrays forms checkbox confirmation


    【解决方案1】:

    尝试替换

    $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
    ";
    

    请注意,我已将普通的 $formCampagne 更改为 implode(',', $formCampagne),因为它实际上是一个数组,如果您需要将其设为字符串想把它放在你的电子邮件中。

    还要注意,如果没有选中任何复选框,$formCampagne 将是NULL,因此您还应该检查是否isset($formCampagne)

    编辑:

    在我的示例中,$formCampagne 实际上应该是 $_POST['formCampagne'],我必须承认我从未使用过 check_input() 函数,也不知道它在数组上的行为方式。

    你也可以尝试var_dump()$formCampagne$_POST['formCampagne']的内容

    【讨论】:

    • 如果我试试这个,邮件什么也没有。即使选择了项目,它也会返回空。
    【解决方案2】:

    如果返回 false,您可以检查 isset($formCampagn),这意味着未选中任何复选框,否则它将为您提供值数组。

    【讨论】:

    • 我试过这个,见编辑部分。但我不知道把它放在哪里。以前从未使用过此功能。
    【解决方案3】:
    $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
    ";
    

    您应该尝试以下方法,而不是 $formCampagn:

    $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
    ";
    

    这应该会列出所有选中的复选框值,用逗号分隔。

    【讨论】:

    • 如果我试试这个,邮件什么也没有。即使选择了项目,它也会返回空
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 2011-04-26
    • 2015-11-24
    相关资源
    最近更新 更多