如何在PHP中显示同一页面上的jQuery变量值? [英] How to display jQuery variables value in PHP on the same page?
问题描述
我有jQuery和PHP代码,我想在PHP $ nicks变量中获取jQuery变量值,然后回显这个$ nicks值,但是在同一页面上。
< script type =text / javascript>
var axel = 131.5678466;
< / script>
<?php
$ nicks = isset($ _ GET ['axel']);
echo $ nicks;
?>
因此,这个输出应该是131.5678466
你不能直接从jquery到php,因为每个代码都在哪里执行。 Jquery是一种仅与前端交互的客户端/浏览器端语言。您也可以将其视为用户直接与之交互的任何内容。 PhP是一种在后端工作的服务器端语言。这通常涉及Web服务器本身处理用户无法控制的代码的操作。
有了这个说法,您不能直接从jquery到php访问变量。 ..但你可以欺骗它以得到你需要的东西。
你可以从jquery发送一个变量到php会话,这将允许你访问变量。 p>
您可以从jquery发送一篇文章到PHP脚本
index.php
<?
session_start();
?>
< head>
src =https://code.jquery.com/jquery-3.1.1.min.js
integrity =sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8 =
crossorigin = 匿名 >< /脚本>
< script>
$(document).ready(function(){
var axel = 131.5678466;
$ .ajax({
type:POST,
url: data.php,
data:{axel:axel},
success:function(data){
}
})
})
< / script>
< / head>
<?php
$ nicks = $ _SESSION ['axel'];
echo $ nicks;
?>
data.php
在php中,您可以获取发布数据并将其发送到会话变量,如
<
session_start();
$ _SESSION ['axel'] = $ _POST ['axel'];
?>
然后返回到您的jquery页面,您可以访问数据到php
由于您正在使用会话,因此您必须确保您想要访问PHP页面标题的会话变量的任何页面。
I have jQuery and PHP code and I want to get jQuery variable value in PHP $nicks variable and then echo this $nicks value, But on the same page.
<script type="text/javascript">
var axel = 131.5678466;
</script>
<?php
$nicks = isset($_GET['axel']);
echo $nicks;
?>
So with this the output should be 131.5678466
You can't access straight from jquery to php because of where each of the codes execute. Jquery is a client/browser side language that only interacts with the front end. You can also think of this as anything that a user interacts with directly. PhP is a server side language that works in the backend. This usually involves the web server itself process the actions of the code where a user does not have control.
With that said you cannot access variables directly from jquery to php....but you can trick it to get what you need.
You can send a variable to a php session from jquery which would allow you to access the variable.
You can start with sending a post to a php script from jquery
index.php
<?
session_start();
?>
<head>
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<script>
$(document).ready( function() {
var axel = 131.5678466;
$.ajax({
type: "POST",
url: "data.php",
data: {axel: axel},
success: function(data) {
}
})
})
</script>
</head>
<?php
$nicks = $_SESSION['axel'];
echo $nicks;
?>
data.php
In php you can get the post data and send it to a session variable like so
<?
session_start();
$_SESSION['axel'] = $_POST['axel'];
?>
Then back to your jquery page you can access the data to php
Since you are working with sessions you have to make sure that any page where you want to access a session variable that the header of the php page has.
这篇关于如何在PHP中显示同一页面上的jQuery变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!