Insert $_POST into HTML form action

Asked by At

I want to insert $_POST value into the HTML form action. By using my code I got this error message: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) ....

This is my sample code in my PHP file:

print <<< HERE
<form action="../../path/to/file.php?id=<?php echo $_POST['mytext'];?>" method="POST">
    // some stuff here ...
    <input type="text" name="mytext" id="mytext" value="">
    <input type="submit" value="Submit">
</form>
HERE;

What is wrong with my code?

1 Answers

1
Zain Farooq On Best Solutions

You don't need to add id in the form action

<form action="../../path/to/file.php" method="POST">
// some stuff here ...
<input type="text" name="mytext" id="mytext" value="">
<input type="submit" value="Submit">
</form>

Instead in the file.php you can make a variable and assign $_POST['mytext'] to that variable

$id = $_POST['mytext'];