How to remember flag entry in PHP forms

For ease of use, I like to create 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>

Thus, if the user fails in the validation, the previously entered form input will still be there, and there is no need to start from scratch.

My problem is that I cannot save the checkboxes selected with the option the user selected earlier (when the page refreshes after the check is completed). How to do it?

+3
source share
7 answers

My first suggestion was to use some client-side validation first. Perhaps an AJAX call that performs validation checks before proceeding.

, :

<input type="checkbox" name="subscribe" <?php echo (isset($_POST['subscribe'])?'checked="checked"':'') ?> />

, subscribe is = 1, .

+5

: -

<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" />

"PHP", , "n" , : -

<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" />

, "" , : -

$course = array();
+2

, ( , ). . , db.

, . , ...

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>&nbsp;&nbsp;&nbsp;
<input type="checkbox" name ="$new_arrival" value="on" '.$arrival.' /> &nbsp;(Tick box if product is new) <BR><BR>
+1
<input type="checkbox" name="somevar" value="1" <?php echo $somevar ? 'checked="checked"' : ''; ?>/>

, , :

$somevar = $_POST['somevar'];

... :

$somevar = htmlspecialchars($_POST['somevar']);
0

, name value. , . array_key_exists(). :

<?php
$checkedText = array_key_exists('myCheckbox', $_POST) ? ' checked="checked"' : '';
?>
<input type="checkbox" name="myCheckbox" value="1"<?php echo $checkedText; ?> />

array_key_exist() undefined, , $_POST['myCheckbox'], .

0

:

<input type="checkbox" name="mycheckbox" <?php echo isset($_POST['mycheckbox']) ? "checked='checked'" : "" ?> />

isset , ​​ . , checked , $_POST .

0
source

My array has name="radioselection"both value="1", value="2"and, value="3"accordingly, is an array of switches ... how to check if the radio value is selected using this code I tried:

<?php echo (isset($_POST['radioselection']) == '1'?'checked="checked"':'') ?> />
0
source

Source: https://habr.com/ru/post/1737951/


All Articles