简体   繁体   English

将隐藏值传递给输入标记中的php变量

[英]Passing hidden value to a php variable in input tag

I am new to coding and pardon me if it a silly thing I am missing. 我是新手,如果我想念这很愚蠢的事情,请原谅我。 I have searched through the forum & did not find an answer that suits my need. 我已经在论坛中搜索过,没有找到适合我需要的答案。 I have 2 files: jobs.php & jobprocess.php 我有2个文件:jobs.php和jobprocess.php

Jobs.php goes as Jobs.php变成

<?php session_start();
include('dbConnect.php');
$q1="abc";  
$q2="pqr";  
$q3="xyz";  
$opportunity=29;    
echo "Opportunity is". $opportunity;
?>

<html>
<head>
<div align="center">

<form method="post" method="post" action="jobprocess.php">


<input type="text" name="q1" placeholder="<?php echo $q1;?>"><br>
<input type="text" name="q2" placeholder="<?php echo $q2;?>"><br>
<input type="text" name="q3" placeholder="<?php echo $q3;?>"><br>
<input type="hidden" name="opportunity" value="<?php echo $opportunity;?>">


<ul class="actions">
<li><input type="submit" name="submit" value="I would like to join!! "></li>
</ul>                           
</form>                                 
</div>
</head>
<body>
</body>
</html>

jobprocess.php goes with the code jobprocess.php与代码一起

<?php session_start();
include('dbConnect.php');


$opportunity = $_GET['opportunity'];
echo "opportunity is " . $opportunity;
?>

Unfortunately, the above code is not defining value="29" for opportunity on 2nd page. 不幸的是,以上代码没有在第二页上为机会定义value =“ 29”。 Thanks in advance 提前致谢

If you echo anything before the html tag it would effectively make the html invalid. 如果您在html标记之前回显任何内容,则将有效地使html无效。 Also, the head of the document MUST not have presentational html elements such as forms , divs etc 另外,文档的head一定不能包含表示forms html元素,例如formsdivs等。

<?php
    session_start();
    include('dbConnect.php');
    $q1="abc";  
    $q2="pqr";  
    $q3="xyz";  
    $opportunity=29;    

?>

<html>
    <head>
        <title>must have a title</title>
    </head>
    <body>
        <?php
            echo "Opportunity is". $opportunity;
        ?>
        <div align="center">
            <form method="post" method="post" action="jobprocess.php">
                <input type="text" name="q1" placeholder="<?php echo $q1;?>"><br>
                <input type="text" name="q2" placeholder="<?php echo $q2;?>"><br>
                <input type="text" name="q3" placeholder="<?php echo $q3;?>"><br>
                <input type="hidden" name="opportunity" value="<?php echo $opportunity;?>">
                <ul class="actions">
                    <li><input type="submit" name="submit" value="I would like to join!! "></li>
                </ul>                           
            </form>                                 
        </div>
    </body>
</html>

And because the form is set to POST you should probably check and use the POSTed variable rather than a GET variable 并且由于表单设置为POST您应该检查并使用POSTed变量而不是GET变量

<?php
    session_start();
    include('dbConnect.php');


    $opportunity = $_POST['opportunity'];
    echo "opportunity is " . $opportunity;
?>

Surprisingly my answer that suggested using session variable instead of hidden form field was deleted?! 令人惊讶的是,我的建议使用会话变量而不是隐藏的表单字段的答案已删除? I guess session variables are illegal now? 我猜会话变量现在是非法的吗?

The answer was chosen for the best answer even. 答案被选为最佳答案。

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

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