【问题标题】:adding radio button handle to ajax script将单选按钮句柄添加到 ajax 脚本
【发布时间】:2012-05-27 13:50:06
【问题描述】:

这个 ajax 函数获取一个文本框值并将其发送给"response.php":(这是ajax.js 的主要函数)

function ajaxFunction() {
  var getdate = new Date(); 

  if(xmlhttp) {

    var txtname = document.getElementById("txtname");

    xmlhttp.open("POST","response.php",true); 
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("txtname=" + txtname.value); 
  }
}

我正在尝试向我的表单添加一个单选按钮,所以我将其更改为:

function ajaxFunction() {

  var getdate = new Date();  
  if(xmlhttp) {

    var txtname = document.getElementById("txtname");
    var radio = document.getElementById("radio2"); //ADDED

    xmlhttp.open("POST","response.php",true); 
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("txtname=" + txtname.value); 
    xmlhttp.send("radio=" + radio.value); //ADDED

  }
}

在 response.php 中:

<?php

if (isset($_POST['txtname'])){
 $radio =  $_POST['radio'];

       //process...

}

?>

输入文本仍然有效,但单选按钮无效。

【问题讨论】:

    标签: php javascript ajax


    【解决方案1】:

    将post参数绑定在一个并post

      var parameters = 'radio='+radio.value+'&txname='+txtname.value;
      xmlhttp.send(parameters);
    

    【讨论】:

      【解决方案2】:

      您的变量名似乎不是“radio2”而是“radio”。

      <?php 
      if (isset($_POST['txtname'])){
         // $radio =  $_POST['radio2']; // <-- here
         $radio =  $_POST['radio']; // fixed
      
           //process...
      }
      ?>
      

      因为你以“电台”的名义发布数据。

      xmlhttp.send("radio=" + radio.value); //ADDED
      

      -----在下面添加了我的答案----

      请尝试...

      xmlhttp.send("txtname=" + txtname.value + "&radio="+ radio.value); 
      

      xmlhttp.send("txtname=" + txtname.value + "&radio2="+ radio.value); 
      

      因为你不能使用 xmlhttp.send() 两次。

      我希望这对你有帮助。

      【讨论】:

      • 我在测试不同的方式时改变了这个。这样,值只添加到response.php?ect.. url,但我不能处理页面本身的值。它破坏了ajax funcionality。但我还是编辑了我的问题。
      • 你能告诉我你的 HTML 代码吗?
      • 就是这样&lt;input type="radio" name="choose" id="radio2" value= "c1"/&gt;&lt;label for="radio2"&gt;choice1&lt;/label&gt; &lt;input type="radio" name="choose" id="radio2" value= "c2" /&gt;&lt;label for="radio2"&gt;choice2&lt;/label&gt;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 2013-08-09
      • 1970-01-01
      • 2016-08-04
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多