0

POST PHP to itself using AJAX does not return the expected value?

Parameters has been successfully send to POST, based on Firebugs Console but it does not dispaly the result to the same page.

How to fix this?

labor.php

    <?php

if(isset($_POST['from']) && isset($_POST['from']))
{
    echo 'from: '.$_POST['from'].
    ' to: '.$_POST['to'];
    //do something here
}

?>

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Labor Reports</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <script>
  $(function() {
    $( "#datepicker_from" ).datepicker();
    $( "#datepicker_to" ).datepicker();
    $( "button" )
      .button()
      .click(function() {
        $.ajax({
            url: 'labor.php',
            type: 'POST',
            data: { from: $('#datepicker_from').val().trim(), to: $('#datepicker_to').val().trim() }, 
            success: function(){

            }
        });
      });
  });

1 Answer 1

2

You aren't doing anything with the returned data.

success: function(data){
    alert(data);
}

data represents the response sent back from the server. In your case, it will be the echoed output of your from and to $_POST values. Coincidentally, you will also get the rest of the page sent back to you as well, you'll probably want to return false; after the echoes, so that it stops the printing of the page.

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

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.