如何将jQuery值传递给php?

编程入门 行业动态 更新时间:2024-10-27 22:25:11
本文介绍了如何将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?

本文发布于:2023-07-22 13:40:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1187994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   jQuery   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!