0

この 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...

}

?>

入力テキストは機能しますが、ラジオ ボタンは機能しません。

4

2 に答える 2

1

投稿パラメーターを 1 つにバインドして投稿する

  var parameters = 'radio='+radio.value+'&txname='+txtname.value;
  xmlhttp.send(parameters);
于 2012-05-27T14:36:40.783 に答える
0

変数名が「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()を2回使用することはできないためです。

これがお役に立てば幸いです。

于 2012-05-27T14:01:04.807 に答える