简体   繁体   中英

how get php respone from jquery .load

i give another codes for example this is my some3.php code:(First file) :

<head>
<script src="jquery-1.7.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('p').click(function(){
        var who = $('input#who').val();
        var why = $('input#why').val();     
        $('#geting').load('file2.php',{who:who,why:why},function(applyData){
  if ( applyData == 'YEY . Ye have hi' ){
alert('OKKK data is ok ');
  } else{
      alert('Nooo We dont have requested output');
  }
        });
    });
});

</script>
</head>
<body>
<p> click </p>
<input type="text" id="who">
<br>
<input type="text" id="why">
<div id="geting" align="center">
</div>
</body>

i this my file2.php:

<?php
echo "1";
echo "2";
    if($_REQUEST['who'] == "hi"){


    $myVariable = "YEY . Ye have hi";
    echo $myVariable;
    } else{
   $myVariable = "The out put is not Hi";
    echo $myVariable;
    }
?>

its not work why? becuse we have echo "1" and echo "2" i want jquery just check $myVariable data not whole php callback ! i think i must use json but i dont know how

Well, assuming that you want to read the value with JQuery off the page you are posting to, you could do this, since you are echo'ing the value out in that page by doing the following: echo $myVariable; Now this is how I generally read a value off another page with JQuery which is by using JQuery's get() method.

$.get("thepagetoretrievefrom.php", function(retrievedvalue) {
    alert("Here's the data you requested: " + retrievedvalue);
    if (retrievedvalue == 1)  {
        //print out something here
        alert("The retrieved value was 1.");
    }
});

And that should retrieve the value from the PHP page. "thepagetoretrievefrom.php" is the page where you want to retrieve the information from. function(retrievedvalue) just indicates that whatever output you're requesting from the page via JQuery will be put into retrievedvalue. Then, using JQuery, you may decide whether you want to do a new call to another page depending on what the "retrievedvalue" was. This, however is not the best method to achieve this, since this will print whatever may be in that page, but if you are requesting one specific value from that page, then it shouldn't be an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM