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
Related
**hi here is the code please answer my question. I'm trying to echo condition by given input but results are three times I want single result condition by given input **
<?php
$arrayName = array('bravo', 'alpha', 'jhony');
foreach ($arrayName as $key) {
if (isset($_REQUEST['num1']) && $_REQUEST['num1'] == $key) {
echo "yes". $_REQUEST['num1']. "Available";
} else {
echo "Sorry!".$_REQUEST['num1']." is not available";
}
}
?>
<form action="" method="get" accept-charset="utf-8">
<input type="text" name="num1">
<button type="submit">submit</button></form>
Rather than loop through your white list, use in_array:
$arrayName = array('bravo','alpha','jhony' );
if(isset($_REQUEST['num1']) && in_array($_REQUEST['num1'], $key)){
echo "yes". $_REQUEST['num1']. "Available";
}
else{
echo "Sorry!".$_REQUEST['num1']." is not available";
}
I've been trying to replace each isset for $_POST(s) using foreach.
This is the real code using isset for each $_POST :
<form method='post'>
<input type='text' name='name'> <br>
<input type='text' name='address'> <br>
<input type='submit' name='send'>
</form>
<?php
if (isset($_POST['name']) && isset($_POST['address']) && isset($_POST['send']))
{
echo "All elements have been submitted";
}
else
{
echo "You forget some elements, try checking name or address";
}
?>
is there a way I can replace 4 isset(s) above with single isset using for each?
I've made this and it goes wrong.
<form method='post'>
<input type='text' name='nama'> <br>
<input type='text' name='alamat'> <br>
<input type='submit' name='kirim'>
</form>
<?php
foreach($_POST as $value)
{
if (isset($value))
{
echo "All elements have been submitted";
}
else
{
echo "You forget some elements, try checking name or address";
}
}
?>
Need help guys, It's just wasting of time if I have to write isset one by one for each element I send via post / get. I've seen something like this before in visual basic, my friend made an foreach construct to validate all textboxes in a form, so he didn't need to create something like this anymore :
if textbox1.text="" && textbox.2.text="" and so on
EDIT: jbrahy just had a typo. His answer is good now.
I think jbrahy's approach is the right general idea... but I don't think it actually works.
I'd do this.
$requiredFields = ["nama","alamat","kirim"];
$allElementsSet = true;
foreach ($requiredFields as $requiredField)
{
if (!isset($_POST[$requiredField]))
{
$allElementsSet = false;
break;
}
}
if ($allElementsSet)
{
echo "All elements have been submitted";
} else {
echo "You forget some elements, try checking name or address";
}
$_POST will only have the variables that are passed in and there are HTML elements that might not be passed in by just submitting. It's probably best to create a $required_fields array and even better if you can get some field validation but that's not what you asked for here. You'll want to take the key and value of $_POST.
$required_fields = array("name" => FALSE, "alamat" => FALSE, "kirim" => FALSE);
foreach ($required_fields as $key => $value){
if (isset($_POST[$key])){
$required_fields[$key] = TRUE;
}
}
foreach ($required_fields as $key => $value){
if (!$required_fields[$key]){
echo "Missing value for " . $key;
}
}
if you are trying to figure out if every key that is passed in has a value then something like this would work also.
foreach ($_POST as $key => $value){
if ($value == ""){
echo "Value(s) are missing";
break;
}
}
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
Is there a way to check of a checkbox is unchecked with php? I know you can do a hidden field type in html but what about with just php when the form is submitted? I tried below no luck.
if(!isset($_POST['server'])||$_POST['server']!="yes"){
$_POST['server'] == "No";
}
If a checkbox is not checked it will not be posted. if(!isset($_POST['checkboxname'])) will do the trick.
Be aware, though, you should at least submit something so that you know the form was submitted in the first place.
if (isset($_POST['formWasSubmitted'])) {
//form was submitted...let's DO this.
if (!isset($_POST['checkboxname'])) {
// checkbox was not checked...do something
} else {
// checkbox was checked. Rock on!
}
}
This is an old question, but for people looking for this....
Better approach to Matt's answer is to use $_SERVER['REQUEST_METHOD'] to check if form was submitted:
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
//form was submitted...let's DO this.
if (!isset($_POST['checkboxname'])) {
// checkbox was not checked...do something
} else {
// checkbox was checked. Rock on!
}
}
$checkedfeild = #$_POST["yourfeildname"];
if(isset($checkedfeild))
{
//Code here
}
else
{
echo"Not checked";
}
Try this:
$checked = $_POST['notif'];
foreach($checked as $ch){
if($ch == $i){
/add element to checked set
$array_checked[]=$ch;
}
}
for($i=1;$i<6;$i++){
if(in_array($i,$array_checked)){
//do for checked
}else{
//do for unchecked
}
}
I have checkboxes which is generated in while loop. These checkboxes are always checked for the first time and the value stored in the database is "On". But when user unchecks it, I want to store "Off" in database. But my problem is i am not able to access the checkbox name to check if which checkboxes are unchecked on button click. I wrote this code
while($row = mysql_fetch_assoc($result)) {
if($pck_id_renew == 'On')
{
echo '<td class=c><input type="checkbox" id="renew_chk" name="renew_chk"
checked="checked" style="width:50px" value="On"/></td>';
}
if($pck_id_renew == 'Off') {
echo '<td class=c><input type="checkbox" id="renew_chk" name="renew_chk"
style="width:50px" value="Off"/></td>';
}
}
You will only receive the input value of checkboxes that are checked. Use an array and store all available checkbox names in it. Loop through this array to detect which checkboxes are checked/unchecked.
Code may look something like this:
// Define available checkboxes
$inputs = array('renew_chk', ...);
// Check input values
$values = array();
foreach ($inputs as $input) {
if (isset($_POST[$input])) {
$values[$input] = 'On';
}
else {
$values[$input] = 'Off';
}
}
// Loop through $values and store value in database
foreach ($values as $input => $value) {
// UPDATE database SET $input = $value WHERE id = [n];
}