douren1891
2013-12-17 22:09 阅读 63
已采纳

多个PHP电子邮件功能。 (复选框)

What should I type in PHP instead of I got now to receive in mail all of the answers typed in checkboxes by user. Now I have only one. Thanks

PHP:

$email_message .= "Sport: ".clean_string($_POST["sport"])."
";
$email_message .= "Music: ".clean_string($_POST["music"])."
";

HTML:

<div id="Oobj58" class="Oobj">
    <input type="checkbox" name="sport" value="Kosz" />koszykówka<br>
    <input type="checkbox" name="sport" value="Zimowe" />sporty zimowe<br>
    <input type="checkbox" name="sport" value="Konie" />jeździectwo konne<br>

</div>

    <div id="Oobj61" class="Oobj">

    <input type="checkbox" name="rozrywka" value="festiwal" />festiwale<br />
    <input type="checkbox" name="rozrywka" value="wesole" />wesołe miasteczka<br />
    <input type="checkbox" name="rozrywka" value="zespolowo"/>paintball,bilard,kręgle..<br/>
    </div>

Edit: Thanks but not exactly can type different 'names' cause I also have one checkboxe to set all on check at once and it works with JS like this:

   <div id="Oobj61" class="Oobj">

<script language="JavaScript">
function toggle2(source) {
  checkboxes = document.getElementsByName('rozrywka');
  for(var i=0, n=checkboxes.length;i<n;i++) {
    checkboxes[i].checked = source.checked;
  }
}
</script>

<input type="checkbox" onClick="toggle2(this)" /><br>

<input type="checkbox" name="rozrywka" value="club" />kluby<br />
<input type="checkbox" name="rozrywka" value="puby" />puby<br />
<input type="checkbox" name="rozrywka" value="koncert" />koncerty<br />
<input type="checkbox" name="rozrywka" value="festiwal" />festiwale<br />
<input type="checkbox" name="rozrywka" value="wesole" />wesołe miasteczka<br />
<input type="checkbox" name="rozrywka" value="zespolowo" />paintball,bilard,kręgle...<br 

/>
</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doutang3077 doutang3077 2013-12-17 22:14

    I'm not 100% sure what it is you're asking here, but if you'd like to see if a checkbox is set you can do the following:

    if(isset($_POST['checkbox-name-here'])){
       //it's set code here, maybe set a variable...
       $var = "checkbox value";}
    

    That's a rough example, but you should get the drift.

    If you're trying to get multiples - then give each checkbox a unique name:

    <div id="Oobj58" class="Oobj">
        <input type="checkbox" name="sport1" value="Kosz" />koszykówka<br>
        <input type="checkbox" name="sport2" value="Zimowe" />sporty zimowe<br>
        <input type="checkbox" name="sport3" value="Konie" />jeździectwo konne<br>
    </div>
    

    Then in PHP you can check if each value is set:

    if(isset($_POST['sport1'])){$sport1="sport1";}
    if(isset($_POST['sport2'])){$sport2="sport2";}
    if(isset($_POST['sport3'])){$sport3="sport3";}
    
    $email_message .= "Sports: ".clean_string($_POST["sport1"])."
    "
                                .clean_string($_POST["sport2"])."
    "
                                .clean_string($_POST["sport3"])."
    ";
    

    There are much tidier ways of doing it, but that should help you work it out?

    Alternatively you could give each of the checkboxes the same name, and then construct an array out of them. This array can then be looped in the PHP email handler. Look at this SO answer for a great example: https://stackoverflow.com/a/4516887/3112128

    点赞 评论 复制链接分享

相关推荐