Gavski Posted April 12, 2014 Share Posted April 12, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/287711-checkbox-php-string-value/ Share on other sites More sharing options...
requinix Posted April 12, 2014 Share Posted April 12, 2014 How about posting the version that doesn't work? What kind of string value and what are you actually receiving in your PHP? Quote Link to comment https://forums.phpfreaks.com/topic/287711-checkbox-php-string-value/#findComment-1475861 Share on other sites More sharing options...
Gavski Posted April 12, 2014 Author Share Posted April 12, 2014 This doesn't work?? when a checkbox is checked, and the form posted, the posted value $_POST['box2'] is null, I am expecting it to hold the value $id for the record associated wit it? Quote Link to comment https://forums.phpfreaks.com/topic/287711-checkbox-php-string-value/#findComment-1475863 Share on other sites More sharing options...
Ch0cu3r Posted April 12, 2014 Share Posted April 12, 2014 Are you sure the checkboxes are included in your form? Also you have a malformed printf statement, what is the ,"] just before the closing parenthesise for? Quote Link to comment https://forums.phpfreaks.com/topic/287711-checkbox-php-string-value/#findComment-1475865 Share on other sites More sharing options...
Gavski Posted April 12, 2014 Author Share Posted April 12, 2014 sorry, I simplified the form to post it on here and the ,"] were the end of another set of values. The check box's do appear ok, one along side each record that is displayed but when I tick a check box the box2[ ] array fails to post the 'value' $id. Quote Link to comment https://forums.phpfreaks.com/topic/287711-checkbox-php-string-value/#findComment-1475872 Share on other sites More sharing options...
Gavski Posted April 12, 2014 Author Share Posted April 12, 2014 also, If I substitute $id in the 'value' of the checkbox with a static value( eg 1,2 or A,B) box2[ ] posts this value fine. Quote Link to comment https://forums.phpfreaks.com/topic/287711-checkbox-php-string-value/#findComment-1475873 Share on other sites More sharing options...
fastsol Posted April 12, 2014 Share Posted April 12, 2014 Well then most likley the $id doesn't have a value when you are trying to use it. If you do a view source of the code in the browser, does the value show for the checkbox or is it blank? Quote Link to comment https://forums.phpfreaks.com/topic/287711-checkbox-php-string-value/#findComment-1475879 Share on other sites More sharing options...
Gavski Posted April 12, 2014 Author Share Posted April 12, 2014 no, the checkbox doesn't display a value but the displayed list does display a value for $id, therefore $id definitely does have a value but the checkbox doesn't seem to wan to post it, this is my problem. cheers and thanks for your patience Quote Link to comment https://forums.phpfreaks.com/topic/287711-checkbox-php-string-value/#findComment-1475883 Share on other sites More sharing options...
Ch0cu3r Posted April 12, 2014 Share Posted April 12, 2014 (edited) 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 April 12, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/287711-checkbox-php-string-value/#findComment-1475884 Share on other sites More sharing options...
Gavski Posted April 13, 2014 Author Share Posted April 13, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/287711-checkbox-php-string-value/#findComment-1475956 Share on other sites More sharing options...
Ch0cu3r Posted April 13, 2014 Share Posted April 13, 2014 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); } Quote Link to comment https://forums.phpfreaks.com/topic/287711-checkbox-php-string-value/#findComment-1475960 Share on other sites More sharing options...
Gavski Posted April 13, 2014 Author Share Posted April 13, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287711-checkbox-php-string-value/#findComment-1475964 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.