开发者

Passing in data from Ajax to PHP

开发者 https://www.devze.com 2023-03-09 04:56 出处:网络
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. I know it passes in lo

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. I know it passes in long 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 wo开发者_JAVA技巧rked?

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. As long as your target script (e.g.: sort.php) starts the session, session data should be available in that script as well. 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:

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

Then in sort.php:

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

精彩评论

暂无评论...
验证码 换一张
取 消