用php从URL中获取参数,并在if条件下将其写入js变量 [英] Parameter from url with php and write it into js variable in an if condition

查看:105
本文介绍了用php从URL中获取参数,并在if条件下将其写入js变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用php从网址中获取一个参数并将其写入js变量中,以进行一些jquery的工作.

I am trying to get a parameter from the url with php and write it into a js variable to do some jquery stuff.

我的网址看起来像

www.example.com/?v=12345

www.example.com/?v=12345

我的代码

vnew = "<?php echo $_GET["v"];?>";
if (vnew  == null) {
myfunction();
}
else {
$(".myDiv").attr('src','newsrc');   
$(".title").html("bla");
};

如果该网址类似于www.example.com,则该值不应该为'null',这样我的函数会触发吗? 但是,如果我将其设置为"12345",则else条件也不会触发.

if the url is like www.example.com shouldn't the value be 'null', so that my function fires? However if i set it to '12345' the else condition does not fire either.

这是怎么了?谢谢!

推荐答案

像这样更改它:

<?php if ($_GET["v"]) { ?>
    myfunction();
<php } else { ?>
    $(".myDiv").attr('src','newsrc');   
    $(".title").html("bla");
<?php } ?>

OR

<?php 
if ($_GET["v"]) {
    echo "myfunction();";
} else {
    echo "$(\".myDiv\").attr('src','newsrc'); $(\".title\").html(\"bla\");";
} ?>

检查您的状况是否正常:

to check if your condition is working:

<?php 
if ($_GET["v"]) {
    echo "myfunction();";
} else {
    echo "Hello";
} ?>

这篇关于用php从URL中获取参数,并在if条件下将其写入js变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆