简体   繁体   中英

radio button values send using ajax php mysql

Please help me with this.

It could be a duplicate question, but I couldn't find the solution anywhere.

I am creating an objective type questionnaire and the options are in radio buttons. Some of the options are mandatory and if the user click that option the comment box will change to a required field. The name of the answers are the question Id. Here is the input field which I am using

<input type='radio' name='answer_value[<?php echo $gques; ?>]' value='<?php echo $gans; ?>' id="rtr" onclick='ajaxFunction()'/>    

I want to do it with ajax, because when the user select the mandatory option it has fetch the answer id and use that id to execute a query to check whether that answer is mandatory or not. If true it will make the comment box a required text area. The ajax code I used is

function ajaxFunction(){    
   var ajaxRequest;    
       try{    
           ajaxRequest = new XMLHttpRequest();    
       }catch (e){    
       try{    
           ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");    
       }catch (e) {    
       try{    
           ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");    
       }catch (e){    
           alert("Your browser broke!");    
       return false;    
             }    
          }    
       }    
ajaxRequest.onreadystatechange = function(){    
if(ajaxRequest.readyState == 4){    
   var ajaxDisplay = document.getElementById('ajaxDiv');    
   ajaxDisplay.innerHTML = ajaxRequest.responseText;    
      }    
   }    

var ans_id = $("input[id=rtr]:checked").val();    
        var dataString = 'id='+ ans_id;    
        alert(dataString);    

        ajaxRequest.open("POST", "anschq.php" +dataString,true);    
        ajaxRequest.send();    
    }    

In the anschq.php I put an alert script to check whether the value is sent or not. But when I click on the radio button the alert box is not displayed from the other page. But the alert(dataString) here is displaying the value of the checked button.

Can anyone find the solution for this problem.....

This is not a GET request. You have to pass the data in the body of the response not in the url (see RFC ).

Replace:

ajaxRequest.open("POST", "anschq.php" + dataString, true);    
ajaxRequest.send();

by

ajaxRequest.open("POST", "anschq.php", true);    
ajaxRequest.send(dataString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM