开发者

php5 and jQuery ajax return function results

开发者 https://www.devze.com 2023-02-19 00:43 出处:网络
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:

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 =开发者_Go百科 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?


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);


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


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;
  }
 ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消