我尝试让用户单击提交按钮时多提交一次论坛。为什么?我尝试向购物车添加更多然后一个同义词,我正在使用的购物车软件不支持一次添加多个产品,我不想在那里编辑核心代码。隐藏的论坛会有'1,2,3'这样的产品ID,然后我需要JavaScript来分隔这些值并将每个值使用AJAX发布到购物车。我并不是一个JavaScript,但我编码了我认为应该工作的内容,但它只是给了我一个警告:'请求存在问题'。两次。我看不出它有什么问题,欢迎任何帮助和建议!下面的代码:多次提交表单
JS
<script type="text/javascript">
function testResults (form) {
var product_id = form.product_id.value;
var quantity = form.quantity.value;
var brokenstring=product_id.split(",");
for (var i in brokenstring)
{
var http_request = false;
function makePOSTRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById('myspan').innerHTML = result;
} else {
alert('There was a problem with the request.');
}
}
}
var poststr = "product_id=" + encodeURI(brokenstring[i]) +
"&quantity=" + encodeURI(quantity);
makePOSTRequest('post.php', poststr);
}
}
</script>
HTML
<form action="javascript:testResults(document.getElementById('myform'));" name="myform" id="myform">
<input type="text" name="product_id" id="product_id" />
<input type="hidden" name="quantity" id="quantity" value="1" />
<br />
<input type="submit" name="button" value="Submit" />
</form>
<span name="myspan" id="myspan"></span>
post.php中
<?php
print_r($_POST);
?>
许多拼写错误使理解困难。你的意思是表格不是正确的? – whatnick
同上→项目。那里 - >他们的。这里 - >这是。 – recursive
对不起!我已经睡了12个小时以上,我的糟糕拼写变得更糟,我的睡眠减少了。 – Scott