簡體   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