2014-11-05 2 views
-1

PHP 페이지에서 값 집합을 반환하는 아약스 함수가 ​​있습니다. 필요로하는 값을 가져와야합니다. 나는이자바 스크립트에서 아약스 응답 데이터를 분리하는 방법

은 ajax.js이

function MakeRequest() 
{ 
    var xmlHttp = getXMLHttp(); 
    xmlHttp.onreadystatechange = function() 
    { 
    if(xmlHttp.readyState == 4) 
    { 
     HandleResponse(xmlHttp.responseText); 
    } 
    } 

    xmlHttp.open("GET", "ajax.php", true); 
    xmlHttp.send(null); 
} 

ajax.php

<?php 
require_once('config.php'); 
if(! $conn) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 
$sql = 'SELECT * FROM thr'; 
mysql_select_db($dbname); 
$retval = mysql_query($sql, $conn); 
if(! $retval) 
{ 
    die('Could not get data: ' . mysql_error()); 
} 
echo "<table border='1'>"; 
while($row = mysql_fetch_assoc($retval)) 
{ 
echo "<tr>"; 
echo "<td>" . $row['id'] . "</td>"; 
echo "<td>" . $row['ther_name'] . "</td>"; 
echo "<td>" . $row['region'] . "</td>"; 
echo "<td>" . $row['phone_no'] . "</td>"; 
echo "<td>" . $row['address'] . "</td>"; 
echo "<td id='lat'>" . $row['corrd_lattitude'] . "</td>"; 
echo "<td id='lon'>" . $row['corrd_longitude'] . "</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 
mysql_close($conn); 
?> 

내가이

의 값을 얻는 방법을

$row['corrd_lattitude'] 
$row['corrd_longitude'] 

의 값을 필요로 할 수있는 방법

+0

JSON을 사용할 수 있습니다 –

+0

응답에서 JSON을 사용하거나 다른 방법으로 '-'또는 일부 특수 문자로이 두 값을 연결하고 응답으로 분할 할 수 있습니다. –

답변

2

먼저 필요한 항목 만 가져올 수 있습니다. 테이블에서 값을 ... 내가 정확히 다음 쿼리를 사용할 수있는 이해.

$sql = 'SELECT corrd_lattitude,corrd_longitude FROM thr'; 

테이블에서 열 값만 반환합니다. 여기

$json = json_encode(array($row['corrd_lattitude'], $row['corrd_longitude'])); 
echo $json; 
0
use json:----------- 
========================================= 

$sql = "SELECT * FROM thr"; 
$rs = mysql_query($sql); 
$res = mysql_fetch_assoc($rs); 

if(mysql_num_rows($rs)>0){ 
echo json_encode(array('lats'=>$res['corrd_lattitude'],'lngs'=>$res['corrd_longitude'])); 
}else{ 
echo json_encode(array('lats'=>'','lngs'='')); 
} 

라트와 lngs는 그 안에서 값을 저장 단순히 varaiable 이름이며 라트와 lngs varaibale에서 다른 페이지에있는 값을 얻을.

0

요청에 따라 매개 변수를 지정할 수 있습니다. GET 방식에서
:
ajax.php?corrd_lattitude=true&corrd_longitude=true
및 PHP 파일에서 당신은 $_GET 배열의 예에서 확인 가능 :

<?php 
[...] 
echo "<table border='1'>"; 
while($row = mysql_fetch_assoc($retval)) 
{ 
    echo "<tr>"; 
    if ($_GET['id'] == true) { 
    echo "<td>" . $row['id'] . "</td>"; 
    } 
    if ($_GET['ther_name'] == true) { 
    echo "<td>" . $row['ther_name'] . "</td>"; 
    } 
    if ($_GET['region'] == true) { 
    echo "<td>" . $row['region'] . "</td>"; 
    } 
    if ($_GET['phone_no'] == true) { 
    echo "<td>" . $row['phone_no'] . "</td>"; 
    } 
    if ($_GET['address'] == true) { 
    echo "<td>" . $row['address'] . "</td>"; 
    } 
    if ($_GET['corrd_lattitude'] == true) { 
    echo "<td id='lat'>" . $row['corrd_lattitude'] . "</td>"; 
    } 
    if ($_GET['corrd_longitude'] == true) { 
    echo "<td id='lon'>" . $row['corrd_longitude'] . "</td>"; 
    } 
    echo "</tr>"; 
} 
echo "</table>"; 
mysql_close($conn); 
?> 

더 나은 대신 HTML의 사용 JSON (또는 일반 값)입니다 - 당신의 JS가에 값을 포장한다 html. 서버 쪽이 더 좋고, 클라이언트 쪽이 html을 만들어야합니다. 내 영어를 위해서 부르세요.

관련 문제