简体   繁体   English

如何将php变量传递给javascript?

[英]how to pass php variable to javascript?

I am using following code to pass PHP variables to javascript. 我正在使用以下代码将PHP变量传递给javascript。 but it is not working. 但它不起作用。

 function gto(str) {
   document.getElementById('goto').action = str;
   document.getElementById('ID').value = <?php echo "$userid" ?>;
   document.getElementById('name').value = <?php echo "$user_name" ?>;
   document.getElementById('gname').value = <?php echo "$usergname" ?>;
   document.getElementById('fmname').value = <?php echo "$userfname" ?>;
   document.getElementById('img').value = <?php echo "$userimg" ?>;
   document.getElementById('email').value = <?php echo "$useremail" ?>;
   document.getElementById('goto').submit();
  }

Following is the PHP code 以下是PHP代码

<?php
    if($_POST["name"] == null)
    {
        $user_name = 'Annomyous';   
    }
    else{
        $user_name = $_POST["name"];
        $userid=  $_POST["id"];
        $usergname=  $_POST["gname"];
        $userfname=  $_POST["fname"];
        $userimg=  $_POST["img"];
        $useremail=  $_POST["email"];
    }
    echo "<p style='color : white'>$user_name";
    echo "$userid" ;
    echo "$gname";
    echo "$fname";
    echo "$img";
    echo "$email";
    echo "$user_name";
    echo "$user_name</p>";
    $user_name =htmlspecialchars($user_name);
    $user_name =str_replace("<script>","", $user_name);
    ?>


the output is a follows:
ReAlItY TuTs104598758504708047866ReAlItY TuTsReAlItY TuTs//this is php echo output.

JAVASCRIPT OUTPUT:- JAVASCRIPT输出:-

 function gto(str) {
       document .getElementById('goto').action = str;
       document.getElementById('ID').value = ;
       document.getElementById('name').value = Annomyous;
       document.getElementById('gname').value = ;
       document.getElementById('fmname').value = ;
       document.getElementById('img').value = ;
       document.getElementById('email').value = ;
       document.getElementById('goto').submit();
   }

Function gto is called here: 函数gto在这里被调用:

<button class="w3-btn header-btn" onclick="gto('Contact.php');">Contact Us</button>

I can see in PHP output I am getting all variable output. 我可以在PHP输出中看到所有变量输出。 but nin juavascript im getting only Annonymous why???? 但是nin juavascript im只是匿名而已? I need to pass post variables to contact us so i am using the form tag and javascript but this is not working Please help me! 我需要传递帖子变量才能与我们联系,所以我正在使用表单标签和javascript,但这无法正常工作,请帮帮我! Thanks in Advance 提前致谢

values from php to js can be passed in many ways, here's one of them 从php到js的值可以通过多种方式传递,这是其中一种
try to pass the php values when calling the js function, like this:- 尝试在调用js函数时传递php值,例如:

<button onclick="gto('Contact.php','<?php echo $userid;?>','<?php echo $username;?>');">Contact Us</button>

and then get values on js function like this:- 然后像这样获取js函数的值:

 function gto(str,userid,username) {

   document .getElementById('goto').action = str;
   document.getElementById('ID').value = userid;
   document.getElementById('name').value =username;

 }

that's it, now use the values in js as your wish 就是这样,现在按照您的意愿使用js中的值

Uh, found another issue. 嗯,发现了另一个问题。 The js is in smart quotes, which won't work... "`" is invalid. js用智能引号引起来,这将不起作用...“`”无效。 use "'". 采用 ”'”。 Also, w3schools can't handle php in their editor, so it's no use. 另外,w3schools无法在其编辑器中处理php,因此没有用。 Example for POST requests: https://www.w3schools.com/code/tryit.asp?filename=FQSA8MJYGJ47 POST请求的示例: https : //www.w3schools.com/code/tryit.asp?filename=FQSA8MJYGJ47

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM