i have followed a tutorial for a working checkbox group after failed attempts with others.
On test file it was working but when i add it to my site i dont get any results after submitting form.
First of all here is the file from tutorial.

Sorry in advance for all the code.

<?php require_once('connection.php'); ?>
<?php
    if (isset($_POST['submit']))
    {
        if (isset($_POST['check_list']))
        {       
            $strcheck_list = implode(",", $_POST['check_list']);
        }
        else
        {   
            $strcheck_list = "";
        }
        echo "Interested in: " . $strcheck_list;
        exit();
    }
?>
<html>
<head>
<title>Checkboxes</title>
</head>
<body>
<form action="" name="check_list" method="post">
<table width="900">
<tr>
    <td width="225"><input type="checkbox" name="check_list[]" value="option1"><label>Option1</label></td>
    <td width="225"><input type="checkbox" name="check_list[]" value="option2"><label>option2</label></td>
    <td width="225"><input type="checkbox" name="check_list[]" value="option3"><label>option3</label></td>
    <td width="225"><input type="checkbox" name="check_list[]" value="option4"><label>option4</label></td>
</tr>
</table>
</form>
</body>
</html>

Which has the desired effect of showing what ive chosen.

But then i when i try to go live with it i create a interests file and add the following:

<?php 
include("config/db_connect.php");

// Check user id
if($_SESSION['userid']=="")
{
    header("location: index.php");
}

    if (isset($_POST['Submit']))
    {
    $insert = mysqli_query($conn,"update user_info set check_list = '".$_POST['check_list']."' where user_id = '".$_SESSION['last_id']."' ") or die(mysqli_error($conn));
    if($insert)
    {
        header("location:  profile.php");
    }

}

?>
<html>
<head>
<title>Checkboxes</title>
</head>
<body>
<form action="" name="check_list" method="post">
<table width="900">
<tr>
    <td width="225"><input type="checkbox" name="check_list[]" value="option1"><label>Option1</label></td>
    <td width="225"><input type="checkbox" name="check_list[]" value="option2"><label>option2</label></td>
    <td width="225"><input type="checkbox" name="check_list[]" value="option3"><label>option3</label></td>
    <td width="225"><input type="checkbox" name="check_list[]" value="option4"><label>option4</label></td>
</tr>
</table>
</form>
</body>
</html>

And on the profile.php i had the following:

<?php if (isset($_POST['check_list']))
        {       
            $strcheck_list = implode(",", $_POST['check_list']);
        }
        else
        {   
            $strcheck_list = "You didnt select any interests";
        }
        echo "Interested in: " . $strcheck_list;
        exit();?>

When i press submit i get You didnt select and interests
and when i go to the database i get array

any help would be much appreicated

Recommended Answers

All 13 Replies

I believe you capitalized "Submit" on line 10:

`    if (isset($_POST['Submit']))`

Make that lower case, and see if that helps.

ok hun didnt realise lol

changed S to lowercase s but its still not echoing the results through just echos You didnt select any interests

Im very confused as to how these pages are related....

On the one page, you have a form that posts to itself (the same page), and inserts into the database.

On the other, you have a page that is expecting a post from said form - and when it's not there, it echoes "You didn't select any interests."

When exactly do you post (or submit a form) to "profile.php"?

what i did was add a header location link from interests.php to the profile.php so if checkbox group had a selection it would header to profile.php

of which it is doing but its echoing "You didn't select any interests." in the profile.php

i have this in the interests.php where the checkboxes are located

if (isset($_POST['submit']))
    {
    $insert = mysqli_query($conn,"update user_info set check_list = '".$_POST['check_list']."' where user_id = '".$_SESSION['last_id']."' ") or die(mysqli_error($conn));
    if($insert)
    {
        header("location:  profile.php");
    }
    if (isset($_POST['check_list']))
        {       
            $strcheck_list = implode(",", $_POST['check_list']);
        }
        else
        {   
            $strcheck_list = "You didnt select any interests";
        }
        echo "Interested in: " . $strcheck_list;
        exit();
}

?>

and this bit in the profile.php where the selection is supposed to echo through

<?php if (isset($_POST['check_list']))
        {       
            $strcheck_list = implode(",", $_POST['check_list']);
        }
        else
        {   
            $strcheck_list = "You didnt select any interests";
        }
        echo "Interested in: " . $strcheck_list;
        exit();?>

Ahh..

It has been a while but if I recall, header() does not implicitly pass POST variables to the redirect.

Since you are using a database, why not use it as it is supposed to be used? Once it is saved, it is ready for consumption. Your "profile.php" page should remember the user (SESSION['last_id']), and should make a call to the database and retrieve what it just saved. While that seems kinda rediculous (because, you have the data why not just use it right?), this will allow you to 1) Debug your database and make sure the insert works, 2) have a re-usable profile page that can be accessed any time you know the user ID / session ID.

Just my 2 cents there.. I'm sure if I am misinformed about HEADER/Redirects not passing POST data, someone will correct me.. but Im pretty sure that's still the case.

Good luck!

Ryan

Ive just took advice and removed the header location profile.php and put profile.php in action and left rest of php code like.

<?php
include("config/db_connect.php");

// Check user id
if($_SESSION['userid']=="")
{
    header("location: index.php");
}
    if (isset($_POST['Submit']))
    {
        if (isset($_POST['check_list']))
        {       
            $strcheck_list = implode(",", $_POST['check_list']);
        }
        else
        {   
            $strcheck_list = "";
        }
        echo "Interested in: " . $strcheck_list;
        exit();
    }
?>

Then in profile.php i have put the following in to fetch interests as same as other items that have been fetched.

<div class="clr"></div>
      <div class="profl_contnt">
      <?php echo $fetch_info['check_list'];?>
     <div class="clr"></div>

Then ive left the top bit alone see below:

$info_query     = mysqli_query($conn,"select * from user_info where user_id = '".$fetch_user['user_id']."' ");
$fetch_info     = mysqli_fetch_array($info_query); 

and when i click submit i get array where the chosen interests go.

Good? I think... ?

So did this solve your problem?

now when i fill in the form i get :

Interests
Array

on profile.php hun

So you are getting the result of this line:

<?php echo $fetch_info['check_list'];?>

The value in there is an array. Instead of echo, try var_dump($fetch_info['check_list']); to see how you need to iterate or consume.

sorted it i replaced

<?php echo $fetch_info['check_list'];?>

with

<?php
      if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}
}
?>

And its working ty for all your help guys x

thought it was fixed but when i refresh the selection goes blank so then i went to put that link in ie
var_dump($fetch_info['check_list']);

i get string(5) "Array"

Refresh may have lost your post data. Any number of things can be causing you trouble.

Without seeing all of your code and knowing our work-flow, it's impossible to give you a better answer that piece meal like I have been.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.