John Davidson

php - How to repopulate multiple select boxes and checkboxes

0 comments
Message:


I cannot find a way that works to re-populate multiple select boxes, Since it is stored as an array is there a way to cross-check and repopulate the selections that were made? I am trying to answer question 4 from the comments section of the code.
The section of the form that I am trying to repopulate is the like_bands multiple select box


my code:


    <?php
session_start();

/*
This page should:

1) Use a cookie (NOT PHP SESSION) that expires a year from now to do the following:
Record the timestamp of the FIRST load of this page (but NOT any subsequent load of this page).

Note: You only need first 3 parameters in setcookie() for this HW.

2) If the suvey was already completed, transfer to third page (thank you page).
That page instructs you to set a cookie that will allow you to detect this.

Recall from the CRUD example how page transfers are done in PHP:
header("Location: URL");

3) The form in this page should submit to THIS PAGE, and then transfer to hw_page2.php
after saving stuff to PHPs built-in $_SESSION superglobal.
The data will then be available in all the other pages (remember no database is allowed for this HW).

4) If the button in the 2nd page is clicked to come back to this page, the
Session data should re-populate into the form.


*/

if (!isset($_COOKIE['load_date'])) {
setcookie('load_date', time(), time() + (86400 * 365));
}

$load_date = $_COOKIE['load_date'];
$is_cool = $_POST['is_cool'];
var_dump($is_cool);
$bands = $_POST['like_bands'];
$band_list = "";
foreach ($bands as $band) {
$band_list .= $band. ", ";
}
$other_band = $_POST['other_band'];
echo $other_band;
$_SESSION['is_cool'] = $is_cool;
$_SESSION['bands'] = $band_list;
$_SESSION['other_band'] = $other_band;
?>

<!DOCTYPE html>
<html>
<head>
<title>User Profile</title>
</head>
<body>
<h3><?=$message?></h3>

User Profile:
<br><br>
<form action="hw_page1.php" method="POST" name="form1" onsubmit="return validate_form()">
<input type="hidden" name="task" value="process_profile">
<?php if ($is_cool != null) {?>
<input type="checkbox" name="is_cool" value="yes" checked='checked'>
Are you cool?
<?php } else { ?>
<input type="checkbox" name="is_cool" value="yes">
Are you cool?
<?php }?>
<br><br>
What Bands do you like?
<br>
<select name="like_bands[]" multiple> <small>* Required Field</small>
<option value="Sabbath">Black Sabbath</option>
<option value="Mastodon">Mastodon</option>
<option value="Metallica">Metallica</option>
<option value="Swift">Taylor Swift</option>
</select>
<br><br>
Favorite band not in the above list.
<br>
<input type="text" name="other_band" value="<?=$other_band?>">
<br><br>
<button type="submit"> Continue/Confirm </button>
</form>

<script>
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Client-side form validation
// Don't change the names of stuff in the form, and you won't have to change anything below
// Used built-in JS instead of JQuery or other validation tool
//////////////////////////////////////////////////////////////////////////////////////////////////////////
function validate_form() {

var form_obj = document.form1;

var count_liked = 0;
for (var i=0 ; i < form_obj['like_bands[]'].options.length ; i++ ) {
if (form_obj['like_bands[]'].options[i].selected) {
count_liked++;
}
}

if (count_liked == 0) {
alert("You must choose a band from the menu.");
return false; // cancel form submission
}

return true; // trigger form submission
}
</script>
<?php
/*if (isset($_POST['like_bands'])) {
header('Location: hw_page2.php');
}*/
?>

</body>
</html>


My Answer:
To repopulate the multiple select box and checkboxes with the values that were previously selected, you can modify the HTML code to check if the values exist in the session data and mark them as selected or checked accordingly. Here's how you can do it:

1. For the multiple select box:
html



2. For the checkbox:
html
>


Make sure to update the PHP code to handle the session data properly and initialize the session at the beginning of the script. Also, ensure that the session data is properly stored and retrieved when the form is submitted.

Additionally, you may need to modify the validation logic to handle the repopulated values correctly.

Rate this post

4 of 5 based on 7478 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us