0

I try to submit a form via AJAX and then work with the values. Here is the markup of the form:

<form id="formDeb" action="controller/processDeb.php" method="POST" enctype="multipart/form-data">
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="formGroupName">Angezeigter Name in Cydia/Sileo</label>
                <input type="text" class="form-control" id="formGroupName" name="formGroupName"
                       placeholder="Vorname Nachname oder K&uuml;rzel">
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label for="formGroupMail">Deine E-Mail Adresse</label>
                <input type="email" class="form-control" id="formGroupMail" name="formGroupMail"
                       placeholder="Deine E-Mail Adresse">
            </div>
        </div>
    <!-- and more fields --> 
</form>

I submit the form via Ajax:

var myFormData = new FormData(this);
var supportedDevices = getSelectedBoxes("devices");
var supportedVersions = getSelectedBoxes("iosversions");

myFormData.append("supportedDevices", supportedDevices);
myFormData.append("supportedVersions", supportedVersions);

$.ajax({
    type: method,
    url: targetUrl,
    data: myFormData,
    processData: false,
    async: true,
    beforeSend: function () {
       isSubmitting = true;

       $btnSubmit.attr('disabled', 'true');
       $btnImg.toggle();
       $btnLoader.toggle();
     }
})

in the processDeb.php File i have this code:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (is_array($_POST)) {
    echo '<pre>' . var_export($_POST, true) . '</pre>';
    $name = $_POST['cName'] ?? '';
    $mail = $_POST['cMailAddress'] ?? '';
}

The values are should be set, but when I try to access $name or $mail the content is ''.

The output of the var_export is the following:

array ( '------WebKitFormBoundaryMBXZo8GpgumN5Etl Content-Disposition:_form-data;_name' => '"formGroupName"

fancy name ------WebKitFormBoundaryMBXZo8GpgumN5Etl Content-Disposition: form-data; name="formGroupMail"

[email protected]

So it should be possible to access the values using $_POST['formGroupName'] but it is not possible.

The enctype is set to multipart/form-data because there are also some upload fields for the user.

1 Answer 1

1

jQuery will, by default, set a Content-Type on an Ajax request of application/x-www-form-urlencoded; charset=UTF-8 which is wrong when you are sending multipart data.

Add

contentType: false

… to the options you pass to $.ajax() so that jQuery won't override the default behaviour of XMLHttpRequest (which is to generate the Content-Type from the FormData object).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.