Jquery+PHP form - Multiple recipients based on checkbox selection - php

I'm asking this question in regards to my friend, so I do not have code samples to post here at the moment. Hopefully I'm clear enough that someone can help.
So he has a simple contact form except it has multiple checkboxes that the user can choose to send their requests to multiple recipients... like so...
x I would like to know about flight school
x I'm interested in becoming a teacher
x I would like someone to contact me about your degrees
Name
Email
Comments
And so based on which checkboxes are selected it should add that recipient to the email function so that they receive the users comments and interest.
The form is validated by jquery and uses the $.ajax function to POST the Name, Email and Comments fields over to a process.php... we are validating that at least one of the checkboxes is selected, however, we haven't been able to figure out how to pass its boolean value to the process.php and in-turn add the relevant email address to the mail() function.
I do realize this is semi-vague without posting our code, but I don't have access to it right now... and I have been searching google for about 30 minutes trying to find something to work with. Any help would be appreciated. Thanks.

you can simply check if the value you got is true or not:
basic idea :
if(checkbox-1-ischecked)
//send email to first recipent
end if
if(checkbox-2-ischecked)
//send email to 2nd recipent
end if
if(checkbox-3-ischecked)
//send email to 3rd recipent
end if
if(checkbox-4-ischecked)
//send email to 4th recipent
end if
Etc

This would seem to answer you check box query. (http://stackoverflow.com/questions/908708/how-to-pass-multiple-checkboxes-using-jquery-ajax-post)
In basic terms it would post an array back to the php script which you could then parse and depending what was ticked / the vars passed back you could then append more email addresses to the 'to' part of the mail function.
For a simplier implimentation you could just keep your three check boxs seperate not in an array and ajax post them back individually.
HTML
<input type='checkbox' name='flight' value='1' id='flight' />
<input type='checkbox' name='teacher' value='1' id='teacher' />
Then simply on the server via PHP
$to="";
if($_POST['teacher'] == 1) {$to = $to."joe#email.com,"};//append email
if($_POST['flight'] == 1) {$to = $to."bob#email.com,"};//append email
$to = rtrim($to, ","); //remove trailing comma
NOTE as with all web to mail scripts make sure you sanitize all vars to prevent spam abuse!

Name your elements as an array like this:
<input type="checkbox" name="mybox[]" value="foo#example.com">Foo</input>
<input type="checkbox" name="mybox[]" value="bar#example.com">Bar</input>
<input type="checkbox" name="mybox[]" value="hello#example.com">Hello</input>
<input type="checkbox" name="mybox[]" value="world#example.com">World</input>
After POSTing the form to your PHP, $_POST['mybox'] will be an array holding the values of the boxes checked.
In your PHP
if(isset($_POST['my_box']))
{
$subject = "sub";
$body = "body";
if (is_array($_POST['mybox']))
{
//multiple items were selected.
$to = implode(',',$_POST['my_box']);
mail($to,$subject,$body);
}
else //only one item was selected
{
echo $_POST['my_box'];
$to = $_POST['my_box'];
mail($to,$subject,$body);
}
}
else
//none were selected

You can simply assign the same name to all checkboxes, which actually results in a checkbox array.
<form name="someform" onsubmit="return validate(this)" action="process.php" method="post">
<input type="checkbox" name="names[]" value="Saji">Saji
<input type="checkbox" name="names[]" value="Muhaimin">Muhaimin
<input type="checkbox" name="names[]" value='Muhsin'>Muhsin
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
In process.php, you could have-
$name_val=$_POST['names'];
foreach($name_val as $values){
//Here $values will contain only the values of the checkboxes you had selected.
echo $values."<br />";
}

Related

$_POST for checkbox

I have this form and its details need to be send in an email. As of now I have only managed to do the other fields to be included in the email but I am having a trouble with this multiple checkboxes. I have lots of checkboxes, 23 to be exact. Now, the problem I am encountering is, how will I include the value of the checkbox if one checkbox is checked or just two or three checkboxes are checked.
HTML
<input type="text" name="state" id="txt1">State:</input>
<input type="text" name="name" id="txt2">Name:</input>
Package A<input type="checkbox" name="radiog_dark" value="150.00" id="tcbx3" class="css-checkbox"/>
Package B<input type="checkbox" name="radiog_dark" value="175.00" id="tcbx3" class="css-checkbox"/>
Package C<input type="checkbox" name="radiog_dark" value="200.00" id="tcbx3" class="css-checkbox"/>
PHP
$state = $_POST['state'];
$entity = $_POST['entity'];
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Entity: ".clean_string($entity)."\n";
Use isset() method to check for the checkbox is checked or not
if(isset($_POST['radiog_dark'])){
// do something
}
Further more you cant have same names for two checkboxes as you will get only one, other 2 will over that one
Use Array Syntax to set your checkbox field like this way and check your checkbox value using isset, you can set the array index like this way to more readablity e.g
name="radiog_dark['package_a']"
name="radiog_dark['package_b']"
<input type="checkbox" name="radiog_dark[]" value="150.00" id="tcbx3" class="css-checkbox"/>
if(isset($_POST['radiog_dark'])){
// do other stuffs
}
change chekbox name radiog_dark to radiog_dark[]
Package A<input type="checkbox" name="radiog_dark[]" value="150.00" id="tcbx3" class="css-checkbox"/>
if(!empty($_POST['radiog_dark'])){
// Loop to store display values of individual checked checkbox.
foreach($_POST['radiog_dark'] as $selected){
echo $selected."</br>";
}

Parse radio button postback data using PHP

I have a question related to php. If anyone have an idea,please share with me. (I am a beginner in php).
I received a button value from an HTML page and displayed corresponding figure in the second page using code "param1.php". In the same program itself ( ie, "param1.php"), there is a set of radio box and i need to receive the radio value using another php program. But here i have confusion how to receive the radio value using another php program say "param2.php". also how the page redirect (to the php program where i receivvee the radio value) on selecting a radio button.
Thanks
I attach the "param1.php" below,
<!DOCTYPE html>
<html>
<body>
<br>
<?php
$data = $_POST['btn'];
$data2=$data.".png";
?>
<img src="<?php echo $data2;?>">
<h3>SCALE</h3>
<input type="radio" name="group1" value="5,10,15" checked> 5,10,15<br>
<input type="radio" name="group1" value="5,9,13"> 5,9,13<br>
</body>
</html>
No form tags
There are no form tags, so your radio tags won't get POST'd to param2.php
<form action="param2.php" method="POST">
<input type="radio" name="group1" value="5,10,15" checked> 5,10,15<br>
<input type="radio" name="group1" value="5,9,13"> 5,9,13<br>
<input type="submit" value="Process" />
</form>
Incorrect $_POST key
Now, in your param2.php file, you will be able to get the value of POST['group1']. In your code you're using the wrong key. btn doesn't exist within $_POST (in the code you've given anyway). The radio button name is group1, so access it as such;
$data = $_POST['group1'];
The value of $data will either be 4,10,15 or 5,9,13. Ensure you've got an image named 4,9,13.png and 4,10,15.png else your image won't show. Because they're comma separated, I'm going to assume these are unique file names. So;
foreach( explode(",", $_POST['group1']) as $file) {
echo "<img src='". $file .".png' />";
}
Also, ensure you do some checks on the posted data to validate the user input

Checkbox Insert / Delete From Database

A little about my code:
I have code that dynamically displays emails with a checkbox next to each email.
<?
foreach($er as $row){ ?>
<input name="emailcheckbox" id="emailcheckbox" type="checkbox" value="check" checked="checked">
<?
echo $row[email]."<br><br>";
echo "<input name='emailID' id='emailID' type='hidden' value='".$row[emailID]."' />";
} $emailquery->execute(); ?>
I can't seem to come up with a way that deletes the emailID of each email from a specific database table when you uncheck the checkbox. When you re-check the checkbox, I want to insert it back into the database table.
The emails won't go away, because they are stored in a completely different table than the one I want to insert/remove it from.
I know this is kind of a full question, so I will answer any questions you may have. Thank you in advance for all your help!
first, change your input to
<input name="emailcheckbox[]" value="<?php echo htmlspecialchars($row[email]);?>" ...
so, after you post this form back to server you will have
$_POST['emailcheckbox'] == array('checkedemail1', 'checkedemail2'...)
so you will need to delete all e-mails from your table and insert emails from this array, with this you will delete unchecked ones and save only checked ones
You can do this way:
<input name="emailcheckbox" id="emailcheckbox" type="checkbox" value="[tablename][email]" checked="checked">
Insert Page:
<?
foreach($_POST['emailcheckbox'] as $item)
{
$query = "INSERT INTO ".$item[0]." VALUES(".$item[1].")";
.....
}....
?>

php populate listbox dynamically without submitting form

I'll try to explain this as best as I can.
I have a form that accepts multiple fields, and in the end, e-mails all the fields to a specific e-mail address.
So for example, I have three text boxes, one list box and two submit buttons.
Two of the text boxes are first name, and e-mail address
The third text box is used to populate the list box. So if I enter, NIKE, into the third text box and push the first submit button. Nike will now be in the listbox.
I want to be able to populate the list box with as many entries as needed, then push the second submit button to send all information (first name, e-mail address and all items in list box).
The problem is, pushing the first submit button always triggers the e-mail sent, since I'm "POST"ing.
I have everything working right now. The third text box submits the new data to a table in mysql, and then retrieves all the data and puts it in the list box.
What's the best way to fix this scenario? Could I stop the Post variable from validating, until the second submit button is used?
Also, I'd like to avoid Javascript, thanks
Make sure the two submit buttons have names. I.E: <input type="submit" name="command" value="Add"> and <input type="submit" name="command" value="Send">. Then you can use PHP to determine which one was clicked:
if($_REQUEST['command'] == 'Add')
{
// Code to add the item to the list box here
}
elseif($_REQUEST['command'] == 'Send')
{
// Code to send the email here...
}
BONUS: For extra credit, make the commands variables so they can be easily changed, and map them to functions...
<?php
$commands = array(
'doSendEmail' => 'Send Email',
'doAddOption' => 'Add Option',
);
function doSendEmail()
{
// your email sending code here...
}
function doAddOption()
{
// your option adding code here...
}
function printForm()
{
global $commands;
?>
Name: <input type="text" name="name"><br>
Email: <input type="text" name="name"><br>
<input type="text" name="add">
<input type="submit" name="command" value="<?= $commands['doAddOption'] ?>">
<select>
<?php /* some code here */ ?>
</select>
<input type="submit" name="command" value="<?= $commands['doSendEmail'] ?>">
<?php
}
if(isset($_REQUEST['command']))
{
$function = array_search($_REQUEST['command'],$commands);
if($function !== -1)
call_user_func($function);
}

displaying all form field names but checkbox's and radios fields dont show

i have an html form full of text fields, checkbox's , and radio fields.
i wanted to get all the names of the fields so that i can get started in validating the information in them.
the method i am using to get them is
if(isset($_POST['submit'])) {
foreach($_POST as $name => $value) {
print $name."<br/>";
}
}
but i noticed that it only displays textbox and textarea field names and it doesnt include checkbox and radio field names through this submission. do i need to include anything for it to grab the field names of those?
Checkboxes and radio buttons work a little differently than your standard inputs. If a checkbox is present on a form that doesn't necessarily mean that it will be available in the resulting POST information. Rather, those values will only be avialable if they are actually marked (checkboxes checked and radio buttons selected). The proper way to test for their value in PHP is not to check the field value but rather to check isset() first.
For a checkbox:
$data['my_checkbox'] = isset($_POST['my_checkbox']) ? 'on' : 'off';
and for a radio button:
$data['my_radio'] = isset($_POST['my_radio']) ? $_POST['my_radio'] : false;
To be a little more descriptive let's say you have the following form:
<form action="test.php" method="post">
<input type="text" name="email" value="" />
<input type="checkbox" name="active" value="Yes" />
<input type="submit" value="Submit" />
</form>
If I were to submit that form with an email value of 'test#email.com' but not check the checkbox I would have the following in $_POST:
Array (
'email' => 'test#email.com'
)
However, if I were to submit the same form with the same email address and check the checkbox I would have the following:
Array (
'email' => 'test#email.com',
'active' => 'Yes'
)
Hope that helps.
0./ Try using the following code to see the raw posted data:
echo '<pre>';
print_r($_POST);
echo '</pre>';
1./ Make sure you use a name attribute value for your checkbox and radio inputs.
Typically for checkboxes, it will be an array.
<input type="checkbox" id"=fruit-apple" name="fruits[]" value="apple" />
<input type="checkbox" id="fruit-pear" name="fruits[]" value="pears" />
2./ Make sure they sit inside the form tag.
3./ If you submit using a javascript call, try disabling javascript and see if the error stays. If it does not, you know your javascript is the culprit.

Categories