Click to See Complete Forum and Search --> : Forms in PHP


Ben H
03-01-2006, 09:00 AM
Hi

I'm a newbie to PHP (have only written sites in old style ASP 3.0). I am hoping for an easy crossover.

My question is ...in all the PHP examples I have seen in using Forms in PHP pages - the Form always directs (method=post) the user to a new page e.g. from "Collect_Data.php" to "Process_Data.php". Why is this ?

Can you not direct the user to the same php page to process the form data ? I am used to having my script code at the top of the page before any html to process the incoming data.

Hopefully I can carry on my way of writing code - unless there is a specific reason for this separation of processing ?

thanks for any help
Ben

Jona
03-01-2006, 09:22 AM
Hi Ben,

The PHP is processed before HTML is output on the web page (or in some cases, as HTML is output on the page), much like ASP does. When you submit a form, your browser takes the information that was entered in that form, encodes it, and sends it to the web server where PHP is running. In order for the information to be sent (and received by PHP), the form’s action (the PHP page processing the form) must be accessed, which requires a page load. Following the submission of the form, PHP has the data and can process it and output it.

In summary, the browser and PHP only have limited, indirect connectivity, with the server being the medium by which the two communicate; a page load is required for your browser to send data to the server (which is given to PHP to process) and for the server to send back data (the result of PHP’s processing) to the user. It’s just the way server-side languages work, due to the level of connectivity they have with browsers.

Hope this helps.

Ben H
03-01-2006, 09:33 AM
Thanks Jona

From your reply I guess I can carry on writing code in "my normal" way, i.e. I tend to write all my Form presentation/handling including database updates all on the same page.

Ben

bokeh
03-01-2006, 09:36 AM
Can you not direct the user to the same php page to process the form data ? Yes it is possible to use the same script for all three steps (form, processing and success). In fact doing so is advantageous from the point of view of making a sticky form. Success page though is best handled by a redirect to avoid warnings after form submission if the back button is pressed.

Jona
03-01-2006, 09:37 AM
You can have the form submit to the same PHP file as the page it’s on. (Did that make sense?)

What I mean is, you can have a form on form.php, and in that form have <form action="form.php" ... >, and when that form is submitted, it will reload the current page, but process the form.

Ben H
03-01-2006, 09:40 AM
Makes perfect sense to me

thanks Jona, thanks bokeh

vicky6an
03-11-2006, 02:46 AM
Hi i am a newbie to php.
I have a form with two text box and a submit button.
When user click the submit button the details entered in the textboxes must be sent to an e-mail ID. Is there any way for that in PHP?????
Pls give me the code.........