3

I'm not sure how to return php function results to a jQuery ajax request my functions.js file has the following function in it:

 var PartNumber = "123456890";
 $.ajax({url: 'aoiprocedures?Action=loadparsedata&FullPartNumber=' + PartNumber,
    success: function(oData){
       alert(oData);
 }});

and my aoiprocedures.php file has the following in it:

<?php
  $pcAction = isset( $_REQUEST['Action'] ) ? $_REQUEST['Action'] : "";
  $FullPartNumber = isset( $_REQUEST['FullPartNumber'] ) ? $_REQUEST['FullPartNumber'] : 0 ;
  switch($pcAction){
        case "loadparsedata":
              $GenerateMsg = DoParseFile($FullPartNumber);
        break;
  }

  function DoParseFile($FullPartNumber){
     //do a bunch of stuff//
     return $FullPartNumber ;
  };
 ?>

so, for test purposes, I should be getting back in my javascript alert() box "1234567890" but I'm getting "" (blank). Any ideas?

3 Answers 3

4

As CoolStraw has already said, ajax sees as response exactly the same thing you see in the browser. So unless you print something out, you will get a blank response.

So...

Change:

return $FullPartNumber ;

To:

echo $FullPartNumber ;

Also change:

$GenerateMsg = DoParseFile($FullPartNumber);

To:

DoParseFile($FullPartNumber);
Sign up to request clarification or add additional context in comments.

2 Comments

No. I'd say echo $GenerateMsg
@CoolStraw I agree unless something has been omitted
2

You have to echo or print your result. The content that comes out of your php file as in when you visit it via browser is the result that's gonna be received by your ajax call

Comments

0

Try:

<script type="text/javascript">
var PartNumber = "123456890";

$.post("aoiprocedures", { my_action: "loadparsedata", FullPartNumber: PartNumber} , success: function(oData){
   alert(oData);

} );

</script>


<?php

  function DoParseFile($FullPartNumber){
     //do a bunch of stuff//
     echo $FullPartNumber ;
  };

  $pcAction = isset( $_POST['my_action'] ) ? $_REQUEST['my_action'] : "";
  $FullPartNumber = isset( $_REQUEST['FullPartNumber'] ) ? $_REQUEST['FullPartNumber'] : 0 ;
  switch($pcAction){
        case "loadparsedata":
              $GenerateMsg = DoParseFile($FullPartNumber);
        break;
  }
 ?>

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.