Take the 2-minute tour ×

I am not sure if this has been asked before, here I go. I have a form <input name="name" value=<?php echo $myName; ?>

The problem i am having is:

  1. I am checking in the db if user exists, and if so here is what i do:


so this name gets outputed here: <input name="name" value=<?php echo $myName; ?>,

however, when i submit my form, i also validate it, so if:

original name in db was: John,

new name after form submit is: John Adam.

But since I am outputing $myName, to the form, it is overriding it? How do i use same variable name to echo to the input? in this case $myName, this below didn't do it!

<input name="name" value="<?php echo $myName; ?>">
share|improve this question
You should be doing the validation BEFORE the DB lookup. –  Barmar Mar 12 '13 at 15:54
ok how about retaining the form values even after validating to submit it to the db? –  Menew Mar 12 '13 at 15:56
Just use $_POST when you want to access the form input values. –  Barmar Mar 12 '13 at 15:56
i am actually doing so, its the first thing on the top of my code, then i check to see if user exists, then i load the form. then afer submit, its validates to see if all the fields were completed, however, if user exists, the values are been override from the original db values. –  Menew Mar 12 '13 at 15:58
add comment

1 Answer

up vote 0 down vote accepted

Can you just switch the order of your if statement?

$myName = '';
if (isset($_POST['name']) {
    $myName = $_POST['name'];
} else {
    $myName = $db->fields['name'];

It's hard to answer better without seeing the overall structure of your code, to see where the logic is going wrong.

share|improve this answer
hehe, i just did just that, and was going to comment on it:-) thanks Barmar! –  Menew Mar 12 '13 at 16:08
add comment

Your Answer


