make sure atleast one checkbox is selected php - php

I have a little mailing form with a few checkboxes. At least one of the boxes need to be selected before mailing should start.
My HTML:
<input type='checkbox' id='part1' name='box1' value='box1' checked>
<label for="part1">Voor Leden agenda</label>
<br/>
<input type='checkbox' id='part2' name='box2' value='box2' checked>
<label for="part2">Voor Leiding agenda</label>
<br/>
<input type='checkbox' id='part3' name='box3' value='box3' checked>
<label for="part3">Verhuur agenda</label>
<br/>
<button type='submit' name='send'/>send</button>
My PHP:
if (isset($_POST['box1'])) {
$box1 = 'yes';
} else {
$box1 = 'No';
}
if (isset($_POST['box2'])) {
$box2 = 'yes';
} else {
$box2 = 'No';
}
if (isset($_POST['box3'])) {
$box3 = 'yes';
} else {
$box3 = 'No';
}
i would like to have a script that gives a message like below if no checkbox is selected:
if()
{
echo "<p class='redfont'>no checkboxes are selected</p>";
echo "<p><a href='javascript:history.back();'>Click to go back</a></p>";
}
edit: how can I give this message with php, only if all boxes are unchecked

if(!isset($_POST['box1']) && !isset($_POST['box2']) && !isset($_POST['box3']))
{
// none is set
}
You could even apply De Morgan's law and write this equivalent expression
if(isset($_POST['box1']) || isset($_POST['box2']) || isset($_POST['box3']))
{
// at least one of them is set
}
You could even send those 3 parameters to 1 isset call but then that would check if all of them are set, which is not your requirement.

Try this:
if(isset($_POST["box1"]) || isset($_POST["box2"]) || isset($_POST["box3"])) {
if(isset($_POST['box1'])) {
$box1 = 'yes';
} else {
$box1 = 'No';
}
if(isset($_POST['box2'])) {
$box2 = 'yes';
} else {
$box2 = 'No';
}
if(isset($_POST['box3'])) {
$box3 = 'yes';
} else {
$box3 = 'No';
}
} else {
echo "<p class='redfont'>no checkboxes are selected</p>";
echo "<p><a href='javascript:history.back();'>Click to go back</a></p>";
}

This one is more readable I think:
$boxes = ['box1', 'box2', 'box3'];
$checked = [];
foreach($boxes as $box){
if(isset($_POST[$box])){
$checked[] = $box;
}
}
if(count($checked) == 0){
// no boxes checked
echo "<p class='redfont'>no checkboxes are selected</p>";
echo "<p><a href='javascript:history.back();'>Click to go back</a></p>";
}else{
// at least one box is checked, you can do another foreach statement
with the $checked variable to do stuff with the checked ones
}

To do this for ANY number of checkboxes (Webistes are bound to expand), I assume all of you checkboxes are of the form box**i** :
if( strpos( implode(',' , array_keys($test)) , 'box' ) !== FALSE )

I use this function in JQuery:
jQuery.validation = function(){
var verif = 0;
$(':checkbox[id=list_exp]').each(function() {
if(this.checked == true){
verif++
}
});
if(verif == 0){
alert("no checkboxes are selected");
return false;
}
}

Related

PHP if/else form

I am trying to create a form where if you fill in a number like 11 you get a response with a color type. I know that I have to change $tulp, but I don't know what to do.
<form name= "gemiddelden" action= "" method="post">
<p>Geelzweem (%): <input type="text" name="getal1"/></p>
<p><input type="submit" name="Bepaal" value="Bepaal"/></p>
</form>
$tulp = date("H");
if($tulp == 10) {
echo("Lichtgeel");
}
else if($tulp >10 && $tulp <20)
{
echo("Geel");
}
else if($tulp >20 && $tulp <30)
{
echo("Zon geel");
}
else if($tulp >30 && $tulp <40)
{
echo("Diep Geel");
}
else if($tulp >40 && $tulp <50)
{
echo("Zwavel Geel");
}
else if($tulp >50 && $tulp <60)
{
echo("Intens Geel");
}
else if($tulp >60 && $tulp <70)
{
echo("Verzadigd Geel");
}
else if($tulp >70)
{
echo("Dit mag niet");
}
else {
echo("Error");
According to google translate Geelzweem represent the shade of yellow. Then the echoed texts are different shades. I am guessing that you need to display a color based on the input in the getal1 field. In that case you need to change the line
$tulp = date("H");
to
if (isset($_POST["getal1"])) {
$tulp = $_POST["getal1"];
} else {
// some default value here
$tulp = 20;
}
Also, although the standard says that it is not mandatory, it is best practice to specify the action of the form
<form name= "gemiddelden" action= "SOME URL HERE" method="post">
Needed to change $tulp to $tulp = $_POST['getal1'];

Search filters not functioning on first attempt

I have a search filter based on flags of different colors.
If I will search based on color it is showing result but when I will select white color flag it is showing entire data in my database table. But If I choose any other color apart from white color for the first time it will show result. And if I choose white color flag second time,then it gives the correct result. What might be the reason. I need help
//filter on flag_color
if($this->input->get('flg')){
$arr_flag = explode('-', $this->input->get('flg'));
$str_flag = implode(',',$arr_flag);
if(6 == $str_flag){
$str_flag = 0;
}
$str_condition .= 'AND t.sender_flag_color_id IN('.$str_flag.')';
}
array(
'0'=>array(
'color'=>'White',
'path'=>'whiteflag.png'),
'1'=> array(
'color'=>'Blue',
'path'=>'blueflag.png'),
'2'=>array(
'color'=>'Green',
'path'=>'greenflag.png'),
'3'=>array(
'color'=>'Yellow',
'path'=>'yellowflag.png'),
'4'=>array(
'color'=>'Red',
'path'=>'redflag.png') ,
'5'=>array(
'color'=>'Orange',
'path'=>'orangeflag.png')
);
view
<?php
$arr_params = $this->uri->uri_to_assoc();
$arr_flag = array();
if(isset($arr_params['flg']))
{ ?>
<input type="hidden" id="flag_in_url" name="flag_in_url" value="yes" />
<?php
}
?>
<!-- id="webmenu1" -->
<select class="flag_color" onchange="urgency_select('flag_color')" name="header_flag" id="webmenuflag1">
<?php
$arr_params = $this->uri->uri_to_assoc();
$arr_flag = array();
$int_flag_url = '';
if(isset($arr_params['flg']))
{
$int_flag_url = $arr_params['flg'];
}
if(isset($arr_flag_color))
{
foreach($arr_flag_color as $key=>$flag_color)
{
if($int_flag_url == $key)
{
$flag_selected = 'selected';
}
else
{
$flag_selected = '';
}
?>
<option <?php echo $flag_selected;?> value="<?php echo $key;?>" data-image="<?php echo base_url();?>images/<?php echo $flag_color['path'];?>"></option>
<?php
}
}
?>
js: urgency_select
var flag_color_id = '';
if(select_option == 'flag_color' || $( "#flag_in_url" ).val() == 'yes')
{
if($( ".flag_color" ).val() != undefined)
{
var flag_color_id = $( ".flag_color" ).val();
if(flag_color_id == 0)
flag_color_id = 6;
search_url += 'flg/'+flag_color_id+'/';
}
}
Its because conflict with the value of white color flag...
In the view I changed the code
if($int_flag_url == $key)
{
$flag_selected = 'selected';
}
to
if($int_flag_url == 0 || $int_flag_url == $key)
{
$flag_selected = 'selected';
}

PHP if - else - else statement

<html>
<input type='text' name='mobile phone' value='
<?php if (strpos($phone_number, '07') === 0) {
echo $phone_number;
} else {
echo $alt_phone;
}?>'
</html>
Works fine. I would like to combine the above with:
<?php if (!empty($alt_phone)) {
echo $alt_phone;
} else {
echo '07777777777';
}?>'`
I have tried ELSEIF with the new condition, and a completely separate <?php ?> section and both times I get a blank page, instead of a textbox with a telephone number in it.
I am trying to achieve this: If $phone_number is a mobile, enter this number, otherwise enter the alt_phone, unless $alt_phone is blank, then enter '07777777777'.
try
<?php
if (!empty($phone_number)) {
echo $phone_number;
}
elseif(!empty($alt_phone))
{
echo $alt_phone;
}
else{
echo '07777777777';
}
?>'`
This will do the trick
<?php
if (strpos($phone_number, '07') === 0) {
echo $phone_number;
}
else if (!empty($alt_phone)) {
echo $alt_phone;
}
else {
echo '07777777777';
}
?>

PHP validating a bunch of check boxes failing

Form
echo '<form action="formProcess.php" method="post">';
echo '<input type="checkbox" name="country[]" value="US">';
echo '<input type="checkbox" name="country[]" value="UK">';
echo '<input type="checkbox" name="country[]" value="SE">';
echo '<input type="checkbox" name="country[]" value="CA">';
echo '<input type="submit" name="Submit" value="Submit">';
echo '</form>';
?>
formProcess.php
<?php
if (isset($_POST['country'])) {
echo 'Set <br>';
if (check($_POST['country'])){
echo 'Country OK<br>';
} else {
echo 'Country Faulty<br>';
}
} else {
echo 'Not Set';
}
function check($colors) { // This function is where I fail
global $countries;
foreach($colors as $country) {
echo "You selected: $country <br>";
if (array_key_exists($country,$countries)) {
return true;
} else {
return false;
}
}
}
?>
I'm trying to validate a bunch of checkboxes, but I'm going wrong with the return part inside the function.
The problem is, even if I select more than one checkbox, i get only one value outputted. The problem is in the return in the function. How is this done right?
Output
Set
You selected: US // Outputs only one even when more are selected.
Country OK
countries array
$countries = array (
"US" => "United States Of America",
"GB" => "United Kingdom",
"CA" => "Canada"
// a lot more removed for this question
}
You can create an array with the selected checkboxes and return that instead.
function check($colors) {
global $countries;
$checkedCountries = array();
foreach($colors as $country) {
echo "You selected: $country <br>";
if (array_key_exists($country,$countries)) {
$checkedCountries[] = $country;
echo $country . "<br />";
}
}
if(count($checkedCountries) > 0)
return true;
else
return false;
}
Your code is a bit awkward, but:
function check($colors) {
global $countries;
foreach($colors as $country) {
echo "You selected: $country <br>";
if (!array_key_exists($country,$countries)) {
return false;
}
}
return true;
}
You will achieve this using the following function
function check($colors) { // This function is where I fail
global $countries;
foreach($colors as $country) {
//if country not exists, set return to false;
if (!array_key_exists($country,$countries)) {
return false;
}
//If you want to echo selected countries
else{
echo "You selected" . $country;
}
}
//If all contries exist, return true.
return true;
}
you can then loop your array outputting the selected countries!
You are returning the result on the first match itself, since you are looking to display all the checked items, you need rewrite your function like this..
<?php
function check($colors) {
global $countries;
$cntries=array();
foreach($colors as $country) {
if (array_key_exists($country,$countries)) {
$cntries[]=$country;
}else { return false;}
}
echo $str= "You selected ".implode("<br>",$cntries);
return true;
}
Set a variable to false before the loop
Set that variable to true inside the loop where you currently return true
Get rid of the else entirely
Return that variable after the loop

Assigning a table to a session in PHP

The following validates only one text box (txt1). If I have a series of text boxes which lies in side a table (which is dynamically populated) how do I assign that entire values of the text boxes to one single session?
Is it possible if at all?
Or else can you suggest me a way out?
<?php
if(isset($_POST['sendtwo']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;} else {$_SESSION['loged'] = $_POST['txt1'];}
}
if(isset($_POST['sendt']) || isset($_POST['sendtwo']))
{if(isset($_SESSION['loged'])) echo $_SESSION['loged']; echo "<table border='1'><tr><td>form 2 is here!</rd></tr>";
?>
//try this
<?php
if(isset($_POST['sendtwo']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!';} else {$_SESSION['loged'] = $_POST['txt1'];}
}
if(isset($_POST['sendt']) || isset($_POST['sendtwo']))
{
if(isset($_SESSION['loged'])) {
echo $_SESSION['loged'];
echo " border='1'>form 2 is here!";
}
}
?>
Something like this? I wasn't sure what you meant:
<?php
if(isset($_POST['sendtwo']))
{
if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;}
else{
foreach($_POST as $Key => $Posted)
$_SESSION[$Key] = $Posted;
}
}
if(isset($_POST['sendt']) || isset($_POST['sendtwo']))
{if(isset($_SESSION['loged'])) echo $_SESSION['loged']; echo "<table border='1'><tr><td>form 2 is here!</rd></tr>";
?>

Categories