繁体   English   中英

如何从jquery .load获取php respone

[英]how get php respone from jquery .load

我给另一个代码例如这是我的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>

我这是我的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;
    }
?>

它不起作用为什么? 我们有回声“1”和回声“2”我想要jquery只需检查$ myVariable数据不是完整的php回调! 我想我必须使用json,但我不知道如何

好吧,假设您想要在发布的页面上使用JQuery读取值,您可以这样做,因为您通过执行以下操作echo $myVariable;该页面中的值: echo $myVariable; 现在,我通常使用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.");
    }
});

这应该从PHP页面检索值。 “thepagetoretrievefrom.php”是您要从中检索信息的页面。 function(retrievevalue)只表示你通过JQuery从页面请求的任何输出都将被放入retrievevalue。 然后,使用JQuery,您可以决定是否要根据“retrievevalue”的内容对另一个页面进行新的调用。 然而,这不是实现此目的的最佳方法,因为这将打印该页面中可能存在的任何内容,但如果您从该页面请求一个特定值,那么它应该不是问题。

暂无
暂无

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

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