How to get the POST data to include all fields including empty values

  • Profile Image
    ampsys
    Asked on September 24, 2019 at 01:38 PM

    How do you get the array to include ALL data fields even when the form is set to skip/hide fields based on certain responses?

    The problem I'm having is that I want the form to be interactive, but the results feed keeps changing. Sometimes it doesn't include fields altogether, other times it does but with a NULL value (which is fine), and yet other times still, the order is all jumbled up.

    Obviously this causes the sql insert to fail because of mismatched data types or missing values altogether.

    Please help!

    Thanks.



    This is a re-post of a comment on How to send Submissions to Your MySQL Database Using PHP

  • Profile Image
    Vanessa_T
    Answered on September 24, 2019 at 02:01 PM

    Unfortunately, the POST data will only include fields which are filled.

    Since POST data is an associative array, the order will not matter since you can access each index by its name key

    Now what I can see could hinder you is determining what are the valid keys. You can manually browse thru your form and get all the unique names of each field. Then on your script, create a loop with all the keys and check if the value is set.

    How-to-Find-Field-IDs-and-Names

    You can have a script like below to do such:


    $fields = array(list all of your field names here);

    foreach ($fields as $field) {

      if (isset($_POST[$field]) && !empty($_POST[field])) {

          // prep mysql field

        }

    }

    //mysql insert code here