使用Jquery将Javascript值转换为PHP [英] Javascript value to PHP with Jquery

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

问题描述

我的JavaScript文件:

My javascript file:

var ms = 3000;
$.get("curl.php", { test: ms } );

我的curl.php:

My curl.php:

$ms = $_GET("test");
echo $ms;

Firefox萤火虫说:

Firefox firebug says:

致命错误:函数名称必须为字符串 第2行上的C:\ Users \ Jansu \ Documents \ workspace \ php \ curl.php

Fatal error: Function name must be a string in C:\Users\Jansu\Documents\workspace\php\curl.php on line 2

可能是什么问题?

更好的情况是当javascript和php代码在同一文件中时,所以我不必发布/获取任何内容.只是以某种方式将javascript传递给php.

Even better would be when the javascript and php code is in the same file, so I don't have to post/get anything. Just somehow pass javascript to php.

推荐答案

您要的是[],而不是()(文档):

You want [], not () (docs):

$ms = $_GET["test"];


重新


Re your edit:

更好的是,当javascript和php代码在同一文件中时,所以我不必发布/获取任何内容.只是以某种方式将javascript传递给php

even better would be when the javascript and php code is in the same file, so i do not have to post/get anything. just somehow pass javascript to php

即使它们位于同一文件中,也可以在完全不同的位置执行它们. JavaScript在客户端上执行,PHP在服务器上执行.为了将数据从客户端传送到服务器,必须将其发送. Ajax(您的操作方式)是在不刷新整个页面的情况下进行操作的主要方式,因此您的工作方向正确.

Even if they're in the same file, they're executed in completely different places. The JavaScript is executed on the client, the PHP is executed on the server. To get data from the client to the server, it has to be sent there. Ajax (the way you did it) is the primary way you do that without causing a full page refresh, so you're on the right track.

这篇关于使用Jquery将Javascript值转换为PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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