2012-09-15 2 views
0

이미지를 클릭하면 john이라는 div의 내용을 PHP 파일의 출력으로 변경하고 싶습니다. 나는 그것이 간단하다는 것을 확신하지만 나는 맞는 모범을 발견 할 수 없다. 당신이 당신의 자바 스크립트와 구문 오류가있어 같은div onclick의 내용을 Ajax로 변경하십시오.

<?php echo "hello"; ?> 

답변

0

아래 코드로 해결할 수있었습니다. 또한이 코드는 PHP 파일에서 "hello"를 가져온 후에 요소의 ID를 반환합니다.

<script language="javascript"> 
      function calldislike(str){ 
       if (str==""){ 
        document.getElementById("john").innerHTML=""; 
        return; 
        } 
       xmlhttp=new XMLHttpRequest(); 
       xmlhttp.onreadystatechange=function(){ 
        if (xmlhttp.readyState==4 && xmlhttp.status==200){ 
        document.getElementById("john").innerHTML=xmlhttp.responseText; 
        } 
       } 
       xmlhttp.open("GET","test.php?q="+str,true); 
       xmlhttp.send(); 
      } 
     </script> 

<img src='images/cross.png' onclick="calldislike(this.id);" id='3'> 
<div id='john'>john</div> 

test.php

<?php echo "hello".$_GET["q"]; ?> 
0

이 나에게 보이는

function ajaxFunction(){ 
       var ajaxRequest; 
        ajaxRequest = new XMLHttpRequest(); 
       ajaxRequest.onreadystatechange = function(){ 
        if(ajaxRequest.readyState == 4){ 
         document.getElementById("john").innerHTML=xmlhttp.responseText; 
        } 
       } 
       ajaxRequest.open("GET", "test.php", true); 
       ajaxRequest.send(null); 
      } 

HTML

<img class='cross' src='images/cross.png' onclick="ajaxFunction();"> 
<div id='john'>john</div> 

test.php : 여기에 내 코드입니다. 나는 크로스 브라우저/버전을 제안 할 수 있습니다이

function ajaxFunction(){ 
    var ajaxRequest = new XMLHttpRequest(); 
    ajaxRequest.onreadystatechange = function(){ 
     if(ajaxRequest.readyState == 4 && ajaxRequest.status==200){ 
       document.getElementById("john").innerHTML=ajaxRequest.responseText; 
      } 
     } 
    ajaxRequest.open("GET", "test.php", true); 
    ajaxRequest.send(); 
} 
+0

감사합니다. 좋은 'xmlhttp'캐치하지만 귀하의 코드를 시도하고 여전히 클릭에 응답하지 않습니다. –

0

을 시도 귀하가 xmlhttp에서 responseText을 얻으려고 노력하지만 XMLHttpRequestajaxRequest

에 저장, 사용 :

var ajaxRequest = getXMLHttpRequest(); 

하고,

function getXMLHttpRequest() { 
    var re = "nothing"; 
    try {re = new window.XMLHttpRequest();} catch(e1) {}; 
    try {re = new ActiveXObject("MSXML2.XMLHTTP.4.0");} catch(e) {}; 
    try {re = new ActiveXObject("Msxml2.XMLHTTP");} catch(e2) {}; 
    try {re = new ActiveXObject("Microsoft.XMLHTTP");} catch(e3) {}; 
    try {re = new ActiveXObject("MSXML2.XMLHTTP.3.0");} catch(ex) {}; 
    if (re != "nothing") {return re;} 
    else {return null;}; 
}; 

뿐만 아니라 chan ging to

ajaxRequest.responseText(); 
+0

감사. 내 방문자의 3 ​​%만이 IE의 이전 버전을 사용하는 것 같습니다. 그래서 지금은 전혀 작동하지 않는 데 초점을 맞추고 있습니다.하지만 일을 처리 할 때 그것을 정렬하는 것이 좋지 않을 것이라고 생각합니다. . –

관련 문제