简体   繁体   English

如何从jquery .load获取php respone

[英]how get php respone from jquery .load

i give another codes for example this is my some3.php code:(First file) : 我给另一个代码例如这是我的some3.php代码:(第一个文件):

<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: 我这是我的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 ! 我们有回声“1”和回声“2”我想要jquery只需检查$ myVariable数据不是完整的php回调! i think i must use json but i dont know how 我想我必须使用json,但我不知道如何

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; 好吧,假设您想要在发布的页面上使用JQuery读取值,您可以这样做,因为您通过执行以下操作echo $myVariable;该页面中的值: echo $myVariable; Now this is how I generally read a value off another page with JQuery which is by using JQuery's get() method. 现在,我通常使用JQuery的get()方法通过JQuery读取另一个页面的值。

$.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. 这应该从PHP页面检索值。 "thepagetoretrievefrom.php" is the page where you want to retrieve the information from. “thepagetoretrievefrom.php”是您要从中检索信息的页面。 function(retrievedvalue) just indicates that whatever output you're requesting from the page via JQuery will be put into retrievedvalue. function(retrievevalue)只表示你通过JQuery从页面请求的任何输出都将被放入retrievevalue。 Then, using JQuery, you may decide whether you want to do a new call to another page depending on what the "retrievedvalue" was. 然后,使用JQuery,您可以决定是否要根据“retrievevalue”的内容对另一个页面进行新的调用。 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. 然而,这不是实现此目的的最佳方法,因为这将打印该页面中可能存在的任何内容,但如果您从该页面请求一个特定值,那么它应该不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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