使用javascript来分配一个php变量 [英] Using javascript to assign a php variable

查看:96
本文介绍了使用javascript来分配一个php变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从envato市场购买modalbox插件在javascript我想使用它来实现我的类别>子类别>项目。无论如何我知道JavaScript变量可以分配一个值使用PHP,但我需要相反。

I purchased modalbox plugin from envato market place made in javascript i want to use it to implement my categories> subcategories> items. Anyhow i know JavaScript variable can be assigned a value using php however i need the opposite.

在我的第一步,我让用户选择一个类别使用基本链接,然后onclick甚至

On my first step i let the user pick a category using basic links then onclick even takes them to second slide where i want to display the subcategories and items related to this category that was selected.

我尝试了两件事:

1) setting onClick = "step2();<?php $chosen_cat = ' . $this->db->escape(trim($category)) . '?>"

希望我可以简单地将所选类别的名称赋值为一个值,然后使用$ chosen_cat step2()javascript方法显示产品,但是这打破了javascript,因为在输出中有冲突;

Hoping i can simply assign the name of the selected category to a value and then just use $chosen_cat in step2() javascript method to display products however this breaks the javascript as there is a conflict in output;


到步骤2(类别)然后使用,
onclick =step2(db-> escape($ category););这传递
值到step2()方法,但是现在我需要调用< - 我需要打印输出
字符串到php,但我不知道如何解决这个
考虑PHP - > JS是可行的作为其服务器到客户端关系,但
回来..

2) I changed step2() to step2(category) and then used, onclick="step2(db->escape($category););" this passes the value to the step2() method however now i would need to call <-- I would need to print out that string to php however i have no idea how to solve this considering PHP -> JS is viable as its server to client relation but back..

$ this- > db-> escape()是一个 http://codeigniter.com/user_guide/database/queries。 html codeigniter方法。

The $this->db->escape() is a http://codeigniter.com/user_guide/database/queries.html codeigniter method.

我的代码:

<script type="text/javascript">

        function step1() {
            modalbox.show(new Element("div").insert(
                new Element("p", { "align": "justify" }).insert(
                    <?php 
                        $categories = $items;
                        $chosen_cat = '';
                    ?>
                    <?php 
                        $i = 0;
                        foreach($categories as $category => $itemarray) {
                            $i++;
                            if($i==27){
                                echo  $this->db->escape('<a class="category" onclick="step2();<?php $chosen_cat = ' . $this->db->escape(trim($category)) . '?>" href="#">'. trim($category) . '</a>');
                            }
                            else {
                                echo $this->db->escape('<a class="category" onclick="step2();" href="#">' . trim($category) . '</a>')  . '+';
                            }
                        }
                    ?>
                )
            ), {
                "title"     : "Step 1/3",
                "width"     : 800,
                "options"   : [{
                    "label"     : "Next »",
                    "onClick"   : step2
                }]
            });
        };

        function step2() {
            alert(<?php $this->db->escape($chosen_cat); ?>);
            modalbox.show([ new Element("div").insert(
                new Element("p", { "align": "justify" }).insert(
                    "If you open a modalbox when another one is opened, it will stretch to the new " +
                    "modalbox width and height and overwrite the content.<br /\><br /\>" +
                    "You can use HTML or Prototype Elements like a DIV or a TABLE."
                )
            ),
                new Element("p", { "align": "justify" }).insert(
                    "You can have multiple contents on a single modalbox by using an array like this:<br /\><br /\>" +
                    "<pre>modalbox.show([ content1, content2, ... ], options);</pre>"
                )
            ], {
                "title"     : "Step 2/3",
                "width"     : 800,
                "options"   : [{
                    "label"     : "« Previous",
                    "onClick"   : step1
                }, {
                    "label"     : "Next »",
                    "onClick"   : step3
                }]
            }); 
        };
        function step3() {
            modalbox.show([ new Element("div").insert(
                new Element("p", { "align": "justify" }).insert(
                    "If you open a modalbox when another one is opened, it will stretch to the new " +
                    "modalbox width and height and overwrite the content.<br /\><br /\>" +
                    "You can use HTML or Prototype Elements like a DIV or a TABLE."
                )
            ),
                new Element("p", { "align": "justify" }).insert(
                    "You can have multiple contents on a single modalbox by using an array like this:<br /\><br /\>" +
                    "<pre>modalbox.show([ content1, content2, ... ], options);</pre>"
                )
            ], {
                "title"     : "Step 3/3",
                "width"     : 800,
                "hideOnPageClick": false,
                "options"   : [{
                    "label"     : "« Previous",
                    "onClick"   : step2
                }, {
                    "label"     : "Finish",
                    "isDefault" : true,
                    "onClick"   : modalbox.hide
                }]
            });
        };

    </script>

可以在onclick事件中使用JavaScript设置会话变量,例如$ _SESSION ['category '] ='category'在js风格,然后读取该会话与PHP和取消设置它我不认为这将创建很多安全问题,因为会话存在不足一秒。

Would it be possible to set a session variable with JavaScript in the onclick event say $_SESSION['category'] = 'category' in js style and then read that session with php and unset it i dont think this would create much of a security problem as session would exist for prolly less than a second.

推荐答案

JavaScript永远不会设置PHP变量。 JavaScript在客户端上运行,PHP在服务器上运行。如果您需要在JS上更改服务器上的某些内容,您必须使用AJAX或发送常规的HTTP请求。

JavaScript is never going to set a PHP variable. JavaScript runs on the client and PHP on the server. If you need to change something on the server from JS, you have to use AJAX, or send a regular HTTP request.

或者,您可以让PHP生成数据是在客户端运行时需要的,通常通过打印一些JSON来成为一个JS变量。

Alternatively, you can have PHP generate the data that is needed at runtime on the client, usually by printing some JSON to become a JS variable

在你的例子中,你需要类似下面的东西为了简单起见)

In your example, you would need something like the following (I'll use jQuery for simplicity)

... onclick="$.ajax('/category.php').done(function(data) {step2(data.category);})"

HTML中的JS

$('#my-button').click(function(){
    $.ajax('/category.php').done(function(data) {
        step2(data.category);
    });
});

我不能给你一个AJAX教程,但主要的是, AJAX请求,当您需要来自服务器的数据。

I can't give you an AJAX tutorial, but the main thing is that you'll make an AJAX request when you need data from the server.

这篇关于使用javascript来分配一个php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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