我使用HTML / AJAX / PHP创建了一个表单。表格要求将网站输入其中一个字段,这是一个例子:
http://www.teamexpress.com/catalog/thumbnail.jsp?N=46&36&Ns=PRODUCT_AFFINITY%7C1&Nty=1&Ntk=Def&Ntx=mode%20matchallpartial&Ntt=sale15&sc=TBX
输入字段id =" sample_page"它转移到我的AJAX / Php脚本很好,唯一的问题是$ _POST数据正在拆分网站地址:
$_POST["sample_page"] is returning this: http://www.teamexpress.com/catalog/thumbnail.jsp?N=46
而不是如上所述的完整地址。看起来$ _POST数据正在将网站拆分为"&"或" ="。我确信有一个简单的解决方法可以解决这个问题,因为我对这一切都不熟悉。
感谢您的帮助。
编辑:这是我正在使用的脚本:
post_vars正在console.log上打印出来,如下所示:
store_name=a&sample_page=http://www.teamexpress.com/catalog/thumbnail.jsp?N=46&36&Ns=PRODUCT_AFFINIT…&product_misc_info_2=//&product_misc_info_3=//&product_container_search=0&
这是我的AJAX请求:
var new_site_array = ["store_name", "sample_page", "product_container", "product_title", "product_link", "product_image", "product_price", "product_original_price", "product_discount", "product_misc_info_1", "product_misc_info_2", "product_misc_info_3"];
var post_data_array = [];
function fix_container_search_syntax(search_value, container_search_checked)
{
if(container_search_checked == 1)
{
search_value = "./" + search_value;
}
else
{
search_value = "//" + search_value;
}
return search_value;
}
function new_site_validate_test(new_site_array)
{
//Check form for any problems
var new_site_fields_filled_in = 12;
var alert_text = "";
var show_alert = 0;
//Check if Container Search is checked
var container_search_checked = 0;
if(document.getElementById("product_container_search").checked)
{
container_search_checked = 1;
}
//
//Check if more than one field is entered and sample page has http:// format
//
for (i=0; i< new_site_array.length; i++)
{
var check_new_site_form = document.getElementById(new_site_array[i]).value;
if(new_site_array[i] == "sample_page" && check_new_site_form.indexOf("http://") < 0)
{
show_alert = 1;
alert_text += "Sample Page is not formatted correctly\n\n";
}
if(check_new_site_form.length < 1 )
{
new_site_fields_filled_in--;
}
if(i>2)
{
var syntax_fix_form_field = fix_container_search_syntax(check_new_site_form, container_search_checked);
post_data_array.push(new_site_array[i] + ';' + syntax_fix_form_field );
}
else if(i==2)
{
post_data_array.push(new_site_array[i] + ';//' + check_new_site_form);
}
else
{
post_data_array.push(new_site_array[i] + ';' + check_new_site_form);
}
}
//
//Add field to see if product container search is checked
//
post_data_array.push("product_container_search;" + container_search_checked);
//Alert Section
if(new_site_fields_filled_in < 2)
{
show_alert = 1;
alert_text += "More Than 1 Field Including the Sample Page Field must be filled in.\n";
}
//
//If Any Alerts show alert windows and stop function else continue to test
//
if(show_alert>0)
{
alert(alert_text);
}
//
//Run Test Scrape with fields
//
else
{
var hr = new XMLHttpRequest();
var url = "./php/new_site_test.php";
var post_vars = "";
var array_split;
for(i= 0; i<post_data_array.length; i++)
{
array_split = post_data_array[i].split(";")
post_vars = post_vars + array_split[0] + "=" + array_split[1] + "&"
}
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function()
{
console.log(hr);
if(hr.readyState == 4 && hr.status == 200)
{
console.log(post_vars);
var return_data = hr.responseText;
document.getElementById("results_inner").innerHTML = "";
document.getElementById("results").style.display = "block";
document.getElementById("results_inner").innerHTML = return_data;
console.log(return_data);
}
}
hr.send(post_vars);
}
}
我希望这段代码不会太乱。这是我第一次尝试这样做,因为我是自学成才。我最初尝试通过JSON文件放置值,但似乎无法弄明白。
Php现在很简单,只是为了验证:
echo($_POST["sample_page"] . "</br>");
答案 0 :(得分:1)
post_vars = post_vars + array_split[0] + "=" + array_split[1] + "&"
您需要使用encodeURIComponent
post_vars = post_vars +
encodeURIComponent(array_split[0]) +
"=" +
encodeURIComponent(array_split[1]) +
"&";