在执行XMLHttpRequest
并使用POST
作为表单方法时,我究竟发送了什么?我知道它应该像send(parameters),parameters =“variable1 = Hello”,例如。但如果我想这样做会怎么样:
parameters = "variable1=" + encodeURIComponent(document.getElementById("variable1").value);
variable1是HTML表单输入的id。
我可以这样做,还是需要将encodeURIComponent值分配给javascript变量并发送该变量:
var variable2;
parameters = "variable2=" + encodeURIComponent(document.getElementById("variable1").value);
你想发送对象及其值,但是,它是来自HTML表单,javascript对象还是php对象的对象?问题是我已经尝试过了,我仍然无法在我的数据库中获取编码输入,我得到的只是用户的原始输入。
顺便说一句,我知道这是一个非常沉闷的问题,但如果我想提出一个解决方案,我觉得有必要准确理解我在做什么。g
function createObject()
{
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer")
{
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
//INSERT
function insert()
{
var Faculty2 = encodeURIComponent(document.getElementById("Faculty").value);
var Major2 = encodeURIComponent(document.getElementById("Major").value);
var Professor2 = encodeURIComponent(document.getElementById("Professor").value);
var Lastname2 = encodeURIComponent(document.getElementById("Lastname").value);
var Course2 = encodeURIComponent(document.getElementById("Course").value);
var Comments2 = encodeURIComponent(document.getElementById("Comments").value);
var Grade2 = encodeURIComponent(document.getElementById("Grade").value);
var Redflag2 = encodeURIComponent(document.getElementById("Redflag").value);
var Owner2 = encodeURIComponent(document.getElementById("Owner").value);
//Location and parameters of data about to be sent are defined
//Required: verify that all fields are not empty. Use encode URI() to solve some issues about character encoding.
var params = "Faculty=" + Faculty2 + "&Major=" + Major2 + "&Professor=" + Professor2 + "&Lastname=" + Lastname2 + "&Course=" + Course2 + "&Comments=" + Comments2 + "&Grade=" + Grade2 + "&Redflag=" + Redflag2 + "&Owner=" + Owner2;
var url = "prehp/insert.php";
http.open("POST", url, true);
//Technical information about the data
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
//Now, we send the data
http.onreadystatechange = function()
{
if(http.readyState == 4)
{ var answer = http.responseText;
document.getElementById('insert_response').innerHTML = "Ready!" + answer;
}
else
{document.getElementById('insert_response').innerHTML = "Error";
}}
http.send(params);
}
PHP code:
$insertAccounts_sql = "INSERT INTO Information (Faculty, Major, Professor, Lastname, Course, Comments, Grade, Redflag, Owner)
VALUES('$_POST[Faculty]','$_POST[Major]','$_POST[Professor]','$_POST[Lastname]','$_POST[Course]','$_POST[Comments]','$_POST[Grade]','$_POST[Redflag]','$_POST[Owner]')";
$dbq = mysql_query($insertAccounts_sql, $dbc);
if($dbq)
{
print "1 record added: Works very well!";
}
else
if(!$dbq)
{
die('Error: ' . mysql_error());
}
$dbk = mysql_close($dbc);
if($dbk)
{
print "Database closed!";
}
else
if(!$dbk)
{
print "Database not closed!";
}
我这样做但数据库得到的值是原始输入而不是编码输入。我的想法已经不多了,不知道还有什么可以尝试的。可能是数据库的设置,数据库可以在存储之前解码输入吗?这对我来说似乎很牵强,但我一直在从各个方面看待这个问题,但仍然无法得出一个新的答案。
PS:很抱歉在答案区发布我的评论,这是第一个计时器。
答案 0 :(得分:0)
创建查询字符串时,它实际上与对象或类似的东西无关。您要发送的只是键/值对。你如何构建这取决于你,但它通常更整洁,更易于管理,首先将你的值分配给变量。即。
var myVar1Value = encodeURIComponent(document.getElementById('variable1').value);
var myVar2Value = encodeURIComponent(document.getElementById('variable2').value);
var url = "http://www.mydomain.com?" + "var1=" + myVar1Value + "&var2=" + myVar2Value;
它被称为查询 string ,所以它只是一个字符串。你在服务器端用它做什么是让“神奇”发生的原因。
编辑:如果您遇到值问题,那么您应该将它们打印到控制台以验证您是否达到了预期效果。