2017-12-02 11 views
0

ページロード時に選択する必要があるラジオボタンが2つあります。 2番目はjavascriptを使ってラジオボタンidを最初のページに転送するphpです。上記のファイルにラジオボタンのIDを送信ラジオボタンがページ上に表示されないロードajax

<html> 
<head> 
<script language="javascript"> 
function checked_attemped(question_id) 
{ 
    var ajaxRequest; 
    try{ 
    ajaxRequest = new XMLHttpRequest(); 

    } catch (e){ 
     // Internet Explorer Browsers 
     try{ 
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try{ 
       ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e){ 
       // Something went wrong 
       alert("Your browser broke!"); 
       return false; 
      } 
     } 
    } 
    ajaxRequest.onreadystatechange = function(){ 

      if(ajaxRequest.readyState == 4) 
      { 
       var x = ajaxRequest.responseText; 
       alert(x);// It shows the radio button id (option1) which is transfered from checked_attempted_option.php 
       alert(document.getElementById(x)); // But it shows null 
       document.getElementById(x).checked=true; //Nothin Happen 
       document.getElementById(option1).checked=true; //This is  working and radio button checked, but above line is not working. I want the  above line work. 

     } 
    } 

    var queryString = "question_id=" + question_id; 
ajaxRequest.open("GET", "checked_attempted_option.php" + queryString, true); 
    ajaxRequest.send(null); 

} 
</script> 
</head> 
<body> 
<script language="javascript"> 
    window.onload=checked_attemped('1'); 
</script> 
<input name="option1" id="option1" type="radio" value="option1"> 
</body> 
</html> 

これは、PHPファイルです

checked_attempted_option.php

 $question_id=$_GET['question_id']; 
     $query="select * from question_attemped where question_id='$question_id'"; 
     $result=mysql_query($query) or die(mysql_error()); 
     $row=mysql_fetch_array($result); 
     echo $row['answer']; 
+0

も共有してください – brk

+0

は、あなたの$ question_id後にこれを入れてくださいHTML:$ question_id = mysql_real_escape_stringの($ question_id)。それはあなたの問題への答えではなく、ちょうど助言 – Ciro

+0

PHPスクリプトは値としてoption1を返す – swdpankaj

答えて

0

以下は、これを解決することができる方法についての少しの助けを提供するかもしれない - そうですこのような作業は大丈夫ですが、データベース検索ではテストしていません。操作上の効果を確認するために、これを「そのまま」実行することができます。

<?php 

    if($_SERVER['REQUEST_METHOD']=='GET' && !empty($_GET['question_id'])){ 
     ob_clean(); 

     $qid=filter_input(INPUT_GET,'question_id',FILTER_SANITIZE_NUMBER_INT); 


     /* 
      You would do the database lookup here ~ but please investigate 
      and start usin `prepared statements` as your code is vulnerable 
      to sql injection attack. 


      ie: 
      select * from question_attemped where question_id='$qid' 

     */ 




     /* but for demonstration simply send a basic, predefined response */ 
     echo 'option' . $qid; 

     exit(); 
    } 

?> 
<html> 
    <head> 
     <script> 
      function checked_attemped(qid){ 
       var callback=function(r,h){ 
        var input=document.getElementById(r); 
         if(input) input.checked=true; 
       }; 

       var xhr=new XMLHttpRequest(); 
       xhr.onreadystatechange=function(){ 
        if(xhr.readyState==4 && xhr.status==200)callback.call(this, xhr.response, xhr.getAllResponseHeaders()); 
       }; 
       xhr.onerror=function(e){ 
        console.log(e); 
       }; 
       xhr.open('GET', '?question_id='+qid, true); 
       xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
       xhr.send(); 
      } 
     </script> 
    </head> 
    <body> 
     <script language="javascript"> 
      window.onload=function(){ 
       [1,2,3,4,5].forEach(function(i){ 
        setTimeout(function(){ checked_attemped(i); }, i * 1000); 
       }); 
      } 
     </script> 

     <input name="option1" id="option1" type="radio" value="option1"> 
     <input name="option2" id="option2" type="radio" value="option2"> 
     <input name="option3" id="option3" type="radio" value="option3"> 
     <input name="option4" id="option4" type="radio" value="option4"> 
     <input name="option5" id="option5" type="radio" value="option5"> 
    </body> 
</html> 
関連する問題