在PHP中使用Javascript变量 [英] Use Javascript variable in PHP

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

问题描述

我在之前的帖子中已经看到了这个问题的一些答案,但没有人给出一个真正的工作示例,只是伪造的代码。有没有人曾经这样做过?

I have seen some answers to this question in previous posts, but no one has given a real working example, just psuedo code. Has anyone ever done this before?

基本上,我所拥有的是javascript(jquery)中的变量,我想使用此变量来驱动查询(对于覆盖窗口)我要去在php中运行。

Basically, what i have is a variable in javascript (jquery), and i want to use this variable to drive a query (for an overlay window) i am going to run in php.

从我读过的内容你可以使用对同一页面的ajax调用来做到这一点所以它不会刷新自己,但我必须遗漏一些因为我不能让它运作...

From what i have read you can do this using an ajax call to the same page so it doesnt refresh itself, but i must be missing something because i can't get it working...

那里有任何例子吗?

谢谢。

更新2010年6月21日:
好​​的,我试图解决但仍然遇到一些问题......这就是我所拥有的。我在edit_1.php中处理的页面。基于Firebug控制台,页面(edit_1.php)正在接收正确的'editadid'。

UPDATE 6/21/2010: Ok, i tried to work through but still having some problems...here is what i have. The page I am working on in edit_1.php. Based on Firebug console, the page (edit_1.php) is receiving the correct 'editadid'.

当我尝试回显它时,我得到一个'未定义的变量'错误但是......任何事情都可以看到我错过了这里?

When i try to echo it out though, i get an 'Undefined variable' error though...anything y'all can see i missed here?

这是javascript:

Here is the javascript:

var jsVariable1 = $(this).parent().attr('id');
var dataString = 'editadid=' + jsVariable1;
$.ajax({ 
    url: 'edit_1.php', 
    type: 'get', 
    data: dataString, 
    beforeSend: function() {

    },
    success: function (response) { 

    } 
});

这是我的php:
if(isset($ _ GET ['editadid']) )
{
$ editadid =(int)$ _ GET ['editadid'];

Here is my php: if(isset($_GET['editadid'])) { $editadid = (int)$_GET['editadid'];

}

echo $ editadid;

echo $editadid;

推荐答案

我想说的是你必须了解ajax是如何工作的,而不是寻找一个例子。如何通过ajax访问URL并将查询参数与它们一起传递(这些可以是您正在寻找的javascript变量)如何在javascript中捕获服务器端响应并用于操作现有页面dom。或者更好的是你可以发布你尝试过的东西,有人可以为你纠正它。

I would say instead of looking for an example you must understand how ajax works. How can you hit a URL via ajax and pass query parameters along with them (these can be the javascript variables you are looking for) How server side response is captured back in javascript and used into manipulate existing page dom. Or Much better you can post what you have tried and somebody can correct it for you.

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

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