Multiple PHP email message function. (checkboxes) - 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"])."\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>

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

Related

How to rank (prioritize) values from numbered checkbox form in PHP

I am using the code below to ask users to rank which programming language they are more comfortable with.
The users need to rank from 1-3 (1 being the one they are most comfortable with)
<form id="form1" name="form1" method="post" action="">
<input type="number" name="php" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="python" required="required" max="3" min="1"/>Python <br />
<input type="number" name="ruby" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
Once the user prioritizes the programming languages and hits submit, how can I on the next page echo the ranking selection? (e.g. Your first choice is x, your second choice is y and your third choice is z)
I would do it like so (Note that I've changed the the value of the name attributes on the form elements):
<form id="form1" name="form1" method="post" action="">
<input type="number" name="lang[php]" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="lang[python]" required="required" max="3" min="1"/>Python <br />
<input type="number" name="lang[ruby]" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
And in the php:
//Get the form results (which has been converted to an associative array) from the $_POST super global
$langs = $_POST['lang'];
//Sort the values by rank and keep the key associations.
asort($langs, SORT_NUMERIC );
//Loop over the array in rank order to print out the values.
foreach($langs as $lang => $rank)
{
//echo out here first, second, and third rank with each iteration respectively.
}
The asort function simply sorts the array by value while maintaining key association.
I am not sure that tag input type="number" exists.
you do better
<legend>
<label><input type="radio" name="php" value="1">1</label>
<label><input type="radio" name="php" value="2">2</label>
<label><input type="radio" name="php" value="3">3</label>
</legend>
<legend>
<label><input type="radio" name="python" value="1">1</label>
<label><input type="radio" name="python" value="2">2</label>
<label><input type="radio" name="python" value="3">3</label>
</legend>
you must not use 'required' attribute for radio tag or checkbox tag
so you make a check javascript function whether radio box is checked or not.
<form name..... onsubmit = "return check_submit();">
<script>
var check_submit = function(){
if($("input[name=php]:checked").val() =="")
return false;
...
return true;
}
</script>
or you can use
<input type="text" name="php">
then on next page you can do like this
$php = intval(trim($_POST['php']));
$python = intval(trim($_POST['python']));
$msg = "your first choice for php is '.$php;
$msg.="your second choice for phthon is '.$python;
.....etc..

Form Check boxes not coming through

I can not get the form check boxes to come through to the email for "recycling". I do not know php so I have no idea what is wrong. When the form comes through to email the "Recycling:" subject is there, but the checked boxes are not.
HTML:
<label>CRT Monitors <input name="recycleobject2[]" type="checkbox" value="crtmonitors"
/></label>
<label>Printers <input name="recycleobject2[]" type="checkbox" value="printers"
/></label>
<label>Computers <input name="recycleobject2[]" type="checkbox" value="computers"
/></label>
<label>Fluorescent Lamps and Batteries <input name="recycleobject2[]" type="checkbox"
value="lamps" /></label>
<label>Televisions <input name="recycleobject2[]" type="checkbox" value="television"
/></label>
<label>Other Equipment <input name="recycleobject2[]" type="checkbox" value="other" />
</label><br />
PHP:
$recycleobject2 = $_POST['recycleobject2']; // not required
$recycleobject2 = array();
$email_message .= "Recycling:" .implode(", ",$recycleobject2)."\n";
your problem is right here:
$recycleobject2 = array();
That line is resetting the $recycleobject2 variable to a brand new empty array. Remove that line and your code should be fine.

Select all checkbox with jQuery form POST for multiple checkboxes

For a project I want to implement a nice filtering mechanism with multiple checkboxes. I get the checkboxes to work correctly as well as a jQuery function to automatically POST the form when checking a checkbox.
Now I want to add a "select all" checkbox above the checkboxes, but I cannot seem to find the correct way. I have tried a dozen solutions for (somewhat) similar questions but I cannot get it to work correctly and consistent.
The HTML part is something like this:
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<input type="checkbox" /> Select All colors<br/>
<label> <input type="checkbox" name="color[]" value="yellow"> Yellow</label><br/>
<label> <input type="checkbox" name="color[]" value="blue"> Blue</label><br/>
<label> <input type="checkbox" name="color[]" value="red"> Red</label><br/>
<label> <input type="checkbox" name="color[]" value="green"> Green</label><br/><br/>
<input type="checkbox" /> Select All brands<br/>
<label> <input type="checkbox" name="brand[]" value="Nike"> Nike</label><br/>
<label> <input type="checkbox" name="brand[]" value="Adidas"> Adidas</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeBrand"> SomeBrand</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeOtherBrand"> SomeOtherBrand</label><br/>
</form>
The jQuery part I use to post the form on each click on the checkbox (is not sufficient):
$('input:checkbox:').live('click',function() {
$(this).closest('form').submit();
});
My question now is what do I need for the jQuery part to make sure this works correctly?
I want to be able to click the label to deselect all from that group and select only that one checkbox. It also needs to POST the form for the array values. And lastly if all checkboxes are checked manually the "select all" one has to be checked as well.
Hopefully someone can help me out as I am stuck with this for a long time...
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#colorall").click(function()
{
var checked_status = this.checked;
$(".color").each(function()
{
this.checked = checked_status;
});
});
$(".color").click(function(){
if($(".color").length == $(".color:checked").length) {
document.getElementById("colorall").checked = true;
} else {
$("#colorall").removeAttr("checked");
}
});
$("#brandall").click(function()
{
var checked_status = this.checked;
$(".brand").each(function()
{
this.checked = checked_status;
});
});
$(".brand").click(function(){
if($(".brand").length == $(".brand:checked").length) {
document.getElementById("brandall").checked = true;
} else {
$("#brandall").removeAttr("checked");
}
});
});
</script>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<input type="checkbox" id="colorall" /> Select All colors<br/>
<label> <input type="checkbox" name="color[]" value="yellow" class="color"> Yellow</label><br/>
<label> <input type="checkbox" name="color[]" value="blue" class="color"> Blue</label><br/>
<label> <input type="checkbox" name="color[]" value="red" class="color"> Red</label><br/>
<label> <input type="checkbox" name="color[]" value="green" class="color"> Green</label><br/><br/>
<input type="checkbox" id="brandall"/> Select All brands<br/>
<label> <input type="checkbox" name="brand[]" value="Nike" class="brand"> Nike</label><br/>
<label> <input type="checkbox" name="brand[]" value="Adidas" class="brand"> Adidas</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeBrand" class="brand"> SomeBrand</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeOtherBrand" class="brand"> SomeOtherBrand</label><br/>
</form>

Different checkboxes send the user to different pages when checked

I need a form in which the checkboxes would open different pages based on their selection when the form is submitted.
So, say I have this simple form:
<form action="" method="post">
<input value="1" type="checkbox" name="sign" />
<input value="2" type="checkbox" name="sign" />
<input value="3" type="checkbox" name="sign" />
<input value="4" type="checkbox" name="sign" />
<input value="5" type="checkbox" name="sign" />
<input type="submit" />
</form>
When an user checks value 1 and submits, he will be redirected to page A. If a user checks 1 and 4 he will be redirected to a different page (page F, for instance). If a user checks 2, 3 and 4 he will be redirected to page R, and so on... There would be 25 different combinations, and therefore, 25 different page results for this form when an user submits it.
In other words, when the form is submitted somehow the system would read which checkboxes were checked and associate each possible combination with a different URL.
Can it be done? If so, how? Anyone ever made something similar? I've searched a long time for solutions, but found only slightly similar ones, not exactly what I need, so any help would be appreciated.
HTML:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[1]" />
<input value="2" type="checkbox" name="sign[2]" />
<input value="3" type="checkbox" name="sign[3]" />
<input value="4" type="checkbox" name="sign[4]" />
<input value="5" type="checkbox" name="sign[5]" />
<input type="submit" />
</form>
PHP:
if (isset($_POST['sign'][1]))
header("Location: a.php");
elseif(isset($_POST['sign'][2]) AND isset($_POST['sign'][3]))
header("Location: b.php");
This can be done in a couple of ways. One is that you can use javascript to intercept the form submission, change the value of the form "action", and then execute the submit.
A second approach might be to just send all this data to a single script and based on the selected values perform a redirect to the intended page.
From should look like:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
And the post like:
if(!empty($_POST['sign'])) {
if(in_array(1, $_POST['sign'])) {
// if just 1, go to page
} elseif(in_array(1, $_POST['sign']) && in_array(4, $_POST['sign'])) {
// if 1 and 4, go to page
}
}
You can just consider each of the inputs as a bit. So, you will always have 5 bits to consider.
Then define an associative array of 25 entries corresponding to each of the possible values:
00001
00010
...
var links = {
"00001" : "www.google.com",
];
Then, when you submit the form, just set the target attribute of the form based on the value.
If I were you and I had no choice than following this idea, I would use something like that in jQuery (assuming your post vars are treated in the page you want to reach):
<form action="" method="post" id="myForm">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
$("#myForm").submit(function() {
var checked_array = new Array();
$("#myForm input").each(function() {
if ($(this).is(":checked")
checked_array.push($(this).attr("value"));
});
if ( checked_array.indexOf(2) !== -1 && checked_array.indexOf(5) !== -1)
("#myForm").attr("action", "/url1.php") ;
else if etc...
});

How to handle multiple checkboxes in a PHP form?

I have multiple checkboxes on my form:
<input type="checkbox" name="animal" value="Cat" />
<input type="checkbox" name="animal" value="Dog" />
<input type="checkbox" name="animal" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['submit']) {
echo $_POST['animal'];
}
I get "Bear", i.e. the last chosen checkbox value even though I picked all three. How to get all 3?
See the changes I have made in the name:
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
you have to set it up as array.
print_r($_POST['animal']);
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['animal'])){
foreach($_POST['animal'] as $animal){
echo $animal;
}
}
use square brackets following the field name
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
On the PHP side, you can treat it like any other array.

Categories