我的代码用于获取输入文本并使用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>
答案 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 );