0
我的目标是通过ajax函数提交整个表单,然后将这些值发送到php文件。我使用Post有几个原因 - 一个是我将在MySQL数据库中更改数据。使用Ajax的Post方法发送多个值的问题
我在查找答案的时候看过几个例子,但他们都没有解决我直接遇到的问题。
举例来说,下面这个例子:
var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);
// Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
// Call a function when the state changes.
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
开始解决这一问题。
具体而言,我不知道如何去发送多个值。在上面我发现的例子中,我看到他们在变量params中硬编码了两个值到他们的语句中。我想知道如何将这些值传递给函数,而不必对它们进行硬编码。
我的形式:
<table border="0" cellpadding="2" cellspacing="5">
<th colspan="2" align="center">Check Out</th>
<form name="checkOut" method="post" onSubmit="return(validate(this))">
<tr>
<td>Territory Number</td>
<td><input type="text" name="numberOut" tabindex="1" maxlength="3" size="3" /></td>
</tr>
<tr>
<td>First Name of Publisher</td>
<td><input type="text" onKeyUp="showHint(this.value)" name="fName" tabindex="2" maxlength="15"/></td>
</tr>
<tr>
<td>Last Name of Publisher</td>
<td><input type="text" onKeyUp="showHint_l(this.value)" name="lName" tabindex="3" maxlength="15" /></td>
</tr>
<tr>
<td>Special Campaign</td>
<td><input type ="checkbox" name="specialC" tabindex="4" value="Yes"/></td>
</tr>
<tr>
<td><input type="button" onClick="clearForm()" value="Reset" /></td>
<td><input type="submit" value="Check Out" /></td>
</tr>
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</table>
在这里,我想通过numberOut,FNAME,LNAME和specialC的值。我知道如何单独传递每一个,但不是作为一个组。最终,我希望能够通过php以$ _POST ['fName']来访问这些值。
从看到这个网站上类似问题的几个答案,许多人说使用jQuery。我知道这很容易,但我想先学习使用ajax,因为我是基于Web的语言的新手。
这正是我一直在寻找的答案。谢谢! – Mlagma