ConnerAiken
ConnerAiken

Reputation: 194

Ajax Call Syntax

Trying to do a simple ajax post, for some reason, it is not posting my data! It successfully posts to the file (ajaxpost.php) but no POST data is passed..

            var myKeyVals = {caption: "test"};
                $.ajax({ //Process the form using $.ajax()
                    type        : 'POST', //Method type
                    url         : 'ajaxpost.php', //Your form processing file url
                    data        : myKeyVals, //Forms name
                    dataType    : 'json',
                    success     : function(data) {

                    if (!data.success) { //If fails
                        if (data.errors.name) { //Returned if any error from process.php
                            $('.throw_error').fadeIn(1000).html(data.errors.name); //Throw relevant error
                        }
                    } else {
                            $('#success').fadeIn(1000).append('<p>' + data.posted + '</p>'); //If successful, than throw a success message
                        }
                    }
                });

Here is my AjaxPost.php..

        <?php
        $text = $_GET['caption'];
        $file = 'people.txt';
        // Open the file to get existing content
        $current = file_get_contents($file);
        // Append a new person to the file
        $current = "LOG: " . $text . "\n";
        // Write the contents back to the file
        file_put_contents($file, $current, FILE_APPEND);
        ?>

Upvotes: -1

Views: 72

Answers (2)

Alnitak
Alnitak

Reputation: 339786

POST data is accessed in PHP using $_POST, not $_GET !

Alternatively, if you wish to support both HTTP methods you can use $_REQUEST.

Upvotes: 1

tftd
tftd

Reputation: 17032

In your php file you're using $_GET['caption'] but you should be using $_POST['caption'] as this is a post request.

Upvotes: 1

Related Questions