Send Checkbox Value in PHP Form

send checkbox value in PHP form

Here's how it should look like in order to return a simple Yes when it's checked.

<input type="checkbox" id="newsletter" name="newsletter" value="Yes" checked>
<label for="newsletter">i want to sign up for newsletter</label>

I also added the text as a label, it means you can click the text as well to check the box. Small but, personally I hate when sites make me aim my mouse at this tiny little check box.

When the form is submitted if the check box is checked $_POST['newsletter'] will equal Yes. Just how you are checking to see if $_POST['name'],$_POST['email'], and $_POST['tel'] are empty you could do the same.

Here is an example of how you would add this into your email on the php side:

Underneath your existing code:

$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['tel'];

Add:

$newsletter = $_POST['newsletter'];
if ($newsletter != 'Yes') {
$newsletter = 'No';
}

If the check box is checked it will add Yes in your email if it was not checked it will add No.

How to send checkbox values in contact form using php?

Change this

$check_msg .= "Checked: $value\n";

To

$check_msg .= "Checked: $value\\n";

Because otherwise in javascript it will throw error, saying string literal is not closed.

Also make the alert like following

echo "<script> alert('$check_msg'); </script>;";

Getting checkbox values on submit

A good method which is a favorite of mine and for many I'm sure, is to make use of foreach which will output each color you chose, and appear on screen one underneath each other.

When it comes to using checkboxes, you kind of do not have a choice but to use foreach, and that's why you only get one value returned from your array.

Here is an example using $_GET. You can however use $_POST and would need to make both directives match in both files in order to work properly.

###HTML FORM

<form action="third.php" method="get">
Red<input type="checkbox" name="color[]" value="red">
Green<input type="checkbox" name="color[]" value="green">
Blue<input type="checkbox" name="color[]" value="blue">
Cyan<input type="checkbox" name="color[]" value="cyan">
Magenta<input type="checkbox" name="color[]" value="Magenta">
Yellow<input type="checkbox" name="color[]" value="yellow">
Black<input type="checkbox" name="color[]" value="black">
<input type="submit" value="submit">
</form>

###PHP (using $_GET) using third.php as your handler

<?php

$name = $_GET['color'];

// optional
// echo "You chose the following color(s): <br>";

foreach ($name as $color){
echo $color."<br />";
}

?>

Assuming having chosen red, green, blue and cyan as colors, will appear like this:

red

green

blue

cyan


##OPTION #2

You can also check if a color was chosen. If none are chosen, then a seperate message will appear.

<?php

$name = $_GET['color'];

if (isset($_GET['color'])) {
echo "You chose the following color(s): <br>";

foreach ($name as $color){
echo $color."<br />";
}
} else {
echo "You did not choose a color.";
}

?>

##Additional options:
To appear as a list: (<ul></ul> can be replaced by <ol></ol>)

<?php

$name = $_GET['color'];

if (isset($_GET['color'])) {
echo "You chose the following color(s): <br>";
echo "<ul>";
foreach ($name as $color){
echo "<li>" .$color."</li>";
}
echo "</ul>";
} else {
echo "You did not choose a color.";
}

?>

Send checkbox value to MySQL database without using $_POST or $_GET

If you are looking for ajax that sends a request to the backend server without reloading the page.

$(document).on('click', '#gestao, #risco, #TOC', function(){
var gestao = $('#gestao').is(':checked') ? $('#gestao').val() : false;
var risco= $('#risco').is(':checked') ? $('#risco').val() : false;
var TOC= $('#TOC').is(':checked') ? $('#TOC').val() : false;

$.post(URL, {
gestao : gestao,
risco : risco,
TOC : TOC
}, function(response){
//Response from backend server
})
});

For more reference, Check https://api.jquery.com/jquery.post

Send checkbox value to mail

You are somewere using $variable_name instead of $variable_name["index"] for an associative or $variable_name[0] for an numeric array. Thats were the error comes from.

how to send by mail values of multiple checkboxes with php

The problem is you just store on $preferences the last value, so you could useimplode on $selected_checkbox and get a list comma-separated so:

$body .= "Preferences: ";
$body .= implode(', ', $selected_checkbox);
$body .= "\n";


Related Topics



Leave a reply



Submit