I am having trouble using $_GET with radio buttons.
4th<input type="checkbox" name="date" value="4th">
5th<input type="checkbox" name="date" value="5th">
6th<input type="checkbox" name="date" value="6th">
The user chooses what days they are available. Then I want to echo out what days the user selected:
<?php echo "You are available " . $_GET["date"] . "!"; ?>
The above code only echos out one. Not all three. Is there a way to do this?
checkbox values are returned in an array as they share the same index, so you need to use name="date[]" in your HTML.
If you want to know more, just try to print_r($_GET['date']); and see what you get.
And you've tagged your question as radio so would like to inform you that radio and checkbox are 2 different things, radio returns single value, where checkbox can return multiple.
Name will be an array
<input type="checkbox" name="date[]" value="4th" />
<input type="checkbox" name="date[]" value="5th" />
<input type="checkbox" name="date[]" value="6th" />
Then get value like this
<?php
echo "You are available ";
foreach($_POST["date"] as $value) {
echo "$value";
}
?>
You could give each input an id:
<input type="checkbox" id="date1" value="4th" />
<input type="checkbox" id="date2" value="5th" />
<input type="checkbox" id="date3" value="6th" />
Then echo it like this:
$date1 = $_GET["date1"];
$date2 = $_GET["date2"];
$date3 = $_GET["date3"];
<?php echo "You are available " . $date1. ",". $date2. ",". $date3. ",". "!"; ?>
Xth<input type="checkbox" name="date[]" value="Xth">
You can use in php
$_POST['date'][0]
$_POST['date'][1]
Please use array to get multiple value -
Code:
4th<input type="checkbox" name="date[]" value="4th">
5th<input type="checkbox" name="date[]" value="5th">
6th<input type="checkbox" name="date[]" value="6th">
<?php
$date=$_GET['date'];
foreach($date as $dt){
echo "You are available " . $dt . "!<br>";
}
?>
I am not checking above code. I guess it will work.
Related
hello
i am not being able to submit these value in my controller.
the code is as follows
<input name="status.<?php echo $data['LEAVE_ID']?>" type="radio" class="ace" value="1" checked="" required="" />
<span class="lbl"> YES</span>
<input name="status.<?php echo $data['LEAVE_ID']?>" type="radio" class="ace" value="0"/>
<span class="lbl"> NO</span>
For multiple radio use array in name attribute with id as a index key like this
name="status[<?php echo $data['LEAVE_ID']?>]"
You can achieve it using below code, you just have to add css.
<?php
foreach($dataArrry as $data) {
echo $_POST['status.' . $data['LEAVE_ID']]."<br/><br/>";
}
?>
Ok I have a set of checkboxes with corresponding text inputs next to them. POST apparently sets a variable as an associative array, I want to be able to concat the values for the checkbox with the corresponding year by using a for loop and putting like indexes together. I'm currently getting an "unexpected [" error in the if statement. I looked into doing exactly this and found a few examples that went about this in the same way with success, what am I doing wrong?
Here is the form section
<form>
<label><input type="checkbox" name="check_list[]" Value = "Castle Connolly Top U.S. Doctor" id ="ccn"/>Castle Connolly Top U.S. Doctor</label>
<input type="text" name="check_year" value="<?php echo decode_text($check_year);?>"/>
<label><input type="checkbox" name="check_list[]" Value = "Vitals Patients Choice"/>Vitals Patient's Choice</label>
<input type="text" name="check_year" value="<?php echo decode_text($check_year);?>"/>
<label><input type="checkbox" name="check_list[]" Value = "Vitals Compassionate Doctor"/>Vital's Compassionate Doctor Recognition</label>
<input type="text" name="check_year" value="<?php echo decode_text($check_year);?>"/>
<label><input type="checkbox" name="check_list[]" Value = "Super Doctor"/>Super Doctor</label>
<input type="text" name="check_year" value="<?php echo decode_text($check_year);?>"/>
</form>
Then in my handler I have this code to get the values into separate arrays and concat them to another variable that I insert
$check_list = isset($_POST['check_list']) ? prepare_input($_POST['check_list']) : '';
$check_year = isset($_POST['check_year']) ? prepare_input($_POST['check_year']) : '';
$check_year_array = array_values($check_year);
$check_list_array = array_values($check_list);
$notable_awards .= ', ';
for($n=0; $n<5;$n++){
if(!empty($check_list_array[$n])){
$notable_awards .= $check_list_array[$n].','.$check_year_array[$n];
}
}
$sql = mysql_query("UPDATE" .TABLE_DOCTORS. " SET
notable_awards = '".$notable_awards."' ,
notable_publications = '".$notable_publications."'
last_logged_ip = '".$user_ip."'
WHERE id =".$doctor_id);
I have 1 form in with multiple checkboxes in it (each with the code):
<input type="checkbox" name="check_list" value="<? echo $row['Report ID'] ?>">
Where $row['Report ID'] is a primary key in a database -so each value is different.
How would I be able to tell which checkboxes have been checked? (Maybe multiple)
This is for an inbox system and I have a button below that I want (when clicked) to delete all messages (ids of: $row['Report ID']) which have the checkbox's checked.
Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).
Here's a little sample as requested:
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="checkbox" name="check_list[]" value="value 2">
<input type="checkbox" name="check_list[]" value="value 3">
<input type="checkbox" name="check_list[]" value="value 4">
<input type="checkbox" name="check_list[]" value="value 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
?>
Edit To reflect what #Marc said in the comment below.
You can do a loop through all the posted values.
HTML:
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
PHP:
foreach($_POST['check_list'] as $item){
// query to delete where item = $item
}
you have to name your checkboxes accordingly:
<input type="checkbox" name="check_list[]" value="…" />
you can then access all checked checkboxes with
// loop over checked checkboxes
foreach($_POST['check_list'] as $checkbox) {
// do something
}
ps. make sure to properly escape your output (htmlspecialchars())
<input type="checkbox" name="check_list[<? echo $row['Report ID'] ?>]" value="<? echo $row['Report ID'] ?>">
And after the post, you can loop through them:
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $report_id){
echo "$report_id was checked! ";
}
}
Or get a certain value posted from previous page:
if(isset($_POST['check_list'][$report_id])){
echo $report_id . " was checked!<br/>";
}
It's pretty simple. Pay attention and you'll get it right away! :)
You will create a html array, which will be then sent to php array.
Your html code will look like this:
<input type="checkbox" name="check_list[1]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[2]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[3]" alt="Checkbox" value="checked">
Where [1] [2] [3] are the IDs of your messages, meaning that you will echo your $row['Report ID'] in their place.
Then, when you submit the form, your PHP array will look like this:
print_r($check_list)
[1] => checked
[3] => checked
Depending on which were checked and which were not.
I'm sure you can continue from this point forward.
I am trying to get a checkbox checked by default, but everything I have tried doesn't seem to work. I don't know if it has to do with the PHP that is in the code.
function show_subscription_checkbox ($id='0') {
global $sg_subscribe;
sg_subscribe_start();
if ( $sg_subscribe->checkbox_shown ) return $id;
if ( !$email = $sg_subscribe->current_viewer_subscription_status() ) :
$checked_status = ( !empty($_COOKIE['subscribe_checkbox_'.COOKIEHASH]) && 'checked' == $_COOKIE['subscribe_checkbox_'.COOKIEHASH] ) ? true : false;
?>
<p <?php if ($sg_subscribe->clear_both) echo 'style="clear: both;" '; ?>class="subscribe-to-comments">
<input type="checkbox" name="subscribe" id="subscribe" value="subscribe" style="width: auto;" <?php if ( $checked_status ) echo 'checked="checked" '; ?>/>
<label for="subscribe"><?php echo $sg_subscribe->not_subscribed_text; ?></label>
</p>
This is a wordpress plugin that allows you to subscribe to blog comments.
I have tried
echo 'checked=\"checked\" ';
echo 'checked="checked" ' ;
echo 'checked> ';
The plugin author states that you used to be able to default check the checkbox but not anymore.
Since this is showing up in google for "default checked checkbox", I figured I'd answer it. Alpay was right: The correct way to ensure that a checkbox is checked by default is like so (followed by an example of one that is not checked):
<input type="checkbox" name="vehicle" value="Car" checked> I have a car
<input type="checkbox" name="vehicle" value="Bike"> I have a bike
Answer was found on w3schools. The author of the original question was having trouble with his PHP code, which is not at all related to the question title.
In HTML, if you want a checkbox to be checked by default, see the following;
<input type="checkbox" name="name1" value="uc"> This checkbox is unchecked <br>
<input type="checkbox" name="name2" value="c" checked> This checkbox is checked<br>
So, you might consider changing
<?php if ( $checked_status ) echo 'checked="checked" '; ?>
to
<?php if ( $checked_status ) echo 'checked'; ?>
The problem is not in the HTML markup being generated; echo 'checked="checked" ', as in the question, works well, and so would the simpler echo 'checked'.
The problem is with the condition $checked_status. You are testing for a variable that is undefined, as far as the code posted is considered.
I had the same problem. I figured out that I was trying to put the checkbox into a table but left out the and.
didn't check:
if(!$stump){echo '<input type="checkbox" name="stump" value="stump" checked="checked"><b> Stump Job!</b>';}
checked:
if(!$stump){echo '<td><input type="checkbox" name="stump" value="stump" checked="checked"><b> Stump Job!</b></td>';}
In regular PHP you can use this to "save" the checked state after its been submitted.
<form name="checkbox" method="post" action="#" >
<input type="checkbox" name="checkbox1" value="Bike" <?php if ($_POST['checkbox1']=="Bike") echo "checked";?>>I have a bike
<input type="checkbox" name="checkbox2" value="Car" <?php if ($_POST['checkbox2']=="Car") echo "checked";?>>I have a car
<input type="submit" name="submit" value="Display this Data" />
if you want to use this data for something else after the submit, just add:
<?php
if(isset($_POST['checkbox1']) OR isset($_POST['checkbox2']) )
{
echo "for 1 : ".$_POST['checkbox1']."for 2: ".$_POST['checkbox2'];
}
?>
if you want to clear the form (basically clear all the post data) you can add:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="submit" name="Clear all form data" value= "Clear this form">
</form>
Is there a read only property for a checkbox?
Because I can still tick on the checkbox even if I have this code, is the read only property only for text box? What's the code if you want the check box to be read-only?
<td><input name="stats1" type="checkbox" id="SSS" readonly="readonly" value="<?php echo $row["STAT"]; ?>" <?php echo $row["STAT"] ? 'checked="checked"' : ''; ?> >SSS</td>
The readonly attribute on HTML input elements actually means that the value is readonly.
You actually want to make a checkbox uncheckable, in that case grab Javascript:
<input type="checkbox" onclick="return false;">
Try disabled="disabled" instead ;)
here i have inserted 7 checkboxes and set their name as c1,c2....c7 in the html page
<input type="checkbox" name="c1" value="english" />
<input type="checkbox" name="c2" value="hindi" />
<input type="checkbox" name="c3" value="gujarati" />
.
.
.
<input type="checkbox" name="c7" value="Marathi" />
here we will use for loop with upto 7 times iteration
for($i=1;$i<=7;$i++)
{
echo $_GET['c'.$i] ."</br>";
}
here 'echo' statement will print values from gettig name of checkboxes c1 ...c7
Here $_GET['c'.$i] will print only its value if that checkboox is checked