[英]How to access $_POST value from an external jquery file?
I am not great at Jquery and PHP I was coding in a way that sometimes I needed my javascript variables to get php post values something like that. 我不太擅长Jquery和PHP,我编码的方式有时需要我的javascript变量来获取类似这样的php post值。
var articleCategoryA2 = '<?php if(isset($_POST['A2']))echo $_POST['A2'];?>';
When I was done with the coding I wanted to get everything organized I decided to have a javascript code in one single file, but it won't interact with the embeded php code in it after I include the javascript code in the php file that gets the post values 当我完成编码后,我希望将所有事情组织起来,我决定在一个文件中包含一个javascript代码,但是在将javascript代码包含在php文件中之后,它不会与其中的php代码交互。职位价值
What should I do? 我该怎么办? For being noob I haven't got a clue.
因为是菜鸟,所以我没有头绪。
Javascript files won't be compiled by php so if you need a php variable passed in you need to put it in a separate script tag Javascript文件不会由php编译,因此如果您需要传入一个php变量,则需要将其放在单独的脚本标签中
<script>
var articleCategoryA2 = '<?php if(isset($_POST['A2']))echo $_POST['A2'];?>';
</script>
<script src="app.js"></script>
Now the variable is available in app.js
. 现在,该变量在
app.js
可用。
One approach is to initialize the variable in JavaScript, then conditionally assign the value of that variable. 一种方法是在JavaScript中初始化变量,然后有条件地分配该变量的值。
So in your JavaScript file: 因此,在您的JavaScript文件中:
var articleCategoryA2; //defined, but null
...then inline within your PHP file: ...然后内联到您的PHP文件中:
<?php
if(isset($_POST['A2'])){
/* if $_POST['A2'] is set, put its value into the JavaScript variable articleCategoryA2 */
echo('
<script>
articleCategoryA2='.$_POST['A2'].';
</script>
');
};
?>
If $_POST['A2']
exists, JavaScript will set its value into articleCategoryA2
. 如果
$_POST['A2']
存在,JavaScript会将其值设置为articleCategoryA2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.