Forums

PHP

This topic is locked

isset() ? - or Single page form submit and action.

Posted 23 Dec 2001 23:14:38
1
has voted
23 Dec 2001 23:14:38 Stephen Bateman posted:
Can someone help me out with a very simple way of having a single web page with a form on it and when clicking submit, the page reloads and completes an action (do a calculation in this case) and then display a result.

I am thinking of having two sections both inside IF statements like

if ($action == ""

DISPLAY FORM

if ($action == "submit"

DO CALC AND DISPLAY RESULTS

I think MM uses the isset() function but I just can't work it out.

The bottom line is I only want one page to make it easy to manage.

Thanks

GT

Replies

Replied 24 Dec 2001 00:30:06
24 Dec 2001 00:30:06 Tim Green replied:
The way to do this is to have a hidden field within your form as an indicator. The MM system is a perfect example of this. Use a tag such as this:-

<pre id=code><font face=courier size=2 id=code>
&lt;input type="hidden" name="formSeen" value="true"&gt;
</font id=code></pre id=code>

Now your processing function would be something like this :-

&lt;?php
if (isset($formSeen)) {
// Your code here
}
?&gt;

So, how does this work. Well when the page is first viewed the PHP interpreter reads the code block first and sees the condition. It checks it's variable pool for the variable $formSeen (which relates to your hidden tag}.

The first time it is run, this variable doesn't exist, so it returns "false" and the rest of the code block is ignored. So it then sends the HTML to the browser.

Now, when submit is clicked all the variables relating to form fields are stored (including $formSeen which is set to true). So when the PHP interpreter now looks at the code block $formSeen is now true and your code is executed.

Quite simple really.

The one thing to ensure, is that the action attribute of your form has it's value set to:-

&lt;?php echo $PHP_SELF; ?&gt;

This ensures that the form submits to itself.

I hope this helps

Merry Christmas!

Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>

Reply to this topic