Zigsaw Consultancy
Zigsaw Consultancy

Reputation: 135

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

Jobs.php goes as

<?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

<?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. Thanks in advance

Upvotes: 1

Views: 5588

Answers (2)

Stoycho Trenchev
Stoycho Trenchev

Reputation: 555

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.

Upvotes: 0

Professor Abronsius
Professor Abronsius

Reputation: 33813

If you echo anything before the html tag it would effectively make the html invalid. Also, the head of the document MUST not have presentational html elements such as forms, divs etc

<?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

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


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

Upvotes: 2

Related Questions