PHP Javascript变量帮助 [英] PHP Javascript variable help
问题描述
有没有办法将javascript变量传递给php函数或只是将js变量赋值给php变量.... ???
Is there any way to pass a javascript variable to a php function or simply assign a js variable to a php variable....???
test( asdas);我需要将asdas更新为动态值,即表单,
test("asdas"); I need to update "asdas" to a dynamic value, i.e of the form,
推荐答案
您可以将值从js传递给PHP使用ajax请求或将js值添加到URL然后重新加载页面。
You can pass value from js to PHP using ajax request or add js value to URL and then reload page.
变体#1(使用ajax):
JS方(jquery)
JS side (jquery)
var js_var = 'hello';
$.ajax({
type: "POST",
url: "some.php",
data: "js_var="+js_var,
success: function(msg){
alert( "Data Saved: " + msg );
}
});
PHP方
$ js_var = isset($ _ POST ['js_var'])? $ _POST ['js_var']:'';
$js_var = isset($_POST['js_var']) ? $_POST['js_var'] : '';
变体#2(页面重新加载):
JS方
<script type="text/javascript">
<!--
var js_var = 'hello';
window.location = "http://www.yoursite.com/?js_var="+js_var;
//-->
</script>
PHP side
$ js_var = isset($ _ GET ['js_var'])? $ _GET ['js_var']:'';
$js_var = isset($_GET['js_var']) ? $_GET['js_var'] : '';
这篇关于PHP Javascript变量帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!