2016-08-05 4 views
0

저는 아약스의 새로운 사용자입니다. 그래서 ...PHP를 사용하여 두 번째 div에 Ajax 출력

간단한 html 페이지에서 ajax를 사용하여 데이터를 받고, mysql 테이블의 데이터를 사용하여 결과를 계산하고, 같은 페이지의 div에 결과를 반영하는 PHP 스크립트에 액세스합니다. 내 javascript 문은 다음과 같습니다.

PHP echo 출력은 outputdiv1이라는 기본 페이지의 div로 이동합니다.

나는 그 부분을 얻었다. 문제 없어. 그것이 어떻게 작동하는지 정확히 모르지만 작동합니다.

나는 PHP 스크립트를 사용하여 동일한 페이지에서 다른 div (outputdiv2라고도 함)로 출력을 표시하고 싶습니다. 내 PHP 스크립트에서 참조하거나이 다른 div 출력을 에코합니까?

나는 두 번째 PHP 스크립트에 액세스하는 자바 스크립트 코드에서 두 번째 $ .post 문을 가지고 있다고 생각한다. 하지만 그렇게되면 mysql 데이터베이스에 다시 액세스해야한다. 나에게 효율적으로 보이지 않는다.

더 좋은 방법이 있나요?

감사합니다.

HTML 코드는 여기 :

theuser 이전

<table width=400 align=center><tr><td> 
There is a question here, with 2 possible answers:<p> 
<form> 
<input type=radio style="width:22px; height:22px" name="ques1" id="opt1" value="answer 1" onclick="post1()">&nbsp; answer 1<br> 
<input type=radio style="width:22px; height:22px" name="ques1" id="opt2" value="answer 2" onclick="post1()">&nbsp; answer 2<br> 

</form> 
<div id="outdiv1">first response from php will go here, beneath the question.<br></div> 

<script type="text/javascript"> 
function post1() { 
    var uans1 = "none" 
    if (document.getElementById("opt2").checked) { 
     uans1 = "answer 2" 
    } 
    if (document.getElementById("opt1").checked) { 
     uans1 = "answer 1" 
    } 
    $.post('ajax/phpscript.php',{postuser:theuser,postname:uans1}, function(data) {$('#ans1div').html(data);}); 
} 
</script> 


</td> 
<td width=20%> 
<div id="outputdiv2"> 
second response from php will go here, to the right of the question.<p> 
</div> 
</td> 
</tr></table> 

첫 번째 응답은 제 2 응답과 같지 않을 것이다 정의된다.

+0

질문에 html 구조를 추가 할 수 있습니까? –

+0

다른 div에 동일한 응답을 넣으시겠습니까? –

+0

아니요, 두 번째 div에서 다른 응답을 원합니다. – user1852336

답변

0

당신은 이것을 이해해야합니다. 누가 div에 글을 쓰는 것이 PHP가 아니라 javascript인지, 아약스를 사용하고 있기 때문입니다. Ajax는 PHP와 comunicate하고 응답을 제공하는 방법입니다. 이제 자바 스크립트로이 응답을 처리해야합니다.

동일한 내용을 outputdiv1 및 outputdiv2에 넣으려면 다시 아약스를 게시 할 필요가 없으며 두 div에 씁니다. 당신은 내가 제안 다른 데이터를 원하는 경우

$.post('ajax/phpscript.php',{postuser:theuser,postname:uans1}, function(data) {$('#outputdiv1').html(data);$('#outputdiv2').html(data);}); 

당신은 시스템이 하나 개 POST 요청에 필요하고 JSON.parse 더 잘 처리 할 수 ​​있도록하는 JSON 형식 (http://php.net/manual/es/function.json-encode.php 참조)을 반환하는 모든 결과를 얻을 생각() 클라이언트 쪽.

1

JSON을 사용하여 통신하고 배열을 반환 할 수 있습니다. JS

$.ajax({ 
    url: 'ajax/phpscript.php', 
    method: 'POST', 
    data: { 
     postuser: theuser, 
     postname: uans1 
    }, 
    dataType: 'JSON' 
}).done(function(data) { 
    if ($.isArray(data)) { 
     $('#outputdiv1').html(data[0]); 
     $('#outputdiv2').html(data[1]); 
    } 
}); 

에서이 같은 뭔가를해야 당신의 PHP 스크립트는 다음과 같이

<?php 
    include('dbconnection.php'); 
    $result = []; 
    //SELECT data for div1 (part you already have) 
    $result[] = $mysql_result_as_html_for_outputdiv_1; // In your case this would be a html string 
    //SELECT other data for div2 
    $result[] = $mysql_result_as_html_for_outputdiv_2; // In your case this would be a html string 

    header('Content-Type: application/json'); 
    echo json_encode($result); 
?> 

이보다 더 깨끗한 솔루션 단지 PHP에서 객체로 데이터를 반환하고 몇 가지 템플릿을하는 것입니다 js 귀하의 데이터에 적합합니다.

+0

감사합니다. 그게 내가 필요한 모든 코드인가? 나는 json을 사용하지 않았다. – user1852336

+0

간단한 해결책 (html 문자열의 배열)을 위해 js 코드가 완성되었습니다. – simialbi

관련 문제