Forum Moderators: coopster

Message Too Old, No Replies

Submit form to itself

Silly me can't get it to work...

         

andrewshim

11:39 am on May 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Newbie here. I can't seem to get the PHP_self to work. Rather than further confusing myself by showing my code here, can someone show me how to code a basic form with one text input that submits the data to itself (displaying the data in the same text input)?

or help point me to the most basic tutorial.

Appreciation in advance ;-)

phparion

11:48 am on May 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I read somewhere PHP_SELF is not good option to be used for some security reasons so you should hard code the page name...

say we have a page form.php

<form action=form.php method=post>
<input type=text name=uname value="<?php echo $_REQUEST['uname'];?>">

<input type-submit value=post>
</form>

kaleb

8:26 pm on May 9, 2007 (gmt 0)

10+ Year Member




<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="text" name="uname" value="<?php echo $_REQUEST['uname'];?>" />
<input type="submit" value="post" />
</form>

andrewshim

2:34 am on May 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks very much phparion and kaleb...

Just one more request ... based on kaleb's code, I'd like to have a default value for the text input that should be retained if the visitor doesn't key in anything. How should I code that?

Million thanks ; - )

andrewshim

6:51 am on May 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



errr... I hope this doesn't make me look foolish but can I do something like this :

<input type="text" name="uname" value="<?php if (!isset($_POST['submit'])) {echo $_REQUEST['uname']; } else {echo '0';}?>" />

to get the input field to display a default value?

Obviously I can't coz... I'm getting a totally blank page... but am I on the right track?

=(

coopster

12:50 pm on May 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



One practice is to typically "set" the fields prior to HTML display. Also, when submitting a form to itself you may want to at least strip any tags that may have been injected into the URI. PHP_SELF is user-supplied data. Anything that is user-supplied cannot be trusted.

<?php
$uname = (isset($_POST['uname'])) ? $_POST['uname'] : 'default value';
?>
<form action="<?php echo strip_tags [php.net]($_SERVER['PHP_SELF']); ?>" method="post">
<input type="text" name="uname" value="<?php echo htmlentities [php.net]($uname); ?>" />
<input type="submit" value="post" />
</form>

andrewshim

12:50 am on May 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



coopster...

wonderful! it works...
I built my form based on this... and it works great except the values don't revert back to 'default value' if I place a reset button or refresh the page.

Thanks so much for taking the time to teach...