本文介绍了如何将jQuery值传递给php?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这段代码,我想将值传递给PHP.
I have this little piece of code and I would like to pass the value to PHP.
var size; if ($(window).width() < 960) { size = "1"; } else { size = "2"; }有没有办法做到这一点? (是jQuery的新功能)
Is there a way to do this? (new at jQuery)
非常感谢!
推荐答案检查此代码
<script src="ajax.googleapis/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> var size; if ($(window).width() < 960) { size = "1"; } else { size = "2"; } $.ajax({ type: "POST", url: 'edit.php', data: "window_size=" + size, dataType: 'text', async: false, cache: false, success: function (result) { alert(result) } }); </script>然后创建edit.php
then create edit.php
<?php print_r($_REQUEST); ?>如果要保存此值,请使用此代码
if you want to save this value then use this code
<?php session_start(); echo $_SESSION['php_value']; ?> <script src="ajax.googleapis/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> var size; if ($(window).width() < 960) { size = "1"; } else { size = "2"; } $.ajax({ type: "POST", url: 'edit.php', data: "window_size=" + size, dataType: 'text', async: false, cache: false, success: function (result) { alert(result); //window.location.reload(); } }); </script>并像这样更新edit.php
and update edit.php as like
<?php session_start(); $_SESSION['php_value'] = $_REQUEST['window_size']; echo $_SESSION['php_value']; ?>您要查看此值或使用,然后刷新此页面 如果要在其他页面中使用此变量,请使用
you want to see this value or use then refresh this page if you want to use this variable in other page then use this
//first page first set it $_SESSION['php_value'] = $_REQUEST['window_size']; //second page or other page and use it $var_value = $_SESSION['php_value'];更多推荐
如何将jQuery值传递给php?
发布评论