2

I'm having the hardest time getting a simple form to process using jQuery and PHP. I've tried getting the data via $_POST, $_GET, and $_REQUEST but I guess I'm missing a simple line of code or a complete process altogether.

app.js

$(document).ready(function() {
    $('#sucMsg').hide();
    $('#errMsg').hide();
    $('form').on('submit', function(event) {
        event.preventDefault();
        var form = $(this);
        alert(form.serialize());
        $.ajax(form.attr('action'), {
            type: 'POST',
            contentType: 'application/json',
            dataType: 'html',
            data: form.serialize(),
            success: function(result) {
                form.remove();
                $('#sucMsg').append(result);
                $('#sucMsg').fadeIn();
                console.log(result);
            },
            error: function(xhr, ajaxOptions, thrownError) {
                console.log(thrownError);
                $('#errMsg').append(thrownError);
                $('#errMsg').fadeIn();
            }

        });
    });
});

formProcess.php

<?php
    if ($_POST) {
        echo "Posted something.";
    } elseif ($_GET) {
        echo "Getted something.";
    } else {
        echo "Nothing is working...";
    }
    $tagNumberPost = $_POST['inputTagNumber'];
    $pricePost = $_POST['inputPrice'];
    $makePost = $_POST['inputMake'];

    $tagNumberRequest = $_REQUEST['inputTagNumber'];
    $priceRequest = $_REQUEST['inputPrice'];
    $makeRequest = $_REQUEST['inputMake'];

    if (isset($_REQUEST['inputTagNumber'])) {
        echo '$_REQUEST works...\n';
    } elseif (isset($_POST['inputTagNumber'])) {
        echo '$_POST works...\n';
    } elseif (isset($_GET['inputTagNumber'])) {
        echo '$_GET works...\n';
    } else {
        echo "Nothing is working...";
    }

    echo "<br/>";
    echo "Tag number: " . $tagNumber . "<br/>\n";
    echo "Make: ".$makePost . "<br/>\n";
    echo "Price: " . $pricePost . "<br/>\n";
?>

What I'm expecting to get back is the all the echo's in my formProcess.php to print out in my #sucMsg div.

5
  • so whats the real problem here? Commented Dec 15, 2014 at 1:02
  • What do you see in your alert box on form submit? Add it to the question. Commented Dec 15, 2014 at 1:03
  • What does form.attr('action') return? Are you sure that you're hitting the url of formPrcoess.php? Commented Dec 15, 2014 at 1:07
  • I see the serialized data of the input boxes @sealocal. Commented Dec 15, 2014 at 1:07
  • Sorry, the form.attr('action') points to formProcess.php. I am sure I'm hitting the url of formProcess.php because the echo calls appear in my result and are .appended to my #sucMsg and then faded in. Commented Dec 15, 2014 at 1:10

1 Answer 1

1

Why are you setting your contentType: as application/json? You do not need that. Remove it.

contentType: 'application/json', // remove this line

Just leave it to its default as application/x-www-form-urlencoded if your request is POST.

And in your PHP, $tagNumber is undefined.

if (isset(
    $_POST['inputTagNumber'], 
    $_POST['inputTagNumber'],
    $_POST['inputMake'],
)) {

    $tagNumber = $_POST['inputTagNumber']; // define tagNumber
    $pricePost = $_POST['inputPrice'];
    $makePost = $_POST['inputMake'];

    echo "<br/>";
    echo "Tag number: " . $tagNumber . "<br/>\n";
    echo "Make: ".$makePost . "<br/>\n";
    echo "Price: " . $pricePost . "<br/>\n";

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

3 Comments

@gh0st because you're sending it as application/json which wouldn't make sense. Your sending it as a form url encoded not json. So that your $_POST values are populated.
Ok so contentType defines what is being sent in the ajax method and dataType defines what is returned. I think I understand now. I was following a tutorial and was under the impression that contentType:json was what was being sent once I serialized.
@gh0st yes, thats basically it. your sending a form post, so setting it to application/json is unneeded. glad this helped

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.