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.
Related
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 (||).
I've made a form with quite a few fields. My old captcha outdated, so I had to use a new one (where I choose Google's reCaptcha). With the old captcha, then if people filled it out wrong, then they could click 'Back' or on this link (that I put on the handling-data.php-page):
go back and try again
... and then all their answers in the fields, would still be there. But after I've changed it to Google's reCaptcha, then if people forget to click the 'I'm not a robot'-tick-box and submit the form; - then if they go back, then all the fields are empty.
So how do I fix this?
I was thinking of passing the $_POST-array to a $_SESSION-variable. So if they click back, then the HTML-fields would look something along these lines:
<input type="text" name="foobar" placeholder="foobar"
value="<?php echo $_SESSION['foobar']; ?>" />
But is that really the way to do it? I can't be the first one to stumble across this. :-/
Upon processing the $_POST data:
session_start();
$_SESSION["postvars"] = $_POST;
Then on the form page:
session_start();
<input type="text"
name="foobar"
placeholder="foobar"
value="<?php echo (isset($_SESSION["postvars"]) ? $_POST["postvars"]["foobar"] : ""); ?>>
For dropdowns:
<select name="barfoo">
<option value="foo" <?php echo (isset($_SESSION["postvars"]) ? ($_SESSION["postvars"]["barfoo"]=="foo" ? "selected" : "") : "" ); ?>
<option value="bar" <?php echo (isset($_SESSION["postvars"]) ? ($_SESSION["postvars"]["barfoo"]=="bar" ? "selected" : "") : "" ); ?>
<option value="example" <?php echo (isset($_SESSION["postvars"]) ? ($_SESSION["postvars"]["barfoo"]=="example" ? "selected" : "") : "" ); ?>
</select>
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 have a form that i pull data from the database and fill in all the fields for a user to edit an existing record. It looks like this currently to set the radio button:
<input type="radio" id="statusActive" value="1" name="status" <?php if ($departmentData->thresholdActive == "1"){ echo 'checked'; }else{ echo ''; } ?>> Active
What would be the Ternary Logic approach to say:
If POST is true, us the post data else use the one from the database?
<input type="radio"
id="statusActive"
value="1"
name="status"
<?php echo ($departmentData->thresholdActive == "1") ? 'checked' : ''; ?>
>
A ternary expression using the following
if ($departmentData->thresholdActive == "1")
{
echo 'checked';
}
else
{
echo '';
}
Would be
echo $departmentData->thresholdActive == "1" ? 'checked' : '';
"Ternary logic" - you probably want
<?php echo ($departmentData->thresholdActive == "1")? 'checked':'';?>
But if you first want to check whether POST is true, it would be cleaner to so something like
(isset($_POST))?$_POST['index'] : $dbValue;
Where 'index' refers to a specific post variable (which I assume is present when POST is set; you didn't specify whether to check for a specific POST variable so it's hard to guess); and assumes that you know how to set $dbValue with a "value from the database"
I'm new to PHP and trying to write code to test whether or not a user has clicked a radio button in response to a survey question. There are numerous radio buttons. If they haven't clicked on one then I'd like to issue an error to the user. I've tried a couple of approaches, but haven't found anything that works. Here is my current code and the error message I get. For the PHP script, I've tried all of the three following examples:
....
if ($_POST['degree_type'] == "MS"||"MA"||"MBA"||"JD"||"PhD") {
$degree_type = ($_POST['degree_type']);
} else if ($_POST['degree_type'] == null) {
$errors[] = 'Please select a degree type.';
}
if (isset($_POST['degree_type'])) {
$errors[] = 'Please select a degree type.';
} else {
$degree_type= $_POST['degree_type'];
}
if (array_key_exists('degree_type', $_POST)) {
$degree_type = ($_POST['degree_type']);
} else {
$errors[] = 'Please select a degree type.';
}
....
Here is my html, located in the same page and below the PHP.
<table>
<tr>
<td class="span6">What type of degree?</td>
<td class="span6">
<input type="radio" name="degree_type" value="MA"
<?php if (($_POST['degree_type']) == 'MA') {echo 'checked="checked"';} ?>
>MA
<input type="radio" name="degree_type" value="MS"
<?php if (($_POST['degree_type']) == 'MS') {echo 'checked="checked"';} ?>
>MS
<input type="radio" name="degree_type" value="MBA"
<?php if (($_POST['degree_type']) == 'MBA') {echo 'checked="checked"';} ?>
>MBA
<input type="radio" name="degree_type" value="JD"
<?php if (($_POST['degree_type']) == 'JD') {echo 'checked="checked"';} ?>
>JD
</td>
</tr>
ETC....
I get an "undefined index" error on each of the HTML lines referencing a radio button. I understand this might be easier to do in JavaScript, but I don't know much about JS... A detailed response would be much appreciated!
Thanks!
If you're getting an undefined error on the HTML page, just can add an isset() check to the logic where you're printing out the value. E.g.:
<input type="radio" name="degree_type" value="JD" <?php if (($_POST['degree_type']) == 'JD') {echo 'checked="checked"';} ?> >JD
Becomes
<input type="radio" name="degree_type" value="JD" <?php if (isset($_POST['degree_type']) && $_POST['degree_type'] == 'JD') {echo 'checked="checked"';} ?>>JD
An 'undefined index' error in PHP means that you are using an undefined variable in an expression. So for example, when you did:
<?php if (($_POST['degree_type']) == 'MA') {echo 'checked="checked"';} ?>
$_POST['degree_type'] was undefined. There's a couple of different possible reasons why the variables are undefined. I'd have to see the rest of the PHP file to know the exact cause.
One reason could be that the form was not properly submitted. Another reason could be that the expression was evaluated before the form was submitted.
Either way, the code below should work. Note that I'm checking if each field is set before attempting to validate it or compare it's value.
NOTE:
Obviously you have to have a proper HTML doctype, opening and closing body tags etc. The HTML in this example is only the form portion of the page.
<!-- myform.php -->
<form name="my-form" method="POST" action="/myform.php">
<span>What degree do you have?</span>
<label for="bs">BS</label>
<input type="radio" name="degree" id="bs" value="BS" <?php if(isset($degree) && $degree == 'BS') echo 'checked="checked"';?> />
<label for="ma">MA</label>
<input type="radio" name="degree" id="ma" value="MA" <?php if(isset($degree) && $degree == 'MA') echo 'checked="checked"';?> />
<label for="phd">PHD</label>
<input type="radio" name="degree" id="phd" value="PHD" <?php if(isset($degree) && $degree == 'PHD') echo 'checked="checked"';?> />
<span>Which do you like better?</span>
<label for="steak">steak</label>
<input type="radio" name="food" id="steak" value="steak" <?php if(isset($food) && $food == 'steak') echo 'checked="checked"';?> />
<label for="lobster">lobster</label>
<input type="radio" name="food" id="lobster" value="lobster" <?php if(isset($food) && $food == 'lobster') echo 'checked="checked"';?> />
<input type="hidden" name="submitted" value="submitted" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
$errors = array();
if (isset($_POST['degree'])) {
$degree = $_POST['degree'];
} else {
$errors[] = 'Please select your degree type.';
}
if (isset($_POST['food'])) {
$food = $_POST['food'];
} else {
$errors[] = 'Please select your food preference.';
}
if (count($errors) > 0) {
foreach($errors as $error) {
echo $error;
}
} else {
echo 'Thank you for your submission.';
}
}
?>
The reason you see these notices is because $_POST['degree_type'] is simply not set. Either by typo, or it just didn't get submitted (because you didn't select any before submitting the form).
Also note,
if ($_POST['degree_type'] == "MS"||"MA"||"MBA"||"JD"||"PhD") {
It doesn't work that way. This will check that $_POST['degree_type'] == "MS" OR: "MS" is truthy (always true) OR "MA" is truthy (always true)... see where I'm heading?
if (in_array($_POST['degree_type'], array("MS", "MA", "MBA", "JS", "PhD")) {
Is a better alternative.
Unrelated:
You should really use <label> elements to markup your labels. Example:
<label><input type="radio" name="degree_type" value="MA"> MA</label>.
This will have MA clickable.
When a form is submitted with no member of a radio button group (defined as the group of radio buttons whose name attributes are the same) selected, the submitted data doesn't include that name at all.
This is why you're getting the "undefined index" error (actually a notice); when you test the value of $_POST['degree_type'], and no radio button named "degree_type" was selected, $_POST['degree_type'] doesn't exist at all.
Fortunately, this simplifies your validation task. By calling array_key_exists('degree_type', $_POST), you can find out whether or not the key is present, and thus whether or not a radio button was selected, without prompting the PHP "undefined index" notice. If the function call returns true, you know that a radio button was selected; otherwise, you know one wasn't, and that's what your validation is trying to determine. Therefore:
if (array_key_exists('degree_type', $_POST)) {
$degree_type = $_POST['degree_type'];
}
else {
array_push($errors, "Please select a degree type.");
};
will cleanly accomplish your task.
//set a default
$degree_type = "";
if (isset($_POST['degree_type'])) {
$degree_type = $_POST['degree_type'];
} else {
$errors[] = 'Please select a degree type.';
}
Then instead of using
if (($_POST['degree_type']) == 'MA')
for your checks, use:
if($degree_type == 'MA')
undefined index means that the key you are using hasn't been initialized. So $_POST['degree_type'] won't appear until after the first time the form is submitted.