0

I want to display the value entered in the form field in the same page. I have created custom php template for the page. Its showing 404 when I submit. Please help how to achieve this.

<table> 
<form action="" method="POST"> 
<tr> 
<td> Name: </td><td><input type="text" name="name"></td> 
</tr> 
<tr> 
<td><input value="Submit" type="submit" /></td></tr> 
</form> 
</table> 

<?php 
if ($_SERVER['REQUEST_METHOD'] === 'POST') 
{
 $NAME = $_POST["name"]; 
echo $NAME;
 }

1 Answer 1

-1

you should set the form action to custom_template.php with this change, when the form is submitted, it will post the data back to the same page custom_template.php, and your PHP script will process the form data and display the entered name on the same page.

<table> 
<form action="custom_template.php" method="POST"> <!-- Ensure form action is set to the same page -->
<tr> 
<td> Name: </td><td><input type="text" name="name"></td> 
</tr> 
<tr> 
<td><input value="Submit" type="submit" /></td></tr> 
</form> 
</table> 

<?php 
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST["name"]; 
    echo "<p>Name: $name</p>"; // Displaying the entered name
}
?>
2
  • Hi thanks for the help. I found the answer. I am using this template in WordPress. I changed the input field name to some other name and its working correctly. I kept form action as blank
    – Manikandan
    Mar 15 at 8:17
  • 1
    Are you aware that template files are never called directly in WordPress?
    – fuxia
    Mar 15 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.