$ _POST不显示当前选项

时间:2015-02-19 00:17:51

标签: javascript php html ajax post

我的代码用于获取输入文本并使用PHP通过AJAX为其生成输出。设置$_POST["inputText"]$_POST["style"]未设置。如何修改我的代码以将$_POST["style"]设置为当前选项(a,b或c)?

functions01.php是任何通用的PHP文件,它会使$_POST["inputText"]$_POST["style"]回显适当的输出。

foo1.php

<!DOCTYPE html>
<html>  
  <body>
	<!-- Input -->
    <div class="form">
      <form onsubmit="makeRequest(); return false">
        <input type="text" id="inputText" name="inputText">
        <select name="style">
          <option value="a">A</option>
          <option value="b">B</option>
          <option value="c">C</option>
        </select>
        <input type="submit">
      </form>
    </div>
	
    <br>
    
    <!-- Output -->
    <div class="txtBox">
      <textarea  id="txtBox">
      </textarea>
    </div>
    
    <!-- AJAX to create output using function01.php file-->
    <script>
        function makeRequest() {
            httpRequest = new XMLHttpRequest();            
            httpRequest.onreadystatechange = function() {               
                document.getElementById("txtBox").innerHTML = httpRequest.responseText;
            };
            httpRequest.open("POST", "functions01.php", true);
            httpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            httpRequest.send("inputText=" + document.getElementById("inputText").value);
        }
        
        //function 
    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

HTML

<select name="style" id="ddlStyle">

JS

  var inputVal = document.getElementById("inputText").value;

  var e = document.getElementById("ddlStyle");
  var styleVal = e.options[e.selectedIndex].value;
  ...
  httpRequest.send("inputText=" + inputVal + "&style=" +  styleVal );