简体   繁体   中英

$.ajax type post is empty in PHP $_POST

The ajax request works fine in the javascript part but when it comes to PHP the POST is erased and it is not passing only one value.

The ajax looks like this:

function goals() { //dynamically added forms with their corresponding inputs to be posted
    setForms.forEach(function(formIndex) { //the forms that are added and their index
        var formData = { 
            'email': "<?php echo $_SESSION["email"]; ?>", //session email works fine
            'goalRow': formIndex,
            'motivation': $("#goals"+formIndex+"-1").val(), //formIndex works fine adding the right number to get the values
            'priority': $("#goals"+formIndex+"-2").val(),
            'goal': $("#goals"+formIndex+"-3").val(),
            'measure': $("#goals"+formIndex+"-4").val(),
            'endDate': $("#goals"+formIndex+"-5").val(),
            'phase1': $("#goals"+formIndex+"-6").val(),
            'phase2': $("#goals"+formIndex+"-7").val(),
            'phase3': $("#goals"+formIndex+"-8").val(),
            'notes': $("#goals"+formIndex+"-9").val(),
            'notification': $("#goals"+formIndex+"-10").val(),
            'frequency': $("#goals"+formIndex+"-11").val(),
            'notificationStart': $("#goals"+formIndex+"-12").val(),
        };
        $.ajax({ //posting the form
            type: "POST",
            url: "?service=goalsabroad-api", //page for the action
            data: formData, //data object
            success: function() { //log to make sure data is passed
                console.log(formData); 
            }
        });
    });
}

Data is PASSED correctly and everything appears when logging for ALL FORMS without a problem. But when I get to the PHP page it says Undefined index:

Chrome dev tab shows status 200 and logs correctly. When opening in new tab I see the PHP errors. PHP looks like this:

<?php

var_dump($_POST);


$email              = $_POST["email"];
$goalRow            = $_POST["goalRow"];
$motivation         = $_POST["motivation"];
$priority           = $_POST["priority"];
$goal               = $_POST["goal"];
$measure            = $_POST["measure"];
$endDate            = $_POST["endDate"];
$phase1             = $_POST["phase1"];
$phase2             = $_POST["phase2"];
$phase3             = $_POST["phase3"];
$notes              = $_POST["notes"];
$notification       = $_POST["notification"];
$frequency          = $_POST["frequency"];
$notificationStart  = $_POST["notificationEnd"];

$query = "SELECT email FROM goalsabroad WHERE email = '$email' AND goalRow = '$goalRow'";
$result = mysqli_query($connect, $query);

if (mysqli_num_rows($result) === 0) {
    echo "empty";
   $query = "INSERT INTO goalsabroad (email, goalRow, motivation, priority, goal, measure, endDate, phase1, phase2, phase3, notes, notification, frequency, notificationStart) 
                    VALUES ('$email', '$goalRow', '$motivation', '$priority', '$goal', '$measure', '$endDate', '$phase1', '$phase2', '$phase3', '$notes', '$notification', '$frequency', '$notificationSart')";
   echo $query;
   mysqli_query($connect, $query);
} else {
    $query = "UPDATE goalsabroad SET motivation = '$motivation' 
                                 AND priority = '$priority' 
                                 AND goal = '$goal' 
                                 AND measure = '$measure'
                                 AND endDate = '$endDate'
                                 AND phase1 = '$phase1'
                                 AND phase2 = '$phase2'
                                 AND phase3 = '$phase3'
                                 AND notes = '$notes'
                                 AND notification = '$notification'
                                 AND frequency = '$frequency'
                                 AND notificationStart = '$notificationStart'
                                 WHERE email = '$email' AND goalRow = '$goalRow'";
    echo $query;
    mysqli_query($connect, $query);
}
?>

The ajax post must be successful because query is ran and new rows are added as expected BUT only email field adds to the query everything else is empty (and the echo $query returns empty fields even email, and var_dump($_POST) also returns array(0) {}

The .htaccess file redirects .php to the 'extensionless' format that is why url has no .php (but I tried both) like every other parameter of this post.

I basically checked every stackoverflow post about this topic but I could not find anything to make it work, maybe I am the problem but I did not have someone else to click the button for me...maybe I just stressed myself so much on this I can't see the obvious. Thank you for your help.

In you PHP script you have:

$notificationStart  = $_POST["notificationEnd"];

But your Ajax call is sending

'notificationStart': $("#goals"+formIndex+"-12").val(),

When looking for $_POST['notificationEnd'] it will be an undefined index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM