简体   繁体   English

将数据从 Ajax 传递到 PHP

[英]Passing in data from Ajax to PHP

Can anyone explain the following script to me.谁能向我解释以下脚本。 I am trying to pass in $user data value so that I can use $_REQUEST['user'] inside of sort.php but I am having trouble doing so.我正在尝试传入 $user 数据值,以便可以在 sort.php 中使用 $_REQUEST['user'] ,但这样做时遇到了麻烦。 I know it passes in long URL.我知道它通过了很长的 URL。

$(function () {
    $('#sorter').submit(function () {
        $.ajax(
            {
            data: 
                {
                    longurl: $('#longurl').val()
                }, 
            url: 'sort.php', 
                complete: function (XMLHttpRequest, textStatus) 
                {
                    $('#longurl').val(XMLHttpRequest.responseText);
                  }
              });
          return false;
      });
  });

I have tried adding something like the marked line inside right after longurl but it hasn't worked?我曾尝试在 longurl 之后添加类似标记线的内容,但它没有奏效?

data: {
    longurl: $('#longurl').val()
    url: '<?php echo $_SESSION[username]; ?>'      ///<------------------
}, 

Any pointers would be very helpful,任何指针都会非常有帮助,

Thanks in advance提前致谢

You do not need to pass the session variable through AJAX.您不需要通过 AJAX 传递 session 变量。 As long as your target script (eg: sort.php) starts the session, session data should be available in that script as well.只要您的目标脚本(例如:sort.php)启动 session,session 数据也应该在该脚本中可用。 In your case, I'm thinking that it is also probably more secure.就您而言,我认为它也可能更安全。

But to answer your question, if you want to send a variable that would be accessible in a PHP script as $_REQUEST['user'] , you'd do:但是要回答您的问题,如果您想将 PHP 脚本中可访问的变量作为$_REQUEST['user']发送,您可以:

$.ajax({
   url: '/url/to/sort.php',
   data: {
       user: 'something',
   },
});

Then in sort.php :然后在sort.php中:

echo $_GET['user']; // something

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM