dongnigeng1295 2018-04-28 03:58
浏览 46
已采纳

如何将php变量传递给javascript?

I am using following code to pass PHP variables to 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
    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:-

 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:

<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. but nin juavascript im getting only Annonymous why???? 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! Thanks in Advance

  • 写回答

2条回答 默认 最新

  • dongshuql24533 2018-04-28 04:58
    关注

    Uh, found another issue. The js is in smart quotes, which won't work... "`" is invalid. use "'". Also, w3schools can't handle php in their editor, so it's no use. Example for POST requests: https://www.w3schools.com/code/tryit.asp?filename=FQSA8MJYGJ47

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵