Im trying to save $_SESSION data to keep checked values on page refresh, I have some code written up but I am missing a piece.
Session Code:
session_start();
if(isset($_POST['submit'])) {
//user posted variable
$checks = $_POST['personalization_result'];
if(isset($_POST['personalization_result'])) {
$_SESSION['value'] = $_POST['personalization_result']; }
else {
$_SESSION['value'] = '';
}
}
Form Code:
<form action="" method="post" id="question-form">
<input type="hidden" name="submit" value="1">
<li>
<input name="personalization_result[memory_0]" type="hidden" class="<?php echo $_SESSION['value']['memory_0'] ?>" value="0" />
<input name="personalization_result[memory_0]" type="checkbox" value="1" id="personalization_result_memory_0" />
</li>
<li>
<input name="personalization_result[memory_1]" type="hidden" class="<?php echo $_SESSION['value']['memory_1'] ?>" value="0" />
<input name="personalization_result[memory_1]" type="checkbox" value="1" id="personalization_result_memory_1" />
</li>
//There are many checkboxes, this is just two for demo purposes
<input class="submit" type="submit" value="Send" />
</form>
For testing I am echoing $_SESSION['value']['memory_0'] && $_SESSION['value']['memory_1']inside the hidden input, they return properly, if the input is checked the class is 1, if it's unchecked the class is 0.
I am not sure the best way to say if it = 1 then echo checked="checked"
My shotty attempt:
<input name="personalization_result[memory_0]" type="hidden" class="<?php echo $_SESSION['value']['memory_0'] ?>" value="0" />
<input name="personalization_result[memory_0]" type="checkbox" value="1" id="personalization_result_memory_0" <?php if($_SESSION['value']['memory_0'] = 1) {echo 'checked="checked"';} ?> />
This returns an error "Cannot use a scalar value as an array", that and I dont like how thats written up.
So with all the given information, whats the best way to write if my value = 1 return checked="checked"?
This will make your PHP much cleaner and scalable:
Use this function at the top:
session_start();
function checkbox($id)
{
$isChecked = '';
if (isset($_SESSION['value']['memory_'.$id]))
{
$isChecked = ($_SESSION['value']["memory_".$id] == 1) ? 'checked="checked"' : '';
}
echo '<input name="personalization_result[memory_'.$id.']" type="checkbox" ';
echo 'value="1" id="personalization_result_memory_'.$id.'" ';
echo $isChecked.' />';
}
and then instead of writing out <input ... <?php ... ?> ... /> for every single checkbox, just use
<?php checkbox(0); ?>
<?php checkbox(1); ?>
Your short attempt has an error:
instead of <?php if($_SESSION['value']['memory_0'] = 1) try <?php if($_SESSION['value']['memory_0'] == 1)
Related
I'm new to wordpress and I have a question
How can I save the checkbox values on all wordpress pages for the current user? For all other users, the code does not apply.
I have a code
<form method="POST">
Option One: <input type="checkbox" name="check1" value="1" <?php
if(isset($_POST['check1'])) echo 'checked="checked"'; ?> />
Option Two: <input type="checkbox" name="check2" value="2" <?php
if(isset($_POST['check2'])) echo 'checked="checked"'; ?> />
<br>
<input type="submit" value="Submit" />
</form>
`<?php
if(isset($_POST['check1'])){
echo '22222';
}
elseif(isset($_POST['check2'])){
echo '3455';
}
?>`
but when you go to another page, everything is reset
how do i fix it?
Tell me please
You might work with php session or javascript localstorage.
Working with tabs will be a right solution, or else, storage right here
session_start();
if(isset($_POST['check'])){
$_SESSION['customer_values']['check_1'] = "VALUE1";
}
if(isset($_POST['check2'])){
$_SESSION['customer_values']['check_2'] = "VALUE2";
}
I do not know why but the code in the comments is buggy
Therefore, I will write it to him here
<form method="POST">
Option One: <input type="checkbox" name="check1" value="1" <?php if ( isset($_SESSION
['customer_values'] ['check1'] ) ) echo 'checked = "checked" '; ?> />
Option Two: <input type="checkbox" name="check2" value="1" <?php if ( isset
($_SESSION ['customer_values'] ['check2'] ) ) echo 'checked = "checked" '; ?> />
<br>
<input type="submit" value="Submit" />
</form>
<?php
session_start();
if(isset($_POST['check1'])){
$_SESSION['customer_values']['check1'] = "VALUE1";
}
if(isset($_POST['check2'])){
$_SESSION['customer_values']['check2'] = "VALUE2";
}
?>
You had this code, if yes then for some reason it does not work
This is one of those ones that probably falls between PHP and SQL stuff.
Basically I have a page where some update fields are displayed depending on who is logged, for example:
<?php if ($row_Users['UserID']=="101"){ ?>
<input <?php if (!(strcmp($row_lodges['101_finalist'],"Yes"))) {echo "checked=\"checked\"";} ?> type="checkbox" name="101_finalist" value="Yes"/>
<input type="text" class="rankfield" name="101_rank" value="<?php echo($row_lodges['101_rank']); ?>" />
<?php } ?>
<?php if ($row_Users['UserID']=="102"){ ?>
<input <?php if (!(strcmp($row_lodges['102_finalist'],"Yes"))) {echo "checked=\"checked\"";} ?> type="checkbox" name="102_finalist" value="Yes"/>
<input type="text" class="rankfield" name="102_rank" value="<?php echo($row_lodges['102_rank']); ?>" />
<?php } ?>
The issue I have is that if User101 is logged in and updates fields 101_finalist and 101_rank, it overwrites 102_finalist and 102_rank with an empty string.
Is it possible to prevent each user from seeing the other fields for other users, and prevent the existing values for those other users not be overwritten?
Hope that makes sense!
Thank you.
You'll want to do something like this. It's basically what #taco is saying:
<?php
// Set's the default user logged in
$_def = ($row_Users['UserID'] == "101")? '101': '102'; ?>
<input <?php if (!(strcmp($row_lodges[$_def.'_finalist'],"Yes"))) { ?>checked="checked"<?php } ?> type="checkbox" name="<?php echo $_def; ?>_finalist" value="Yes"/>
<input type="text" class="rankfield" name="<?php echo $_def; ?>_rank" value="<?php echo ($_def == '101')? $row_lodges['101_rank']:$row_lodges['JA_rank']; ?>" />
You can do something like
<?php
$userid = $row_Users['UserID'];
$finalist = $userid."_finalist";
$rank = $userid."_rank";
?>
<input <?php if (!(strcmp($row_lodges[$finalist],"Yes"))) {echo "checked=\"checked\"";} ?> type="checkbox" name="101_finalist" value="Yes"/>
<input type="text" class="rankfield" name="101_rank" value="<?php echo($row_lodges[$rank]); ?>" />
Im trying to add a class after a form is submitted depending on the option the user chose.
<form enctype="multipart/form-data" action="upload.php" method="POST">
title:<br />
<input type="text" name="title" value="" /> <br />
description:<br />
<input type="text" name="description" value="" /> <br />
Categories:<br />
<input type="checkbox" name="categories[]" value="1">Landscpae<br />
<input type="checkbox" name="categories[]" value="2">Portrait<br />
<input type="checkbox" name="categories[]" value="3">Monochrome<br />
Please choose an image: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
<li class='INSERT OPTION HERE'> test </li>
With the option to select more than one class so add a space after each one.
on my database the Value="1" "2" "3" == the option name
let me know if more information is needed
Something along these lines might work:
$valid = array(1,2,3);#valid items
if(isset($_POST['categories']) && is_array($_POST['categories'])){
$post = array_unique($_POST['categories']);#you might not need this, but this removes duplicate values
foreach($post as $key => $value){
if(in_array($value, $valid)){
$class = true;
}else{
unset($post[$key]);#remove bad values, also this may not be necessary but removes unwanted items.
}
}
if(isset($class)){
$class = 'class="class' . implode(' class', $post) . '"';
}
}#else needed for non array categories?
<li <?php if(isset($class)){ echo $class;} ?> > test</li>
DEMO
You want to make sure that the form is submitted, and if it is then you echo a class and can create a switch statement that will alter the class depending on what was selected.
<li
<?php
if(isset($_POST['categories'])) {
echo 'class="';
switch($_POST['categories']) {
case 1;
echo 'class1';
break;
case 2;
echo 'class2';
break;
case 3;
echo 'class3';
break;
echo '"';
}
}
?>
> test </li>
HI
i'm using a php page and i need to keep the value of and check box and radio button (checked or not checked) after post page.
how could i make it?
thanks
First get the radio button value.
$radiobuttonvalue = $_POST['radiobuttoname']
Then for each radio button with the same name, do this
<input type="radio" name="radiobuttonname" value="value" id="radiobuttonname" <?php if($radiobuttonvalue == "value") { echo 'checked="checked"';} ?>
You need something like:-
<?php
$postCheckboxName = '';
if (isset($_POST['checkbox_name']) || 'any_value' == $_POST['checkbox_name']) {
$postCheckboxName = ' checked="checked"';
}
?>
<input type="checkbox" name="checkbox_name" value="any_value"<?php echo $postCheckboxName;?> />
<?php
$postRadioName = '';
if (isset($_POST['radio_name']) || 'any_other_value' == $_POST['radio_name']) {
$postRadioName = ' checked="checked"';
}
?>
<input type="checkbox" name="radio_name" value="any_other_value"<?php echo $postRadioName;?> />
This code should get you going. I'm basically checking whether the POST value of either the checkbox / radio element is set or not & whether the corresponding element's value matches with my respective element's value or not.
Hope it helps.
Something like this:
<?php if (isset($_POST['checkbox_name']))?>
<input type="checkbox" checked="checked" value="<?php echo $_POST['checkbox_name'];?>" />
<?php} ?>
<?php if (isset($_POST['radio_name']))?>
<input type="radio" checked="checked" value="<?php echo $_POST['radio_name'];?>" />
<?php} ?>
What happens is that you check if the input variables are in the $_POST and if so you add checked="checked" to the input fields to make them checked.
This worked for me, and is self explanatory
sample code usage:
<div class="form-group">
<label class="radio-inline">
<input type="radio" name="time" value="lunch" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='lunch' ){echo ' checked="checked"';}?>>Lunch</label>
<label class="radio-inline">
<input type="radio" name="time" value="dinner" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='dinner' ){echo ' checked="checked"';}?>>Dinner</label>
</div>
How do I check in PHP whether a checkbox is checked or not?
If the checkbox is checked, then the checkbox's value will be passed. Otherwise, the field is not passed in the HTTP post.
if (isset($_POST['mycheckbox'])) {
echo "checked!";
}
you can check that by either isset() or empty() (its check explicit isset) weather check box is checked or not
for example
<input type='checkbox' name='Mary' value='2' id='checkbox' />
here you can check by
if (isset($_POST['Mary'])) {
echo "checked!";
}
or
if (!empty($_POST['Mary'])) {
echo "checked!";
}
the above will check only one if you want to do for many than you can make an array instead writing separate for all checkbox try like
<input type="checkbox" name="formDoor[]" value="A" />Acorn Building<br />
<input type="checkbox" name="formDoor[]" value="B" />Brown Hall<br />
<input type="checkbox" name="formDoor[]" value="C" />Carnegie Complex<br />
php
$aDoor = $_POST['formDoor'];
if(empty($aDoor))
{
echo("You didn't select any buildings.");
}
else
{
$N = count($aDoor);
echo("You selected $N door(s): ");
for($i=0; $i < $N; $i++)
{
echo htmlspecialchars($aDoor[$i] ). " ";
}
}
Try this
index.html
<form action="form.php" method="post">
Do you like stackoverflow?
<input type="checkbox" name="like" value="Yes" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
form.php
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['like']))
{
echo "<h1>You like Stackoverflow.<h1>";
}
else
{
echo "<h1>You don't like Stackoverflow.</h1>";
}
?>
</body>
</html>
Or this
<?php
if(isset($_POST['like'])) &&
$_POST['like'] == 'Yes')
{
echo "You like Stackoverflow.";
}
else
{
echo "You don't like Stackoverflow.";
}
?>
If you don't know which checkboxes your page has (ex: if you are creating them dynamically) you can simply put a hidden field with the same name and 0 value right above the checkbox.
<input type="hidden" name="foo" value="0" />
<input type="checkbox" name="foo" value="1">
This way you will get 1 or 0 based on whether the checkbox is selected or not.
I love short hands so:
$isChecked = isset($_POST['myCheckbox']) ? "yes" : "no";