0

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; }}} ?>/>&nbsp;&nbsp;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; }}} ?>/>&nbsp;&nbsp;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; }}} ?>/>&nbsp;&nbsp;option3</h3></label></td>
</tr>
</table>
</form>
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102

2 Answers2

0

use <?php if(in_array("option1", $_POST['interests'])) echo "checked"; ?> http://www.w3schools.com/php/func_array_in_array.asp check out the tutorial for more info..

in your usage

<td width="300"><label><h3><input type="checkbox" name="interests[]" value="option3" <?php if(in_array("option3", $_POST['interests'])) echo "checked"; ?> />&nbsp;&nbsp;option3</h3></label></td>
ameenulla0007
  • 2,663
  • 1
  • 12
  • 15
0

You can verify and set the checkbox using the following logic:

<?php 
    if(isset($_POST['interests']) && in_array("option1", $_POST['interests'])){
        echo " checked='checked'";
    }
?>

Here's the reference:

So your code should be like this:

// your code

<tr>
<td width="300"><label><h3><input type="checkbox" name="interests[]"  value="option1" <?php if(isset($_POST['interests']) && in_array("option1", $_POST['interests'])){ echo " checked='checked'"; } ?>/>&nbsp;&nbsp;option1</h3></label></td>
<td width="300"><label><h3><input type="checkbox" name="interests[]" value="option2" <?php if(isset($_POST['interests']) && in_array("option2", $_POST['interests'])){ echo " checked='checked'"; } ?>/>&nbsp;&nbsp;option2</h3></label></td>
<td width="300"><label><h3><input type="checkbox" name="interests[]" value="option3" <?php if(isset($_POST['interests']) && in_array("option3", $_POST['interests'])){ echo " checked='checked'"; } ?>/>&nbsp;&nbsp;option3</h3></label></td>
</tr>

// your code
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • i have put the code in above when i put this in a click submit and the values show on process.php like below option1 option2 option3 but if i click refresh on process.php instewad of option1, option2, option3 i get none – janice robinson Jan 30 '16 at 21:14
  • @janicerobinson *...like below* ? What's the problem now? – Rajdeep Paul Jan 30 '16 at 21:16
  • i have put the code in above when i put this in a click submit and the values show on process.php like below option1 option2 option3 but if i click refresh on process.php instewad of option1, option2, option3 i get none – janice robinson Jan 30 '16 at 21:24
  • @janicerobinson That's what it does. When you hit submit, it shows everything as expected because `$_POST` variables are all set. And when you refreshes the page again, all the `$_POST` variables will get lost. For your purpose, store the values in session variables and then crosscheck against checkbox values. – Rajdeep Paul Jan 30 '16 at 21:34
  • just googled set variables and came to w3 schools and before i do the set variables being as there is more than one option do i do it like this hun $_SESSION["interests"] = "option1, option2, option3"; – janice robinson Jan 31 '16 at 18:46
  • @janicerobinson [Official documentation](http://php.net/manual/en/reserved.variables.session.php) is always a good place to start. In addition, refer [this SO question](http://stackoverflow.com/questions/5489365/how-to-use-store-and-use-session-variables-across-pages). Or do a proper Google search, [set session variable in php](https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=set%20session%20variable%20in%20php) – Rajdeep Paul Jan 31 '16 at 19:02
  • Ty hun for your help n guidance – janice robinson Jan 31 '16 at 19:40