[PHP / JavaScript]:通过带有参数的JavaScript代码调用PHP文件 [英] [PHP/JavaScript]: Call PHP file through JavaScript code with argument

查看:84
本文介绍了[PHP / JavaScript]:通过带有参数的JavaScript代码调用PHP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用PHP文件,但想将参数传递给PHP文件。没有得到正确的方法,我试图编写一个cookie并在PHP文件加载时读取该cookie。但这也行不通。我正在使用以下代码来编写和读取cookie。我只想在这里测试JavaScript的读取cookie功能。我知道如何在PHP中读取cookie值。

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>


推荐答案

Cookie不是在客户端和客户端之间传输变量的方法服务器。你应该使用get(querystring)或post方法将键/变量对附加到你的请求URL。

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;

jQuery ajax example;

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

这篇关于[PHP / JavaScript]:通过带有参数的JavaScript代码调用PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆