1

javascript AJAXでphpから2つのID値を取得する方法

test.php

function lc(str) {
    if (str.length == 0) {
        document.getElementById("txtHint").innerHTML = "";
        return;
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "gethint.php?q=" + str, true);
    xmlhttp.send();
} 


<p>Output1: <span id="txtHint"></span></p>
<p>Output2: <span id="wrdHint"></span></p>

gethint.php

<?php
$q=$_GET["q"];

if (strlen($q) > 0)
  {
  $out1=strlen($q);
  }
if (str_word_count($q) > 0)
  {
  $out2=strrev($q);
  }
echo $out1;
echo $out2;
?>

Output1 のみが機能してい
ます。Output2 は機能していません。

誰でも私を助けてもらえますか....

4

2 に答える 2

1

PHP コードを変更し、コンマで区切られた両方の ID を出力します。例:

<?php
$q=$_GET["q"];
$out1="";
$out2="";
if (strlen($q) > 0)
  {
  $out1=strlen($q);
  }
if (str_word_count($q) > 0)
  {
  $out2=strrev($q);
  }
echo $out1.",".$out2;

?>

次に、あなたのJavaScriptで

使用する

var str = xmlhttp.responseText;
var arr = str.split(',');
// arr[0] will be the first id
// arr[1] will be the second id

document.getElementById("txtHint").innerHTML = arr[0];
document.getElementById("wrdHint").innerHTML = arr[1];
于 2013-10-21T10:47:00.870 に答える
0

両方の値を結合してから、jquery 関数でそれらを分離します。もう 1 つの方法は、json_encode() を使用して、キーと値のペアで json にエンコードすることです。そうすれば、それらを解析するのがより簡単になります。

于 2013-10-21T10:57:47.900 に答える