SHINHAN
SHINHAN

Reputation: 735

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

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(){

            }
        });
      });
  });

Upvotes: 0

Views: 257

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50787

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.

Upvotes: 2

Related Questions