[PHPJavaScript]: Call PHP file through JavaScript code with argument - Stack Overflow

I want to call a PHP file but want to pass an argument to the PHP file. Not getting the correct approac

I want to call a PHP file but want to pass an argument to the PHP file. Not getting the correct approach, I am attempting to write a cookie and read that cookie when the PHP file loads. But this is also not working. I am using following code to write and read cookie. I just want to test the read cookie function of JavaScript here. I know how to read the cookie value in PHP.

<script>
    function SetRowInCookie(NewCookieValue)
    {
        try
        {
            alert(NewCookieValue);
            document.cookie = 'row_id=' + NewCookieValue;
            loadCookies();          
        }
        catch(err)
        {
            alert(err.description);
        }
    }

    function loadCookies() {
    var cr = []; if (document.cookie != '') {
    var ck = document.cookie.split('; ');
    for (var i=ck.length - 1; i>= 0; i--) {
      var cv = ck.split('=');
      cr[ck[0]]=ck[1];
    }
    }
        alert(cr['row_id']);
    }   
</script>

I want to call a PHP file but want to pass an argument to the PHP file. Not getting the correct approach, I am attempting to write a cookie and read that cookie when the PHP file loads. But this is also not working. I am using following code to write and read cookie. I just want to test the read cookie function of JavaScript here. I know how to read the cookie value in PHP.

<script>
    function SetRowInCookie(NewCookieValue)
    {
        try
        {
            alert(NewCookieValue);
            document.cookie = 'row_id=' + NewCookieValue;
            loadCookies();          
        }
        catch(err)
        {
            alert(err.description);
        }
    }

    function loadCookies() {
    var cr = []; if (document.cookie != '') {
    var ck = document.cookie.split('; ');
    for (var i=ck.length - 1; i>= 0; i--) {
      var cv = ck.split('=');
      cr[ck[0]]=ck[1];
    }
    }
        alert(cr['row_id']);
    }   
</script>
Share Improve this question edited Dec 25, 2012 at 2:22 hakre 198k55 gold badges449 silver badges856 bronze badges asked Nov 4, 2009 at 13:36 RKhRKh 14.2k52 gold badges159 silver badges279 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

I'm not sure what in your code (running on the client's PC) you expect to cause the php script (running on the server) to run. You'll need to invoke the php by making some kind of http request (like get http://yoururl/recheckcookie.php). With at HTTP request, the javascript code on the client to queries the webserver for the output of your recheckcookie.php script. This script can then recheck the cookie, and return some/no output.

Look up XMLHttpRequest or preferably the corresponding JQuery to see how to perform the HTTP request.

Cookies are not the way to transfer variables between client and server. you should append key/variables pairs to your request URL using either a get (querystring) or post method.

jQuery ajax example;

$.get('http://www.myphpserver./script.php?row_id=' + NewCookieValue);

I think, you dont need cookies. try it with $.post, where you can define which url will be called, something like:

$.post(url, params, callback_function);

Well I'm not sure what it is you are ultimately trying to achieve but it sounds like using AJAX could be your solution. There is a good tutorial here.

AJAX will basically allow you to call a php script, pass it variables and then use it's output on your webpage.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744699626a4588701.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>