[英]Accessing the PHP Variable in javascript
Good day to all of you Ma'am and Sir女士们和先生们大家好
I have a problem to my project (never mind the name of the project because it is for privately use and it is for security of the website).我的项目有问题(不要介意项目的名称,因为它是私人使用的,而且是为了网站的安全)。
The problem is I can not access or forward the PHP Variable value to the external javascript file.问题是我无法访问或转发 PHP 变量值到外部 javascript 文件。
this is the code in insert.php这是 insert.php 中的代码
<?php
$username = $_POST['username'];
?>
<input type="text" id="code"/>
<button id="con_btn"/>Continue</button>
this is the code in insert_auth.js这是 insert_auth.js 中的代码
$(document).ready(function(){
$("#con_btn").click(function(){
var code = $("#code").val();
$.ajax({
method: "post",
url: "post.php",
data:{
code:code,
},
success: function(data){
$("#insertresult").html(data);
}
});
});
});
I just want to get the value of the $username from php file.我只想从 php 文件中获取 $username 的值。 Thanks in advance!提前致谢!
I take it as you already have the POST value : $_POST['username'];
我认为您已经拥有 POST 值: $_POST['username'];
. . So, I'd suggest you to store this value as a hidden input field.因此,我建议您将此值存储为隐藏的输入字段。
<?php $_SESSION['username'] = $_POST['username']; // Store username in session ?>
<input type="text" id="code"/>
<button id="con_btn"/>Continue</button>
Now, in your JS, you can access this hidden field value:现在,在您的 JS 中,您可以访问此隐藏字段值:
$(document).ready(function(){
$("#con_btn").click(function(){
var code = $("#code").val();
var username = "<?php echo $_SESSION['username']; ?>" // Read username from session
$.ajax({
/* Other code */
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.