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
Related
I have a little problem here. I have a list, which is:
<form action="display.php" method="post">
<select name="holiday">
<option value="month">Kiekvieno mėnesio skaičiavimas</option>
<option value="day">Kiekvienos dienos skaičiavimas</option>
</select>
<br> <br>
<input type="submit" name="select" value="Pasirinkti" />
</form>
What I need to do is that when the user selects value ="month", php code would do one action, and when the user selects value ="day", php code would do another action?
My php code looks like this:
<?php
if($_POST['select']){
// Storing selected value in a variable
$selectedValue= $_POST['holiday'];
}
else if ($selectedValue == $_POST['month']) {
$todaysDate = new DateTime();
while ($employee = $select->fetch()){
$employmentDateValue = new DateTime($employee['employment_date']);
// Comparing employment date with today's date
$differenceBetweenDates = date_diff($employmentDateValue, $todaysDate);
$workMonths = $differenceBetweenDates->y * 12 + $differenceBetweenDates->m;
$holidayDays = $workMonths *4;
echo "<tr>";
echo "<td>".$employee['name']."</td>";
echo "<td>".$employee['surname']."</td>";
echo "<td>".$employee['employment_date']."</td>";
echo "<td>".$workMonths."</td>";
echo "<td>".$holidayDays."</td>";
echo "</tr>";
}
}
else {
echo "Lalalala";
}
?>
I've tried to do so with $_POST['select'], but it doesn't work. Thanks for any help guys
<?php
if($_POST['select']){
// Storing selected value in a variable
$selectedValue= $_POST['holiday'];
if ($selectedValue == 'month') {
}
else if ($selectedValue == 'day') {
}
else{
echo "Lalalala";
}
}
?>
You need to do $_POST['holiday'], so change:
if($_POST['select']){
// Storing selected value in a variable
$selectedValue= $_POST['holiday'];
}
to
if($_POST['holiday']){
// Storing selected value in a variable
$selectedValue = $_POST['holiday'];
}
You also need to change the line:
else if ($selectedValue == $_POST['month']) {
So it's not part of the original if statement:
if ($selectedValue == 'month') {
// your code
}
else {
echo "Lalalala";
}
<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';
}
?>
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;
}
}
I cannot get my PHP script to echo the second else statement if the first result empty.
The way my script currently works is "Print Addresses (from another array) > List Comments", however even if a comment is empty for an address is will either print the comments or nothing, I cannot make the script echo the word "No comment".
if(!empty($row['id']))
{
echo "$row[comment]<br/>";
}
else
{
echo "no comment<br/>";
}
Any help appreciated. Thanks.
Assuming this comes from a database and each row has an id, this will always be true:
if(!empty($row['id']))
Try:
if(!empty($row['comment']))
If id is something else, the same logic applies: check the value that you intend to print:
if (!empty($row['id']) && !empty($row['comment']))
{
echo $row['comment'].'<br/>';
}
else
{
echo "no comment<br/>";
}
EDIT: If this code is looping through all comments attached to a post or something, there will never be any output if there are no comments to loop through. In that case try something like this:
if (count($comments) === 0)
{
echo "no comments<br />";
}
else
{
foreach ($comments as $row)
{
if (!empty($row['comment']))
{
echo $row['comment'].'<br />';
}
else
{
echo "no comment<br />";
}
}
}
OR:
$comment_count = 0;
foreach ($comments as $row)
{
if (!empty($row['comment']))
{
echo $row['comment'].'<br />';
$comment_count++; // We have at least one comment
}
else
{
echo "no comment<br />";
}
}
if ($comment_count === 0) echo 'no comments<br />';
I have a checkbox down below...
It is in the loop :
<script>
function checkCheckBoxes_abel() { //check if the checkbox is checked before submitting.
if (document.payform.pay_checkbox.checked == false)
{
alert ('You didn\'t choose any of the checkboxes for payment !');
return false;
}
else
{
alert ('One or more checkboxes from payment form are checked!');
document.forms["payform"].submit();
return true;
}
}
</script>
<form name="payform" onsubmit="return checkCheckBoxes_abel();" method="POST" action="payment.php">
for($record_count=0;$record_count<$record;$record_count++)
{
<td><input type="checkbox" name="pay[]" id="pay_checkbox" value="<?php echo $amount_dueArr[$record_count];?>" onClick="checkTotal()"/></td>
}
</form>
How can I pass the value of the checkbox that is being selected ?
Thanks
can I do :
if (isset($_POST['pay']))
{
foreach($_POST["eg_payamt_"] as $key => $payamt){
echo "eg_payamt_$key => $payamt\n <br>";
}
}
on payment.php ?
Thanks
An illustration :
I have three checkboxes...
If I check one of the checkbox,
Checkbox ticked on : Array
and if I'm not checking any of them
Checkbox ticked on :
Which is correct, but the content of the Array is not only one but three of them,
How can I make it only one ? or only two ? depends on how many checkboxes are being checked.
can I do it on another field ?
it seems that it works only for one field
if (isset($_POST['pay']))
{
if(is_array($_POST['pay']))
{
//foreach($_POST["pay"] as $key => $desc)
foreach($_POST["eg_description_"] as $key => $desc)
{
echo "eg_description_$key => $desc\n <br>";
}
}
else
{
//echo 'description :'.$_POST['pay'];
echo 'description :'.$_POST["eg_description_"];
}
}
there are 2 types of values will receive in POST, if someone selects only one checkbox that will throw warning in foreach loop so you can try this way
if (isset($_POST['pay']))
{
if(is_array($_POST['pay'])) {
//foreach($_POST["eg_payamt_"] as $key => $payamt){
foreach($_POST["pay"] as $key => $payamt){
echo "eg_payamt_$key => $payamt\n <br>";
}
}
else {
echo 'pay : '. $_POST['pay'];
}
}
Check these -
http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html
http://www.html-form-guide.com/php-form/php-form-checkbox.html