2

I have a complex problem due to the generic function I wrote to save form data in MySQL. All forms are send through Ajax after being serialized:

$('.suggestSave, .buttonSave').on('submit', function(e) {
        e.preventDefault();
        var data = $(this).serialize();
        ...

I'm then fetching all POST values by looping through the values and creating the data saved in the database:

        $data = array();
        foreach ($_POST as $param_name => $param_val) {
          if($param_name != 'action' && $param_name != 'patient' && $param_name != 'jsaction') {
            $name = sanitize($param_name);
            $value = sanitize($param_val);
            $data[$name] = $value;
         }
     }

Naturally, an unchecked checkbox won't be sent through. The problem being that if it's empty, I'd like to store the Int value 0 instead of 1 for this checkbox. On the HTML side, I have a classic syntax

   <input type="checkbox" value="1" name="c_name1" checked/>
   <input type="checkbox" value="1" name="c_name2"/>

Any suggestions on how to deal with that problem ? In a one-checkbox-situation, I would check if the post value isset(), but since I'm getting the names and values by looping through... I'm out of ideas...

0

3 Answers 3

1

You can use an array to define the checkboxes and their default values:

$checkboxes = array(
    'c_name1' => 0,
    'c_name2' => 0,
);

$data = array();

foreach ($_POST as $param_name => $param_val) {
    if($param_name != 'action' && $param_name != 'patient' && $param_name != 'jsaction') {
        $name = sanitize($param_name);
        $value = sanitize($param_val);
        $data[$name] = $value;
    }
}

$data = array_replace($checkboxes, $data);
Sign up to request clarification or add additional context in comments.

1 Comment

True, but once again that would have to be done manually. Thanks anyway, the solution dealing with the problem on client-side seems more appropriate to what I'm trying to do !
1

Preceding the checkbox with a hidden field with the same name solves the problem. If the box is checked, the value is overwritten by the checkbox's value and if not then the first value is returned.

   <input type='hidden' value="0" name="c_name1"/>
   <!-- This will be overridden if the following checkbox is checked  -->
   <input type="checkbox" value="1" name="c_name1" checked/>

   <input type='hidden' value="0" name="c_name2"/>
   <input type="checkbox" value="1" name="c_name2"/>

Either document.form.c_name1 or $_POST ['c_name1'] (if submitted) will return 0 or 1 depending on whether the checkbox is checked. Note that the name is not declared as an array - that would return 1 or 2 values depending on the checkbox state.

1 Comment

This is actually NOT working when you use jquery.ajax to serialize the form data since it sends c_name1 = 0 AND c_name1 = 1. The way the php code handles the post data it generates an error. In a regular case you are absolutely right of course !
0
$data['c_name1'] = ( isset($data['c_name1']) )? $data['c_name1']: 0;

1 Comment

As I said, if that code had to work on a few checkboxes, that's what I would do. In this case I don't know the name of the inputs and there could be hundreds (and there will be...).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.