0

I have two files, landing.php and test.php. Landing.php has a form that is submitted via ajax using the post method to test.php. If the ajax call is successful, the browser loads test.php, at which point I am having an issue. I cannot figure out what I am doing wrong in my attempts to display the value of the post data that is sent to test.php.

The following is the ajax call from landing.php. I am reasonably sure that this is working correctly from having looked at the post data in Firebug.

$('#searchForm').submit(function(e) {
    e.preventDefault();
    var data1 = $('#checkBoxDiv input').serialize() + '&' + 'barcodeID=' + $('#barcodeID').val();
    $.ajax({
        url: 'test.php',
        data: data1,
        type: 'POST',
        success: function() {
            window.location.href = "test.php";
        }
    });  
});  

Here are the contents of test.php.

<?php

$bar = $_POST['barcodeID'];

echo "<html>";
echo "<body>";
echo "<p>*" . $bar . "*</p>";
echo "</body>";
echo "</html>";
?>

If I were to take the asteriks out of the line with the <p> from test.php, I would be presented with a completely blank screen when I arrived at test.php in my browser. With them there, however, I am presented with "**" on my screen. The most confusing part is, however, that if I include

echo $bar;

in test.php, I see the value of $bar (00-00102 - exactly as it should be) in Firebug's response tab on the post data viewer.

After some research, I read that post was the method of choice for when you do not want to display query strings in the URL bar, as well as the method to use when you have a large amount of data to send (which will be the case for me, it'll end up being around five to six hundred characters when all is said and done). I've looked at other stackoverflow posts and articles, attempted to replicate what was recommended, and still cannot get this right.

Am I doing something completely wrong or attempting to use these methods in a way in which they are not intended to be used?

5 Answers 5

2

Don't change the window location in the success of your ajax call. The call is posting the data to test.php and you are getting your response back there. Then you are redirecting to the page with a blank post which results in just the asterisks.

If you only want to display the results from test.php change your $.ajax call to the following:

$.ajax({
    url: 'test.php',
    data: data1,
    type: 'POST',
    success: function(response) {
        $('html').replaceWith(response);
    }
});

Or if you do want to change the page, forget about the ajax and just post the form to test.php

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

Comments

2

You are reloading the window after the AJAX succeeds, there is no POST information at that point.

You can do something like this though:

$('#searchForm').submit(function(e) {
    e.preventDefault();
    var data1 = $('#checkBoxDiv input').serialize() + '&' + 'barcodeID=' + $('#barcodeID').val();
    $.ajax({
        url: 'test.php', // <-- POST exists during this call
        data: data1,
        type: 'POST',
        dataType: 'html',
        success: function(data) {
            $('html').replaceWith(data);
        }
    });  
});  

Comments

1

The AJAX POST will be finished in one turn. That means, the values you had posted to test.php through ajax will be present only through that request. What you are doing is, you are redirecting to test.php upon receiving response from test.php. This is a fresh request, and it does not have any POST values. Hence, there is nothing in $_POST['barcodeID'];

Also, in your code, you are having $('#barcodeID').val() twice. Try removing the last one.:

var data1 = $('#checkBoxDiv input').serialize() + '&' + 'barcodeID=' + 
    $('#barcodeID').val();
    $('#barcodeID').val();

Comments

1

If you are trying to use AJAX to replace the content of the page with the result of the form submission to test.php then you would need to change the AJAX call in landing.php to:

$.ajax({
    url: 'test.php',
    data: data1,
    type: 'POST',
    success: function(response) {
        $('html').replaceWith(response);
    }
});

Otherwise I would suggest not using AJAX and just do a regular form submission using HTML by setting the action attribute of your form as such:

<form method="post" action="test.php">

Comments

1

You are passing the variables to the test.php file as a POST request, which means that the test.php file will get that data and act on it. Then after that request is successfully completed you are redirecting to test.php without passing any variables.

If you really want to submit a form (with the redirect that comes with it), do that using html:

<form action="test.php" method="post">
    <input type="text" name="barcodeID" />
</form>

If you don't need the redirect, do what others have suggested and show the output on the same page instead (someDiv is the html element where you would like to display the response from test.php):

$('#searchForm').submit(function(e) {
    e.preventDefault();
    var data = $('#checkBoxDiv input').serialize() + '&' + 'barcodeID=' + $('#barcodeID').val();

    $.post('test.php', data, function(response) {
        $('someDiv').html(response);
    });
});

If you must do it using JavaScript and a redirect, append a hidden dummy form to the page and submit it:

$('<form action="test.php" method="post">' +
    '<input type="hidden" name="barcodeID" value="123456789" />' +
    '</form>')
    .appendTo('body')
    .submit();

Comments

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.