php - Retrieve multiple checkboxes stored as an array -



php - Retrieve multiple checkboxes stored as an array -

i working on form contains multiple checkboxes (each own value). lastly checkbox marked "other" and, when checked, text input should appear allow user write his/her own value.

<input name="pvital[]" type="checkbox" id="pvital[]" value="i &amp; o" />i &amp; o<br/> <input name="pvital[]" type="checkbox" id="pvital[]" value="daily weight" />daily weight<br/> <input name="pvital[]" type="checkbox" id="pvital[]" value="foley catheter" />foley catheter<br/> <input name="pvital[]" type="checkbox" id="pvital[]" onclick="showhide(whatever);" value="" />other<br/> <input name="pvital[]" type="text" id="whatever" style="visibility: hidden;" />

i insert these values same field in database, array of strings, using php's implode function:

if ((isset($_post['pvital']))){ array_walk($spvital, 'getsqlvaluestring'); $spvital = implode(',',$_post['pvital']); } $insertsql = sprintf("insert admissionorder (vitalsigns) values (%s)", getsqlvaluestring($spvital, "text")); mysql_select_db($database_pps, $pps); $result1 = mysql_query($insertsql, $pps) or die(mysql_error());

this working fine insertion, encounter problem in retrieving these checkboxes... "other" text input if appropriate.

well, can see couple of problems.

first, array_walk on line 2 isn't doing it's running on empty... array? string? clobbered. should walking along $_post element , imploding results.

second, if checks box "other" you're going empty entry in pvital, much more awkward way of figuring out box has been checked giving own distinct name.

third, you're collapsing bunch of discrete values single text field , expecting able unpack text field -- maybe exploding along commas? -- , discrete values back, problematic.

if command database i'd suggest making table after pattern

admission_vital_signs( admission_order_id int, vital_sign_text varchar, is_free_text bool)

to hold said discrete values.

if don't command database command output mechanism i'd suggest using delimiter doesn't appear in nature -- pipe "|" -- can @ to the lowest degree split things cleanly, , maybe preface freetext freetext:: ("::" not found in nature) can recognize when shows up.

if don't command database or output mechanism need edit original post output mechanism have work with.

edited:

alright, if understand correctly, you're retrieving info database , trying work out whether should tick appropriate boxes on postback, command output mechanism. next set of instructions assume don't command database; if do, making new table way go here method's still crib-worthy.

name checkbox other "pvitalother', value '1', , textfield 'pvitalothertext'. no brackets. here's psuedocode going in , coming out.

if isset($_post['pvitalother']) $_post['pvital'][] = "freetext::{$_post['pvitalothertext']}"; $pvitals = implode('|', array_walk('mysqlescape', $_post['pvital'])); //and store $pvitals in database

when you're coming out:

$pvitals = explode('|', $row['vitalsigns']); $pvitals_last = $pvitals[count($pvitals)-1]; if strstr("freetext", $pvitals_last) $lastparts = explode('::', $pvitals_last); $pvitalother = true; $pvitalothertext = $lastparts[1];

then when go build checkboxes pipe in bit of php like

<? php if (in_array('daily weight', $pvital)) echo "checked"; ?>

right middle of fixed-value checkbox tags. same thing "other" based on pvitalother , set appropriate initial visibility , value of freetext input field.

php

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -