0

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"])."\n";
$email_message .= "Music: ".clean_string($_POST["music"])."\n";

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 Answer 1

1

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"])."\n"
                            .clean_string($_POST["sport2"])."\n"
                            .clean_string($_POST["sport3"])."\n";

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

Sign up to request clarification or add additional context in comments.

6 Comments

1.Thanks for answering 2.Please check edit. 3.Dont know much about PHP that's why I just copied some of the PHP contact form code and I'm trying to change it to work with my page
Okay I need just a little bit more clarification. There is a HTML form with checkboxes, and when the form is submitted, the HTML values are processed into a mail handler and emailed?
.I think so.. yes. The form head is: <form name="htmlform" method="post" action="email.php"> If you want to see it yourself www.2know.pl/formularz.html The HTML and CSS are public
Webistes looking good! Well done. HTML isn't much use right now though, it's the PHP. I know what's going on now though and can give a better example above.
.Don't get scared of the mess there in code. it's my first website
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.