I'd like to print something like "yes, checked" if a cck single checkbox is checked.
The single on/off checkbox has allowed values of no and yes.
checkbox info is -
<input type="checkbox" class="form-checkbox" checked="checked" value="yes"
id="edit-field-billing-terms-value" name="field_billing_terms[value]">
I'm trying, and failing with modifications of this code -
<?php
$node->field_billing_terms[value] . '<br />';
if($node->field_billing_terms[value] == 'yes' ) {
print "yes, checked";
}
?>
Can someone give me some pointers where I'm going wrong? More info can be provided if needed.
Fields are normally in a zero based array when attached to the node object, this should fix your problem:
$node->field_billing_terms[0]['value'] . '<br />';
if($node->field_billing_terms[0]['value'] == 'yes' ) {
print "yes, checked";
}
Related
Intro
I have some custom fields in my WordPress profile area. Here's an example of a checkbox in my code:
<input type="checkbox" name="A" id="A" value="yes" <?php if (esc_attr( get_the_author_meta( "A", $user->ID )) == "yes") echo "checked"; ?> /><label for="my_field "><?php _e("Order Button"); ?></label><br />
This field is A, and I'm saving it in the same file with:
update_usermeta( $user_id, 'A', $_POST['A'] );
This is working, and when the user clicks it on or off, it saves.
The Problem
However, I am having a big issue posting the results if the checkbox was checked.
In another file I have this:
if(isset($_POST['A'])) {
echo 'asd';
} else { }
I have tried a lot of variations, but I can not figure out how to get the If statement to check whether the user checked the box or not, it only echos the else.
TL;DR:
I can't figure out how to check if the usermeta is checked or not, and how to output that into another element of the website.
http://pastebin.com/Q3WSv47m Link to User Profile Code
http://pastebin.com/spU2dqGk Link to Header area where I'm trying to call the checkbox.
You must check checked variable value. Try this:
if (isset($_POST['A']) && $_POST['A'] == 'yes')
{
echo 'A is checked';
}
else
{
echo 'Something else is checked.';
}
note: make sure your form method is POST.
I've tried to get those checkbox that are unchecked. At this moment i'm just getting those that are checked. Here is my code. That value will be inserted on a table which i don't want to leave it null, that's why i need to get those checkbox unchecked, so that i could insert 1 for checked or 0 for unchecked:
<?php
if (!empty($_POST['menu'])) {
# code...
foreach ($_POST['menu'] as $value) {
# code...
echo "<p>ID Checkbox checked: ".$value;
}
}
?>
One of the reasons for what i need to get both status: checked or unchecked is because i don't want to leave database fields empty.
Unchecked checkboxes don't get POSTed. If you know what fields should be there, you'll probably have to list them out manually.
The wonderful thing about checkboxes is that when you don't have one checked, and you submit a form, nothing is sent to the server. Take this checkbox for example:
<input type="checkbox" name="test"/>
If you left it unchecked and looked for $_POST['test'] it would error out, as it is not set.
So, try doing this:
if(!isset($_POST['name_of_checkbox'])){
// Fill database with some empty value.
} else {
// Do what you need to do for checked value.
}
Hope that gives you some insight!
Say if they are named the same, and you know the number of checkbox you can use a for loop:
if (!empty($_POST['menu'])) {
for ($i=0; $i < 10; $i++) {
if(isset($_POST['menu'][$i])){
echo "Checkbox checked: " . $_POST['menu'][$i];
}else{
echo "Checbox uncheck #" . $i;
}
}
}
You can put the names in an array, then iterate:
$checkboxes = array('cbx1', 'cbx2', 'cbx3');
foreach ($checkboxes as $checkbox) {
if (isset($_POST[$checkbox])) {
echo "<p>ID Checkbox checked: " . $_POST[$checkbox];
} else {
echo "Checbox uncheck :" . $checkbox;
}
}
So yeah many ways to achieve this, it depends on the situation.
Check for #Artur approach as well for client side solution.
<input type="hidden" name="checkbox[8]" value="0">
<input type="checkbox" name="checkbox[8]" value="8">
This would be one way to also get 0's posted. Important part is that the name's are the same. So if checkbox is checked, it's value would override the hidden input value.
Thank you all for your time and be so kind to answer my question. The reason for what i needed to have those checkboxes unchecked and checked is because i have a dynamic menu and so i assign menus to users. Here is the solution i found with a friend of mine at work:
Code for the checkboxes. I query all the menus available on my table Menus so that i will show the administrator all the menus availables so then he could choose which menus he will assign to the users:
´
<table>
<tr>
<td>Menus a asignar:</td>
<td>
<?php
$query_menus_checkbox = mysql_query("SELECT id_menu, nombre_menu FROM menus");
while ($checkbox_mostrar = mysql_fetch_row($query_menus_checkbox)) {
# code...
?>
<input type="checkbox" name="menus[<?php echo $checkbox_mostrar[0]; ?>]" value="<?php echo $checkbox_mostrar[0] ?>"><?php echo $checkbox_mostrar[1] ?>
<p></p>
<?php
}
?>
</td>
</tr>
</table>
´
Then here is the code to process which checkboxes are checked or not to insert on my table (id_user is not shown but i have taken from another query which is not shown so you'll have to query yourself):
´
$res=mysql_query("select id_menu from menus");
$arid=array();
while($xd=mysql_fetch_assoc($res)){
$arid[]=$xd['id_menu'];
}
for($i=0;$i<count($arid);$i++){
$id_menu=$arid[$i];
$activo=(isset($_POST['menus'][$id_menu]) && $_POST['menus'][$id_menu]!="")?1:0;
$inserta_menus = mysql_query("INSERT INTO menus_usuarios(id_menu, id_usuario, estado) values ('$id_menu', '$id_user[0]', '$activo')");
}
´
I am building a Order form for a Client's website. One of the pages lets users choose Vehicle Spareparts by entering the quantity beside each part. I have achieved this by writing the following code
<input class="qty" type="number" name="oil-pump" min="0" max="100">
The above input attribute is added several times besides each sparepart type while changing just the name value. On clicking Submit, the user is taken to a review page with details on the spareparts ordered.
I presently have the following code in review page:
<?php
echo 'Engine Bearings: ' . $_POST['eng-bearings'];
echo 'Cylinder Heads: ' . $_POST['cyl-heads'];
echo 'Cam and Valve Train: ' . $_POST['cam-valve-train'];
echo 'Oil Pump: ' . $_POST['oil-pump'];
echo 'Oil Caps and Pipes: ' . $_POST['oil-caps-pipes'];
?>
This works well if the user has entered a value in every field. If he doesn't, then the review page has just the echo text without any quantity. Also, I don't want the quantity to apppear if the user has entered 0.
I can't figure out how to use isset() with null and OR statements to not echo text when the value entered is 0 or left blank.
Any help is appreciated. Thanks!!
This is the perfect opportunity for a function:
function print_user_input($title, $queryVar) {
if (isset($_POST[$queryVar]) && !empty($_POST[$queryVar])) {
echo $title . ': ' . $_POST[$queryVar];
}
}
Use it like so:
print_user_input('Engine Bearings', 'eng-bearings');
Echo the value conditionally. If the target data is empty, echo an empty string:
echo empty($_POST['eng-bearings']) ? '': 'Engine Bearings: ' . $_POST['eng-bearings'];
Repeat for the other post values. Information about the ternary operator (<conditional> ? <value1> : <value2>) is here.
You need to check if its set & that its not empty before printing it on a screen.
<?php
if (! empty($_POST['eng-bearings'])) {
echo 'Engine Bearings: ' . $_POST['eng-bearings'];
} else {
// whatever you wanna do here in case eng-bearings is not set or its empty
}
// Do same for other $_POST values ...
?>
Hello I'm having some problem.
I want it to be checked by default, and if you unselect it and press submit I want it to be unchecked
<input type="checkbox" name="show_signature" value="1"<?php echo isset($_POST['show_signature'])) ? ' checked=""' : '' ?>>
This works good unchecking > submitting and checkbox is unchecked and same if you check it and send the form, it stays checked.
But, I want it to be checked by default. Should'nt this work?
if (isset($_POST['show_signature'])) {
echo ' checked=""';
} else {
echo '';
}
Tried this to
if (isset($_POST['show_signature']) || !isset($_POST['show_signature'])) {
echo ' checked=""';
} else {
echo '';
}
Okay; from the question, I got this process
When the page loads the first time, you want the checkbox to be checked.
When the page is submitted, if the checkbox is unchecked, let it remain unchecked; otherwise, let it be checked.
Try
/*making the assumption that the submission process starts when a submit button
with name **submit** is present, use this
*/
if (isset($_POST["submit"])){
value = '<input type="checkbox" name="show_signature"';
value .= (isset($_POST["show_signature"]))? 'checked="checked"': "";
value .= ' />';
print value;
}
else{ //when the page is initially loaded
print '<input type="checkbox" name="show_signature" checked="checked" />';
}
The reason the post depends on the submit button (or any other field)t is because if the user unchecks the box, the $_POST["show_signature"] variable will not be found, and the form will not be processed at all.
That should resolve the issue.
Hope the explanation is clear and this helps.
I am currently trying to design a for loop to iterate through any 'checked' check boxes, and from that, use the value within the 'value' slot to run some queries. I am unfortunately struggling with this as the list of checkboxes are not pre-defined, they are dynamic from the database pending the users previous selection.
The loop actually works to present the items to be checked:
?>
<input type="checkbox" name="option[]" value="$listing_id">
<font size="+1" color="green"><?php echo"$list_name"; ?>:</font><br />
<?php
The listing ID within the value is what I need to work with in a mysql query before I run an update query. The for loop that's meant to work is:
foreach($_POST['option'] as $option) //loop through the checkboxes
{
...
}
The update query will work within this as its simply copied from somewhere else, I just need the 'Listing_ID' from the check boxes that are checked.
I ran this code to hopefully do some debugging:
if(empty($_POST['option'])){
echo "no checkboxes checked.";
} else {
if(!isset($_POST['option'])){
echo "no set.";
}
}
and it returns "no checkboxes checked."
I have now hit a grey area as to why this for loop isn't working (this was taken from another example on the internet).
empty($_POST['option']) will return true, if either $_POST['option'] is not set (same as !isset($_POST['option']) (!)) or an empty array.
If you need to debug what's going on, use var_dump($_POST['option']); to find out what has been submitted for the option checkboxes. I also suggest you do a var_dump($_POST); so you can see what has been submitted overall - e.g. in case the post action is not post you will immediatly notice). For HTML output:
echo '<pre>', htmlspecialchars(print_r($_POST, true)), '</pre>';
That should give you the information you're looking for. For each individual checkbox, you can do:
foreach($_POST['option'] as $option) //loop through the checkboxes
{
var_dump($option);
}
First of all your code seems to be bugged to me. Maybe is just a typo but
<input type="checkbox" name="option[]" value="$listing_id">
should be
<input type="checkbox" name="option[]" value="<?=$listing_id?>"/>
Moreover using empty over an array is not good at all.
Try echoing out the $option in the loop to see what the value is and there you can see if there is something there.
foreach($_POST['option'] as $option) //loop through the checkboxes
{
echo $option . "<br />";
}
Also make sure your form's method is set to POST or that it's action is pointed to the correct place. You also have an error in your input:
<input type="checkbox" name="option[]" value="$listing_id">
I assume you meant:
<input type="checkbox" name="option[]" value="<?php echo $listing_id;?>">
UPDATE:
The error ended up not being in the code posted. Error was discovered in an if statement that always returned false that in-cased the code posted above.