我嘗試讓用戶單擊提交按鈕時多提交一次論壇。爲什麼?我嘗試向購物車添加更多然後一個同義詞,我正在使用的購物車軟件不支持一次添加多個產品,我不想在那裏編輯核心代碼。隱藏的論壇會有'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