I have a checkbox group in one file and the results that are ticked in another file. After I press submit the selected item shows up in the process file OK, and then the values go to the database.
But after I refresh the process page the selected values disappears. How can I stop this? I will need them to show on a user profile.
I have tried putting the two parts in the same file, but I get the same effect.
Here is the process.php
<?php
include("connection.php");
extract($_POST);
$check_exist_qry="select * from interests";
$run_qry=mysqli_query($con,$check_exist_qry);
$total_found=mysqli_num_rows($run_qry);
if ($total_found >0)
{
$my_value=mysqli_fetch_assoc($run_qry);
$my_stored_interests=explode(',',$my_value['interest_name']);
}
if (isset($submit))
{
$all_interests_value = implode(",",$_POST['interests']);
if ($total_found >0)
{
//update
$upd_qry="UPDATE interests SET interest_name='".$all_interests_value."'";
mysqli_query($con,$upd_qry);
}
else
{
//insert
$ins_qry="INSERT INTO interests(interest_name) VALUES('".$all_interests_value."')";
mysqli_query($con,$ins_qry);
}
}
if (!empty($_POST['submit'])) {
echo "<ul>";
foreach ($_POST['interests'] as $value) {
echo "<li>$value</li>";
}
echo "</ul>";
} else {
echo "none";
}
?>
<form action="checkbox1.php" method="post">
<table width="900">
<tr>
<td width="300"><label><h3><input type="checkbox" name="interests[]" value="option1" <?php if(isset($_POST['interests'])) { foreach($_POST['interests'] as $tmp) { if($tmp == "") { echo "checked=\"checked\"option1"; break; }}} ?>/> option1</h3></label></td>
<td width="300"><label><h3><input type="checkbox" name="interests[]" value="option2" <?php if(isset($_POST['interests'])) { foreach($_POST['interests'] as $tmp) { if($tmp == "") { echo "checked=\"checked\"option2"; break; }}} ?>/> option2</h3></label></td>
<td width="300"><label><h3><input type="checkbox" name="interests[]" value="option3" <?php if(isset($_POST['interests'])) { foreach($_POST['interests'] as $tmp) { if($tmp == "") { echo "checked=\"checked\"option3"; break; }}} ?>/> option3</h3></label></td>
</tr>
</table>
</form>