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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧