I have the following code:
<div><input type="checkbox" name="fhaac_publicly_queryable" value="<?php
if (isset( $_POST ['fhaac_publicly_queryable'])) {
echo "checked";
} elseif ($fhaac_publicly_queryable == "on") {
echo "checked";
}
?>" name="fhaac_publicly_queryable" /> Publicly Queryable on a search</div>
I have the content saving to the database, but on save, the checked check box disappears and I can't figure out how to query so it returns. Any ideas?
Thanks in advance
You're writing the checked attribute as the value of the value attribute. Plus, the name attribute is written twice. Do this instead:
<div>
<input type="checkbox" name="fhaac_publicly_queryable" <?php
if (isset($_POST['fhaac_publicly_queryable'])){
echo 'checked';
} else if($fhaac_publicly_queryable == "on"){
echo "checked";
}?>>
Publicly Queryable on a search
</div>
I still encourage you to please do the PHP logic somewhere else, because this just looks awful. It really does. And to edit your if/else, it could be improved with or (||).
Related
I'm building a contact page with email, name, etc. in HTML with PHP. I have radio buttons on my contact page as well. If the user submitted their name and checked a radio button but forgot to put an email in, the form processing page will flipped them back to the contact page with an error. I'm able to have my contact keep the values put in for their name (and email if user inputs it), but it does not keep the value checked in the radio button.
I'm new to PHP, so I bet it's a silly error on my part. Here's what I have for my Name Input:
Your Name:
<input type="text" name="name" <?php
if (isset($form['name'])) {
echo 'value="';
echo htmlentities($form['name']);
echo '"';
}
?>/>
I tried to do something similar to my Radio. Here's what I have for my Radio:
Are you New to our Business?<br>
<input type="radio" name="customer" value ="yes" <?php
if (isset($form['customer'])) {
echo 'value="';
echo htmlentities($form['customer']);
echo '"';
}
?>/>
Yes, I am!<br>
<input type="radio" name="customer" value="no"<?php
if (isset($form['customer'])) {
echo 'value="';
echo htmlentities($form['customer']);
echo '"';
}
?>/>
No, I am a returning customer!
I'm storing the values on the user input in an array called $form - that's why I have ($form['name]). I would like to it to continue doing that. Some other responses I have researched simply have an isset without the array part.
Hopefully I've provided enough information... Thanks for your help!
You need to do it like this:
if (isset($form['customer']) && $form['customer'] == "yes") {
echo 'checked="checked"';
}
and
if (isset($form['customer']) && $form['customer'] == "no") {
echo 'checked="checked"';
}
You need to change the if-conditional to the following:
<?php
if (isset($form['customer']) && $form['customer'] == "yes") {
echo 'checked';
}
?>/>
Replace the "yes" with "no" for the No part.
I am trying to make a simple PHP open and closed switch with Radio Buttons... Basically you would come to the page and select wether the restaurant is open or closed, then the resulting echoed value would show on the homepage...
I am struggling with this code I was wondering if someone could give me some insights
<?php
if (isset($_POST['button1'])) {
$txt=$_POST['button1'];
file_put_contents('status.txt',$txt,FILE_APPEND|LOCK_EX);
exit();
}
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
Restaurant Open:
<input type="radio" name="button1" value="Open" onClick="submit();" <?php echo ($_POST['button1'] == 'Open') ? 'checked="checked"' : ''; ?> /> Open
<input type="radio" name="button1" value="Closed" onClick="submit();" <?php echo ($_POST['button1'] == 'Closed') ? 'checked="checked"' : ''; ?> /> Closed
</form>
<?php
if (isset($_POST['button1']) == 'Open')
echo "Open Today.";
else if (isset($_POST['button1']) == 'Closed')
echo "Closed Today.";
?>
If you need any additional info let me know...
EDIT: Also I need this value to stay in place until someone else comes and switches it.....
Function isset returns TRUE or FALSE. Change it:
if ( isset($_POST['button1']) && ($_POST['button1'] == 'Open') )
echo "Open Today.";
else if ( isset($_POST['button1']) && ($_POST['button1'] == 'Closed') )
echo "Closed Today.";
But using isset is not necessary.
You need to store the value in a database somewhere so it is remembered. When you pass it in the form it exists only for your own session.
The homepage should retrieve the value from the database and use it to decide what to display.
Here is an introduction to PHP and MySQL. It will seem like overkill for just storing a single value but is very useful knowledge for almost any web application.
i have a php file which stores a variable, and i need to create a submit form which passes this variable.
So the code of the current file is:
if(isset($_POST['Submit'])) {
echo "<pre>";
$checked = implode(',', $_POST['checkbox']);
echo $checked;
}
I have to insert the submit form and on click it goes to another php file, i need to get there the $checked variable.. how can i store this variable?..
Thanks you!
The principle of POSTing is that you can send all the data to the next .php page.
<?php
if ($checked == 1) {
$checked = 'checked="checked"';
}
else {
$checked = '';
}
?>
<form action="POST" method="target_file.php">
<input type="hidden" name="variableA" value="Something I want target_file.php to know" />
<input type="hidden" name="variableB" value="Something else I want target_file.php to know" />
<input type="checkbox" name="gender" value="male" <?php echo $checked ?>" />
</form>
Your target_file.php:
echo "Here I am :), variableA: ".$_POST['variableA'];
echo "Here I am :), variableB: ".$_POST['variableB'];
echo "Here I am :), My gender is: ".$_POST['gender'];
Also, don't trust on checking your field values that are send through the $_POST. Check what every value is and if it meets certain criteria before sub-statements may be executed.
Hope someone can help me with my code. i am using rewritten URL's and have this piece of code on a form. When the page first loads mypage.htm, the type_24 checkbox get's checked by default. If they check the type_12 box, i want the type_24 box to uncheck.
My problem is, if i check the type_12 box, the page refreshes and both the type_12 and type_24 boxes are checked.. which is not what i want. I think it's because i'm reloading my rewritten URL in my action because it works fine if i just have php file as the action.
Any ideas how i can fix my code so that it only has the type_12 checked when i check the type_24 box?
<form name="frmrefresh" id="frmrefresh" method="post" action="mypage.htm">
<input type="checkbox" name="type_12" id="type_12" <?php if(isset($_POST['type_12']) && $_POST['type_12']=="12"){?> checked="checked"<?php }?> value="12" onClick="uncheck24(this);" /> <label>12</label>
<input type="checkbox" name="type_24" id="type_24" <?php if(isset($_GET['id']) && $_GET['id']!=''){?>checked="checked"<?php }?><?php if(isset($_POST['month_24']) && $_POST['type_24']=="24"){?> checked="checked"<?php }?> value="24" onClick="uncheck12(this);"/> <label>24 Months</label>
<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" />
</form>
In the header i have the functions:
function uncheck12(obj)
{
if (obj.checked == true)
{
document.getElementById("type_12").checked = false;
document.frmrefresh.submit();
}
}
function uncheck24(obj)
{
if (obj.checked == true)
{
document.getElementById("type_24").checked = false;
document.frmrefresh.submit();
}
}
A number of remarks:
Consider using radio buttons instead of checkboxes
I think you mean isset($_POST['type_24']) instead of isset($_POST['month_24'])
As to your problem, I bet your rewrite rule is something like:
RewriteRule ^mypage.htm$ mypage.php?id=5
This means that when the form gets submitted via POST, PHP will still set the $_GET['id'] variable for you since it's in the query string. And since you check the '24' option whenever $_GET['id'] is set, the second checkbox will always get checked.
To fix this you could consider adding the check $_SERVER['REQUEST_METHOD'] == 'GET':
<form name="frmrefresh" id="frmrefresh" method="post" action="mypage.htm">
<input type="checkbox" name="type_12" id="type_12" <?php if(isset($_POST['type_12']) && $_POST['type_12']=="12"){?> checked="checked"<?php }?> value="12" onClick="uncheck24(this);" /> <label>12</label>
<input type="checkbox" name="type_24" id="type_24" <?php if(isset($_GET['id']) && $_GET['id']!='' && $_SERVER['REQUEST_METHOD']=='GET'){?>checked="checked"<?php }?><?php if(isset($_POST['type_24']) && $_POST['type_24']=="24"){?> checked="checked"<?php }?> value="24" onClick="uncheck12(this);"/> <label>24 Months</label>
<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" />
</form>
For usability purposes I like to set up my form fields this way:
<?php
$username = $_POST['username'];
$message = $_POST['message'];
?>
<input type="text" name="username" value="<?php echo $username; ?>" />
<textarea name="message"><?php echo $message; ?></textarea>
This way if the user fails validation, the form input he entered previously will still be there and there would be no need to start from scratch.
My problem is I can't seem to keep check boxes selected with the option that the user had chosen before (when the page refreshes after validation fails). How to do this?
My first suggestion would be to use some client-side validation first. Maybe an AJAX call that performs the validation checks before continuing.
If that is not an option, then try this:
<input type="checkbox" name="subscribe" <?php echo (isset($_POST['subscribe'])?'checked="checked"':'') ?> />
So if subscribe is = 1, then it should select the box for you.
For Example, consider the following code for checkbox :-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
Then, this would remember the checkbox of "PHP" if it is checked, even if the validation for the page fails and so on for "n" number of checkboxes as shown below:-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
HTML<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("HTML", $_POST["course"]))) {
echo "checked";
} ?> value="HTML" />
CSS<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("CSS", $_POST["course"]))) {
echo "checked";
} ?> value="CSS" />
Javascript<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("Javascript", $_POST["course"]))) {
echo "checked";
} ?> value="Javascript" />
And most importantly, do not forget to declare the "course" variable as an array at the start of the code as shown below :-
$course = array();
I have been battling how to create sticky check box (that is able to remember checked items any time you visit the page). Originally, I get my values from a database table. This means that my check box value is entered to a column on my db table.
I created the following code and it works just fine. I did not want to go through that whole css and deep coding, so...
CODE IN PHP
$arrival = ""; //focus here.. down
if($row['new_arrival']==1) /*new_arrival is the name of a column on my table that keeps the value of check box*/
{$arrival="checked";}// $arrival is a variable
else
{$arrival="";};
echo $arrival;
<b><label for ="checkbox">New Arrival</label></b>
<input type="checkbox" name ="$new_arrival" value="on" '.$arrival.' /> (Tick box if product is new) <BR><BR>
<input type="checkbox" name="somevar" value="1" <?php echo $somevar ? 'checked="checked"' : ''; ?>/>
Also, please consider sanitising your inputs, so instead of:
$somevar = $_POST['somevar'];
...it is better to use:
$somevar = htmlspecialchars($_POST['somevar']);
When the browser submits a form with a checked checkbox, it sends a variable with the name from the name attribute and a value from the value attribute. If the checkbox is not checked, the browser submits nothing for the checkbox. On the server side, you can handle this situation with array_key_exists(). For example:
<?php
$checkedText = array_key_exists('myCheckbox', $_POST) ? ' checked="checked"' : '';
?>
<input type="checkbox" name="myCheckbox" value="1"<?php echo $checkedText; ?> />
Using array_key_exist() avoids a potential array index undefined warning that would be issued if one tried to access $_POST['myCheckbox'] and it didn't exist.
You may add this to your form:
<input type="checkbox" name="mycheckbox" <?php echo isset($_POST['mycheckbox']) ? "checked='checked'" : "" ?> />
isset checks if a variable is set and is not null. So in this code, checked will be added to your checkbox only if the corresponding $_POST variable has a value..
My array has name="radioselection" and value="1", value="2", and value="3" respectively and is a radio button array... how to I check if the radio value is selected using this code
I tried:
<?php echo (isset($_POST['radioselection']) == '1'?'checked="checked"':'') ?> />