Jump to content

Recommended Posts

I'm trying to display an array in HTML and next to each record is a checkbox, as below. I then want to be able to select a record by checking the checkbox and posting the 
 
'value' of box2[ ] for that record. The problem I am having is the checkbox doesn't seem to post the value ($id), it only seems to handle static values (1,2,3 etc) instead of a string.
 
Any ideas how to work this, or suggesions for marking an individual record to then post it's id?
thanks
 
 
 
$linkid=dbconnect("db"); 
 
mysql_select_db("db");
 
if(!($result = mysql_query("SELECT * FROM table WHERE id = '$id'"))) die ("Result error");
{
echo "<table>\n";
 
 
while ($myrow = mysql_fetch_array($result)) 
    {
 
printf("<tr><td>%u</td><td><input type='checkbox' name='box2[ ]' value='$id'></td><td>%s</td><td>%s</td></tr>\n",
 
  $myrow["id"],$myrow["descr"],$myrow["batno"],"]);
 
    }
 
echo "</table>\n";
   
   
       }//endwhile
    }//endif 

 

Link to comment
https://forums.phpfreaks.com/topic/287711-checkbox-php-string-value/
Share on other sites

 

 

but the displayed list does display a value for $id

No it displays the value for $row['id']. You most likely need to substitute  $id in the printf with %1\$u  which which will refer to the first value in the list of values for printf (in your case $row['id'] ).

//              +-------------------- references $row['id'] --------------+
//              |                                                         |  
printf("<tr><td>%u</td><td><input type='checkbox' name='box2[ ]' value='%1\$u'></td><td>%s</td><td>%s</td></tr>\n",  // formatted string
       $myrow["id"],$myrow["descr"],$myrow["batno"]);  // list of values

 

 

no, the checkbox doesn't display a value

fastsol meant look at the actual HTML source code of your webpage to check to see if the value attribute for the checkboxes are being populated with a value.

 

Can you tell use the output of this

printf('<pre>%s</pre>', print_r($_POST, true));

If box2 does not show up in the array structure that statement produces when your form has been submitted then your checkboxes are not being included in same the <form></form tags as your other fields and this is why $_POST['box2'] is empty. In order for you to verify this you must check your HTML source code (right click view source) and check to make sure all <input> type tags are within your <form></form> tags.

Edited by Ch0cu3r

Great, nearly there?

 page source now shows $id values,

 

printf('<pre>%s</pre>', print_r($_POST, true));

 

returns -

 

Array
(
[box2] => Array
(
[0] => 5
)

I now have the value i want in box2 array.

 

Final problem I cant seem to the value out of box2 array?

 

eg $new_var = $box2[0];

echo $new_var;

 

doesnt work,

also 

 

echo count($box2); returns 0

 

I really need to get $box2[0] as a var, I've tried implode() but get invalid parameter error???

 

thanks

That is because you are using the wrong variable. The data is contained in the  $_POST['box2']  array, so you need assign that to $new_var, example

// form has been submitted
if($_SERVER['REQUEST_METHOD'] == 'POST'))
{
    // get the array of checkbox values
    $new_var = $_POST['box2'];

    // output the first item from the array
    echo $new_var[0];

    // Or display all values in a comma delimited list
    echo '<br />' . implode(',', $new_var);
}

Well, I now have value $box2[0] successfully stored as a $var. this will allow me to finish building the page and others that use similar.

 

seems my mistake was thinking $new_var = $_POST['box2']; made $new_var as a string but it's not, it's an array that can then be imploded.

 

I have learned a lot from this, but most of all how good ppl will hep you out if you get stuck. so many cheers for all the help and thanks for your patience.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.