[PHP/JavaScript]: Call PHP file through JavaScript code with argument

后端 未结 4 920
不思量自难忘°
不思量自难忘° 2021-01-28 14:44

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 fil

相关标签:
4条回答
  • 2021-01-28 15:13

    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);
    
    0 讨论(0)
  • 2021-01-28 15:14

    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.

    0 讨论(0)
  • 2021-01-28 15:15

    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.

    0 讨论(0)
  • 2021-01-28 15:25

    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.com/script.php?row_id=' + NewCookieValue);
    
    0 讨论(0)
提交回复
热议问题