douruobokui58233 2016-11-05 15:10 采纳率: 0%
浏览 164
已采纳

将php变量作为隐藏输入传递,其中html包含在一个echo中

I'm new with PHP and still learning.

<?php
if(isset($_POST['btnLogin'])){
    $myVariable = $_POST['fieldParameter'];

    if(condition){
        //do something
    }else{

      echo "  
        <form method='POST' action='submit.php'><br/>       
          <input type='hidden' name='myVariable' value='<?php echo $myVariable; ?>'/>
          <br/>
          <input type='submit' name='btnSubmit' id='submit' value='Submit'>
        </form>

          ";
        }
}
?>

Notice that the variable $myVariable is contained in the main IF block. I'm trying to send the value of $myVariable to submit.php as hidden field.

Also, i enclosed all the html tags using one echo statement with double quotes.

I found related questions here in SO but can't find similar to embedding php within a long echo of html tags

I tried to put value='<?php echo $studentNo; ?>' with no success.

I want to access it in a submit.php file like this,

submit.php

<?php 
    $aVariable = $_POST['myVariable'];
    echo $aVariable;
?>

How can I pass the value contained in $myVariable as hidden field? Is there something wrong with the way I use double and single quotes?

Thank you.

  • 写回答

2条回答

  • douaoren4402 2016-11-05 15:20
    关注

    You just need to type $myVariable instead of in your string. Double quotes "" only creates a string literal. It doesn't directly output data like inline HTML. As you can see from the syntax coloring in StackOverflow, the

    You can try these variants (simplified):

    // code before
    echo "<input type='hidden' name='myVariable' value='$myVariable'/>";
    // code after
    
    // OR //
    
    // code before
    ?>
    <input type='hidden' name='myVariable' value='<?= $myVariable ?>'/>
    <?php
    // code after
    

    Note that the quotes you use in HTML don't affect PHP, as long as you escape them properly (use \" and \' where appropriate).

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

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn