Jump to content
Larry Ullman's Book Forums

Problem Passing Checkbox Value Through Post


Recommended Posts

I have to pass selected checkbox value from one script to another. However, my checkbox selection never passes to the receiving page. My scripts are as follow:

 

 

function chkBox($str) {

echo "<div><input type='checkbox' name='member[]' value='$str'>" .ucfirst($str). "</div>";

 

}

 

$q = "SELECT * from users";

 

//Run the query:

$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

 

 

if(mysqli_num_rows($r) > 0)

{

//Asimple message to show:

 

//start a table with header row:

echo '<table align="center" cellspacing="10" cellpadding="10" width="80%"><tr class="rowH">

<th><a href="newbookentry.php">Add Book for</a></th> // user will click this link to go to receiving script

 

echo '<form action="newbookentry.php" method="post">

while($row = mysqli_fetch_row($r))

{

 

// list out member names in a horizontal row:

echo '<tr class="rowA">';

echo '<td>';

chkBox($row[2]);

// next things }

 

Second (receiving script): newbookentry.php

 

 

//check if a checkbox is selected:

if(isset($_POST['member']) && !empty($_POST['member'])) {

$mems = $_POST['member'];

//echo $mems;

} else {

$errors[] = 'Please select at least 1 member name.';

echo "Please select a member.";

}

 

// more

 

This script always shows error message, even if I select single/multiple checkboxes on first page.

Link to comment
Share on other sites

Checked that the query actually returns what you want from it? It's often the basic that fails.

 

Secondly. print_r()'s your friend. Your BEST friend. Use it for what it's worth. Carve this into your forehead and tattoo it on your arm:

 

echo '<pre>' , print_r($var) , '</pre>';

 

Use that on the $_POST array and the db result.

Link to comment
Share on other sites

In newbookentry.php, you could try something like

if (isset($_POST['member'])) {
 $members = implode(", ",$_POST['member']);
}
else {
 $members='Please select a member';
}
echo $members;

 

If the data is going to be stored in a d/b, I like to give each checkbox a unique name e.g. member1, member2 etc. Then you can loop through them to identify each checkbox specifically.

Link to comment
Share on other sites

Thanks for the reply, Antonio and Margaux.

 

The problem is I'm using link in between 2 scripts. If I use a submit button, then it works fine. So it looks like the form is submitted by "submit" button, and not by a hyper link.

 

( I would like to know how to do that with a link, though)

Link to comment
Share on other sites

 Share

×
×
  • Create New...