Skip to content
Advertisement

How to pass variable using post method?

There are a page page1.php below:-

<?php
    $name = "xyz";
?>
<form action="page2.php" method="POST">
    Age : <input type="number" name="age">
    <button type="submit">SUBMIT</button>
</form>

after submitting the form at page1, How to get value of $name of page1.php in page2.php what should change in page-1?

Advertisement

Answer

If you ask also for a name on page1, then simply use another input:

<form action="page2.php" method="POST">
    Name : <input type="text" name="name">
    Age : <input type="number" name="age">
    <button type="submit">SUBMIT</button>
</form>

If the name is fixed and you want it transmitted via POST, usa a hidden input:

<form action="page2.php" method="POST">
    Name : <input type="hidden" name="name" value="<?php echo htmlspecialchars($name) ?>">
    Age : <input type="number" name="age">
    <button type="submit">SUBMIT</button>
</form>

Then in page2.php read the POST-ed value:

$name = $_POST['name'];
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement