0

I am trying to send an AJAX function from jQuery to PHP. I can get it to the right page fine, but when I get there, how do I access the data from the AJAX call? It's probably something silly I'm missing.

jQuery:

$(document).ready(function() {

    $("#assessform_page").on("submit", "#assessform", function(e) {
        e.preventDefault();
        var assessrows = {};
        var url = $(this).attr("action");

        $("div[data-test-num]").attr("data-test-num", function(index, value) {
            assessrows[value] = {};
            $(this).children("input[data-tab-row], select[data-tab-row]").attr("data-tab-row", function(i, v) {
                assessrows[value][v] = $(this).val();
            });
            $(this).children("label[data-tab-row]").attr("data-tab-row", function(i, v) {
                assessrows[value][v] = $(this).text();
            });
        });

        $.ajax({
            url: url,
            data: assessrows,
            type: "POST",
            success: function() {
                window.location.href = url;
            },
        });

    });

});

HTML:

<form id="assessform" action="assessment.php" method="post" enctype="application/x-www-form-urlencoded" name="assessform">
    <div data-test-num="1">
        <label data-tab-row="objname">First Value: </label>
        <select autofocus="autofocus" data-tab-row="status">
            <option value="not_started">Not started</option>
            <option value="in progress">In Progress</option>
            <option value="obstacles">Obstacles</option>
            <option value="excluded">Excluded</option>
            <option value="done">Done</option>
        </select>
        <input type="text" data-tab-row="numer" value="" />
        <input type="text" data-tab-row="denom" value="" />
        <br />
        <br />
    </div>
    <button type="submit" name="submit-button" id="submit-button">Submit</button><br />
    <button type="button">Save</button><br />
</form>

PHP:

<?php ?> (I can't really do anything until I get this variable.)

What variable do I call to get the data from the AJAX, and what function, if any, do I need to call before I can do that?

2 Answers 2

1

EDIT:

If your assessment.php make a response as follows:

  <?php
       echo 'successfully_completed';
  ?>

The response data from PHP will be passed as follows:

$.ajax({
    url: url, // assessment.php
    data: assessrows,
    type: "POST",
    success: function(data) {
        alert("The response is [" + data + "]"); 
        // "The response is [successfully_completed]"

        // If you want to pass this data to the next page
        window.location.href = "/foo/success.php?data="+data;  
    }
});
Sign up to request clarification or add additional context in comments.

9 Comments

You're right, I shouldn't have gone clogging up the global namespace. I'm not really gonna do anything else with it, but you're still right. Also, I had it as a variable when I wrote it, but I was testing stuff & it was a string when I copied it into the question. Will change it back.
The linked question talked about how you return the data on a JavaScript page, but what I'm asking is how to return it on a PHP page.
Oh, I'm sorry. Do you mean you are asking the way to send data from javascript to PHP?
No, I'm already sending it. If you notice, the URL goes to a PHP page, not a JavaScript page. I'm asking how I get the data variable once I'm there.
In your code, You are posting data to the url and jumping to the same url after sccessfully completed. Could you elaborate on that? Why you need this.
|
1
$.ajax({
    url: "url",
    data: assessrows,
    type: "POST",
    success: function(data) {
        console.log(data); //do something with data
        window.location.href = url;
    },
});

3 Comments

Should I do something with data in the success function before it goes to the PHP page? Will that help me access it from there?
I did not know you wanted to do that, but it looks like @naoki already has it covered.
Sorry, I'm still pretty new to AJAX, so I don't really know what you can & can't do with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.