我到底要通过参数发送什么?

发布于 2024-11-17 16:06:10 字数 3791 浏览 0 评论 0原文

当执行 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:很抱歉在答案区发表我的评论,第一次来这里。

When doing a XMLHttpRequest and using POST as the form method, what exactly am I sending? I know it should be like send(parameters), parameters = "variable1=Hello", for example. But what if I want to do this:

parameters = "variable1=" + encodeURIComponent(document.getElementById("variable1").value);

variable1 being the id of the HTML form input.

Can I do it like this or do I need to assign the encodeURIComponent value to a javascript variable and send that variable:

var variable2;
parameters = "variable2=" + encodeURIComponent(document.getElementById("variable1").value);

You're suppose to send the object and it's value, but, is it an object from the HTML form, a javascript object or a php object? The problem is I already tried it and I still can't get the encoded input in my database, all I get is the raw input from the user.

BTW, I know it's a pretty dull question, but I feel the need to understand exactly what I'm doing if I want to come up with a solution.


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!";
    }

I did that but the value that the database got was the raw input and not the encoded input. I'm running out of ideas, don't know what else to try. Could it be the settings of the database, can the database be decoding the input before storing it? That seems far-fetched to me, but I've been looking at this from all angles and still can't come up with a fresh answer.

PS: Sorry for posting my comments on the answer area, first timer here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我ぃ本無心為│何有愛 2024-11-24 16:06:10

创建查询字符串时,它实际上与对象或类似的东西无关。您想要发送的只是键/值对。如何构造它取决于您,但首先将值分配给变量通常更整洁且更易于管理。即

var myVar1Value = encodeURIComponent(document.getElementById('variable1').value);
var myVar2Value = encodeURIComponent(document.getElementById('variable2').value);
var url = "http://www.mydomain.com?" + "var1=" + myVar1Value + "&var2=" + myVar2Value;

它被称为查询字符串,所以它只是一个字符串。你在服务器端对它所做的事情就是让“魔法”发生的原因。

编辑:如果您遇到值问题,那么您应该将它们打印到控制台以验证您是否得到了预期的结果。

when creating query strings, it has really nothing to do with objects or anything like that. All you want to be sending is key/value pairs. how you construct that is up to you, but it often neater and more manageable to assign your values to variables first. i.e.

var myVar1Value = encodeURIComponent(document.getElementById('variable1').value);
var myVar2Value = encodeURIComponent(document.getElementById('variable2').value);
var url = "http://www.mydomain.com?" + "var1=" + myVar1Value + "&var2=" + myVar2Value;

It's called a query string, so it's just a string. what you do with it on the server side is what makes the 'magic' happen.

edit: If you're having problems with values, then you should print them to console to verify you are getting what you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文