php变量中的Javascript变量 [英] Javascript variable within a php variable

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

问题描述

我有php函数,用于检查数据库中是否存在用class::checkThis($my_entry);

I have php function that checks if an entry exists in my database that I call with class::checkThis($my_entry);

在我的脚本中,javascript用于确定我选择的文件夹的名称,因此$ my_entry应该看起来像这样:

In my script javascript is used to determined the name of the folder I am selecting, so $my_entry is supposed to look like this :

(通常由php确定,粗体部分由javascript确定)

C:/库/用户/apatik/文件夹1

您猜我找不到混合这些语言的有效方法,而且我还没有真正的JavaScript经验来弄清楚这一点.

As you guess I can't find a working way to mix up thoses languages, and I don't really have any experience in javascript yet to figure out this.

返回路径第一部分的php代码就是$_SESSION['cwd'].'/',返回所选文件夹名称的javascript变量是data_name,由var data_name = $(this).attr('data-name');

The php code that returns the first part of the path is simply $_SESSION['cwd'].'/' and the javascript variable that returns the selected folder's name is data_name and is determined by var data_name = $(this).attr('data-name');

有没有办法得到像if (class::checkThis($_SESSION['cwd'].'/'.data_name) == true)这样的东西?

Is there a way to get something like if (class::checkThis($_SESSION['cwd'].'/'.data_name) == true) ?

到目前为止,我没有尝试过任何方法,但是在寻找替代方法时遇到了麻烦.

None of what I tried so far worked and I'm having troubles finding an alternative.

感谢您的帮助

推荐答案

Ajax jQuery是您最好的解决方案. 这个使用带有事件的post方法,仅供参考...

Ajax jQuery is your best solution. This one using post method with event, just for reference...

<a href="#" class="anchor" data-name="folder1">Am I exist?</a>
<a href="#" class="anchor" data-name="folder2">Am I exist too?</a>

$('.anchor').click(function(){

    var data_name = "name=" + $(this).attr('data-name');
    $.ajax({
        type: "POST",
        url: "is_exist.php",
        data: data_name,
        success: function (data) {
                    alert(data.response);
        }
    });
};

is_exist.php

if(isset($_POST['name'])){
    $data['response'] = class::checkThis($_SESSION['cwd'].'/'.$_POST['name']) == true ? "exist" : "buried";
    echo json_encode($data);

}

这篇关于php变量中的Javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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