发送JavaScript变量PHP变量 [英] send javaScript variable to php variable

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

问题描述

可能重复:
  如何JavaScript变量传递到PHP?

首先,我认为,我不得不JavaScript的转换为PHP的,但后来我发现,着,因为服务器和客户端执行的。所以,现在我只是想送一个变量

First i thought that i had to convert javascript to php, but then i found out that cant because of server and client side executions. So now I simply want to send ONE variable

<script type="text/javascript">
function scriptvariable()
{        
    var theContents = "the variable";
}
</script>

到PHP变量

to a php variable

<?php
$phpvariable
?>

在的JavaScript函数执行时,可以说,我点击一个按钮。

That function in the javascript executes when lets say i click on a button.

现在我已经拿去想法如何获取phpvariable等于JavaScript的一个,这样我就可以使用phpvariable查找的东西在我的数据库。 我知道我可以将其添加到我的网址或某些东西,只是刷新页面..但我还想用AJAX做进一步在我的网页我可能要使用此方法阿贾克斯..

Now i have Nooo idea how to get that phpvariable Equal to the javascript one so i can use that phpvariable to look up stuff in my Database. I know i can add it to my url or some thing, and just refresh the page.. But id like to do it with AJAX as further in my webpage i might have to use this Ajax method..

那么,有没有一种简单的方法来做到这一点,而无需转储code网页在我的网页做一个简单的事情?

So is there an easy way to do this without having to dump pages of code on my page to do one simple thing?

推荐答案

乔丹已经说你要回发的JavaScript变量到服务器,然后服务器才能办理值。要做到这一点,你可以编写一个JavaScript函数,提交表单 - 或者你可以使用AJAX / jQuery的。 jQuery.post

As Jordan already said you have to post back the javascript variable to your server before the server can handle the value. To do this you can either program a javascript function that submits a form - or you can use ajax / jquery. jQuery.post

也许最适合你的最简单的方法是这样的

Maybe the most easiest approach for you is something like this

function myJavascriptFunction() { 
  var javascriptVariable = "John";
  window.location.href = "myphpfile.php?name=" + javascriptVariable; 
}

在你的myphpfile.php您可以使用 $ _ GET ['名称'] 你的JavaScript被执行后。

On your myphpfile.php you can use $_GET['name'] after your javascript was executed.

问候

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

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