0

Here's the issue. I have a database column called pymnt_meth_pref, which contains a comma separated string of payment methods chosen from a multiselect form.

<td>Payment Methods Used:<br /> 
              <input type="checkbox" name="pyment_meth_pref[]" value="Cash">I can pay with cash.<br />
              <input type="checkbox" name="pyment_meth_pref[]" value="Check">I can pay by check.<br />
              <input type="checkbox" name="pyment_meth_pref[]" value="Credit Card">I can pay by credit card.<br />
              <input type="checkbox" name="pyment_meth_pref[]" value="Paypal">I can pay with Paypal.<br />              </td>

This array is posted to a variable and turned into a comma separated string

if (isset($_POST['pyment_meth_pref']))
    $pymntmethpref = implode(", ", $_POST['pyment_meth_pref']);

    if (isset($_POST['pyment_meth_acc']))
    $pymntmethacc = implode(", ", $_POST['pyment_meth_acc']);

This is then inserted into the database as a comma separated string.

What I would like to do, is take this string and apply the values to the original form when the user goes back to the form and 'pre-select' the checkboxes, indicating that the user has already selected those values previously, and keeping those values in the database if they choose to edit any other information in the form.

I'm assuming this would need to be done with javascript but if there is a way to do it with PHP I'd rather do it that way.

2 Answers 2

2

Try explode(). as below

$valuesArr = explode(',', $dbresult);

and in your HTML code make condition for each of checkboxs as below:

 echo '<input type="checkbox" name="pyment_meth_pref[]" value="Cash"'.
       (in_array('Cash', $valueArr) ? 'checked="checked"' : '').' />
       '<input type="checkbox" name="pyment_meth_pref[]" value="Check"'.
       (in_array('Check', $valueArr) ? 'checked="checked"' : '').' />';

and so on.

i hope this help

1
  • That was perfect. I knew there was a way to get it done, I just couldn't remember in_array. Thank you.
    – Budove
    Nov 11, 2012 at 23:34
1
<?php

# Assumed that $pymntmethpref and $pymntmethacc are the DB comma-delimited values

$pm_pref = explode( ', ' , $pymntmethpref );
$pm_acc = explode( ', ' , $pymntmethacc );
$html_sel = 'checked="checked" ';

?>
<td>Payment Methods Used:<br /> 
  <input type="checkbox" name="pyment_meth_pref[]" <?php echo ( in_array( 'Cash' , $pm_pref ) ? $html_sel: '' ); ?>value="Cash">I can pay with cash.<br />
  <input type="checkbox" name="pyment_meth_pref[]" <?php echo ( in_array( 'Check' , $pm_pref ) ? $html_sel: '' ); ?>value="Check">I can pay by check.<br />
  <input type="checkbox" name="pyment_meth_pref[]" <?php echo ( in_array( 'Credit Card' , $pm_pref ) ? $html_sel: '' ); ?>value="Credit Card">I can pay by credit card.<br />
  <input type="checkbox" name="pyment_meth_pref[]" <?php echo ( in_array( 'Paypal' , $pm_pref ) ? $html_sel: '' ); ?>value="Paypal">I can pay with Paypal.<br />
</td>
1
  • Thank you. I upvoted you answer because you were basically right, but Mohammed's answer worked the first time.
    – Budove
    Nov 11, 2012 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.