简体   繁体   English

使用ajax php mysql发送单选按钮值

[英]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. 答案的名称是问题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. 我想用ajax来做,因为当用户选择强制性选项时,它已经获取了答案ID并使用该ID执行查询以检查该答案是否是强制性的。 If true it will make the comment box a required text area. 如果为true,它将使注释框成为必需的文本区域。 The ajax code I used is 我使用的ajax代码是

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. 在anschq.php中,我放置了一个警报脚本来检查该值是否已发送。 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. 但是这里的alert(dataString)显示选中按钮的值。

Can anyone find the solution for this problem..... 谁能找到解决这个问题的办法.....

This is not a GET request. 这不是GET请求。 You have to pass the data in the body of the response not in the url (see RFC ). 您必须在响应的正文中而不是在url中传递数据(请参阅RFC )。

Replace: 更换:

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

by 通过

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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