tags:

views:

36

answers:

2

I have a picture upload inside a form... The file is a php file btw...

Problem is whenever this form is filled in, and the user clicks to upload the first picture, the form is submitted to itself and all the fields which the user may have filled in will go blank...

I know of one way to do it, alot of 'isset' in my php code, but is there any simpler or maybe better way I don't know of?

Thanks

+2  A: 

You echo back the POST variable on your fields.

<form method="POST">
  <input type="text" name="name" value="<?php echo $_POST['name']?>" />
  <input type="submit" name="submit" />
</form>

When the form is submitted to self, the same data will be filled.

Sarfraz
Yes, but when entering the form the first time it will say undefined index and it wont find ['name'] anywhere... So this is good after it has been submitted once right?
Camran
@camran: there is solution for that too. just add @ before the $_POST var where it is echoed in the field. thanks
Sarfraz
Wonderful Sarfraz :)
Camran
@cameron: You welcome !!
Sarfraz
Don't forget **htmlspecialchars** otherwise you are inviting an XSS attack.
David Dorward
@David. It is up to developer. I would use it, i just answered the question to his point only and not beyond. By the way rather than htmlspecialchars, htmlentities should be used along with utf-8 encoding argument. thanks
Sarfraz
@Sarfraz: I'm curious, `htmlspecialchars()` also supports UTF-8 encoding. Is there any particular reason why you would use `htmlentities()` instead?
Alix Axel
@David: htmlentities() checks for most possible stuff while htmlspecialchars checks less stuff.
Sarfraz
@David: This is rather confusing (to me to some extent). You can ask a question on SO about this and we both can have this clarified. Thanks :)
Sarfraz
A: 

Well i do not know of anything else. I always use this:

<input type="text" value="<?= isset($value) ? $value : ""; ?>">

I think it is not too much code in the Templates, but it does the Trick.

Alternatively you could use some Frameworks wich abstract everything for you, but i cannot recommend some...

Paul Weber