Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Cant figure out how to echo name from form on submit with out linking to external php



<html>
<head>
<title>Sample web form</title>
</head>
<body>

Hello Form





<form>

Please enter your name:


<input type="text" name="name" autofocus>
<input type="submit" value="Submit Name">
</form>

if (!empty($_GET['name']))
{
$name = $_GET['name'];
echo "

Hello $name";
}
?>
</body>
</html>








<!--
<html>
<head>
<title>Sample web form</title>
</head>
<body>


Hello Form





<form action="hello.php" method="get">

Please enter your name:


<input type="text" name="name" autofocus>
<input type="submit" value="Submit Name">
</form>



</body>
</html>



<html>
<body>

--!>




HELP!

Posted
Comments
Sergey Alexandrovich Kryukov 22-Jan-16 16:06pm    
Not clear. What do you mean by "echo"? This is a PHP instruction, can only be used on server side, for example, to generate new HTML to be sent back to the client in HTTP response.
You can echo the whole page or a part of it. Not clear what do you mean by "external" PHP. External to what?
If you need to show some text of HTML elements on click of submit button, use JavaScript, not PHP.
—SA

1 solution

First of all, please see my comment to the question.

The element <form> makes little sense without the attributes action and method. (You show such thing in your code fragment below horizontal line.) It would be just rendered as a block element (or with different display properties, depending on your styles), no different from regular div. A form makes sense only if you really need to send some HTTP request to the server side, where PHP can process it. It's not clear what would you mean by "external" file. The URI used in your request sent via the Submit button is defined as the value of your action attribute, it could be some PHP file; the special case is when it is the same file as the file of your form, because usually you want to display the same form after postback and getting a refreshed page of the page. If this is the case (it's not clear from your question), you, basically, need to render whole HTML text via PHP echo, so you would get back to the same page with the same form, plus the result of handling the data sent in your form. If this is what you wanted to achieve and still have some question on this option, please clarify. Anyway, no matter what PHP you want to use and what is "external" and what is not, this is your first option.

Another option is not using PHP at all for your "echo". For this purpose, you don't need a form at all. You can have a regular button and add a click handler to it in JavaScript. The handler function can read any of your data from your control, handle it and add processed data in the form of any HTML added to your existing HTML DOM (or replacing part of existing content). This is pretty easy, but you may want to ask additional question if you have concern on some detail.

Finally, you could use compound approach which also does not require a form. If you still need some processing on server side, with PHP, you can send some arbitrary HTTP request and receive response using Ajax. You have to collect data from input control and send the request explicitly using JavaScript; the HTTP response is also handler with JavaScript. The benefit of this approach, in comparison with forms, is that you don't have to reload the whole page, instead, you just modify the HTML DOM on the part of the page, only in part relevant to the data received from HTTP response. This way, you can greatly, improve responsiveness of your application, reduce the use of network traffic, eliminate or reduce visual flicker, and so on. Please see: Ajax (programming) — Wikipedia, the free encyclopedia[^].

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900