Select multi-select form from array

Posted by Budove on Stack Overflow See other posts from Stack Overflow or by Budove
Published on 2012-11-11T22:57:03Z Indexed on 2012/11/11 22:59 UTC
Read the original article Hit count: 184

Filed under:
|
|
|

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 as 'pre-selected' checkboxes, indicated 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.

© Stack Overflow or respective owner

Related posts about php

Related posts about html