将 jquery 值分配给 php 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36026673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assign jquery value to php variable
提问by vignesh raj kumar
In same page i set the jquery value while click the button. i want pass the value to php variable in same page without form submitting.
在同一页面中,我在单击按钮时设置了 jquery 值。我想在不提交表单的情况下将值传递给同一页面中的 php 变量。
<input type="button" name="next" class="next btn btn-primary" value="Proceed To Checkout Page" />
Jquery
查询
$(".next").click(function(){
<?php $var1="1";?>
}
While check the php value
同时检查php值
<?php if(isset($var1)){
echo $var1;
}else{
echo "NULL";
}?>
Every time time i got the null value. Where i getting mistake. Ps: I cant able to send the ajax call to get the value
每次我得到空值时。我哪里出错了。Ps:我无法发送ajax调用来获取值
回答by Ali Zia
You simply cannot do that, you need to understand the difference between client/server side programming, you cannot assign Javascript value to PHP variable, yea but you can assign PHP value to your javascript.
您根本无法做到这一点,您需要了解客户端/服务器端编程之间的区别,您不能将 Javascript 值分配给 PHP 变量,是的,但是您可以将 PHP 值分配给您的 javascript。
You can use cookies to achieve this.
您可以使用 cookie 来实现这一点。
In Javascript:
在 JavaScript 中:
<script type="text/javascript">
document.cookie = "var1=1";
</script>
And in PHP
在 PHP 中
<?php
$phpVar = $_COOKIE['var1'];
echo $phpVar;
?>
回答by prabhu programmer
If at all you want to send php value to php page using jquery and ajax, then first of all, create input text with type hidden. put your php value in it. and when you click then get that value from that input type hidden in jquery and then pass it to whichever page you want.
如果您想使用 jquery 和 ajax 将 php 值发送到 php 页面,那么首先,创建类型为 hidden 的输入文本。把你的 php 值放在里面。当您单击然后从隐藏在 jquery 中的输入类型中获取该值,然后将其传递到您想要的任何页面。
Do some thing like this.
做一些这样的事情。
$(".next").click(function (){
var php_val=$("#php_val").val();//it is getting value from hidden filed whose id is 'php_val'.
//make ajax call with this as follows
$.post("a.php",{php_val:php_val},function(data){
//a.php is page name where u want to send php value.
})
});
<?php $var=1; ?>
<input type="hidden" id="php_val" value="<?php echo $var; ?>"/>
I hope this answers your questions
我希望这能回答你的问题
回答by Vishnu R Nair
It will help you ,
它会帮助你,
<script>
$(document).ready(function() {
$(".next").click(function() {
<?php echo $var = 1 ; ?>
});
});
</script>
<?php
if (!empty($var)) {
echo $var;
}
?>