How can I keep data in text fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dapa
Forum Newbie
Posts: 9
Joined: Fri Nov 10, 2006 3:10 pm

How can I keep data in text fields

Post by dapa »

I have a register form on my site which asks for first name, last name, email, username and password to be entered.

If the username already exists in my database I have coded a prompt to alert the user that the username is not available as it is already assigned to someone else.

However all previous data that has been entered in the firstname, lastname and email textfields is lost and therefore the user would need to re-enter this lost data.

How do I keep the firstname, lastname & email data in the textfields so that the user only has to try inputting a different username?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Set the value attribute of each input field to it's respective $_POST value after running each value through htmlspecialchars().
dapa
Forum Newbie
Posts: 9
Joined: Fri Nov 10, 2006 3:10 pm

Post by dapa »

The Ninja Space Goat | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too. [/color]




I have tried to use htmlspecial chars()

However it does not seem to work. Please see the code I have used below. Please can you tell me where I am going wrong. I'm a Total novice at php so please forgive me. Is something to with how I am trying to validate the form?

[syntax="php"]
<body><?
  if (!isset($_POST['submit']))
  {
    // Form was submitted. 

    $pFirstName = (isset($_POST['firstname'])) ? trim($_POST['firstname']) : '';
    $pLastName  = (isset($_POST['lastname']))  ? trim($_POST['lastname'])  : '';
    
    // Other validating can go here
  }
  else
  {
    // Form wasn't submitted, set default values

    $pFirstName = '';
    $pLastName  = '';
  }
?>

<form action="<?php echo $editFormAction; ?>" method="POST" name="form1" id="form1" onSubmit="YY_checkform('form1','firstname','#q','0','Field \'firstname\' is not valid.','lastname','#q','0','Field \'lastname\' is not valid.','email','S','2','Field \'email\' is not valid.','username','#q','0','Field \'username\' is not valid.','password','#q','0','Field \'password\' is not valid.','con_password','password','6','Field \'con_password\' is not valid.');return document.MM_returnValue">
  <table width="400" border="0" cellspacing="5" cellpadding="0">
    <tr>
      <td width="149">first name</td>
      <td width="236"><input name="firstname" type="text" id="firstname" value="<?php htmlspecialchars($pFirstName) ?>" /></td>
    </tr>
    <tr>
      <td>last name</td>
      <td><input name="lastname" type="text" id="lastname" value="<?php htmlspecialchars($pLasttName) ?>" /></td>
    </tr>
    <tr>
      <td>e-mail</td>
      <td><input name="email" type="text" id="email" /></td>
    </tr>
    <tr>
      <td>username</td>
      <td><input name="username" type="text" id="username" /></td>
    </tr>
    <tr>
      <td>password</td>
      <td><input name="password" type="password" id="password" /></td>
    </tr>
    <tr>
      <td>confirm password</td>
      <td><input name="con_password" type="password" id="con_password" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
    <input type="hidden" name="MM_insert" value="form1">
</form>
</body>
</html>


The Ninja Space Goat | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too. [/color]
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

htmlspecialchars() returns a string, but doesn't put that string on the output buffer. You need to echo each returned string.
dapa
Forum Newbie
Posts: 9
Joined: Fri Nov 10, 2006 3:10 pm

Post by dapa »

Can you show me where to inser the echo please?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Echo the return value of htmlspecialchars:

Code: Select all

<?php echo htmlspecialchars($pLasttName) ?>
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

// simple example
<form name="x" action="<? echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="text" name="test" value="<? echo ($_POST['test']?$_POST['test']:"");?>">
<input type="submit" name="submit" value="Test mE">
dont byte me if i made a typo!
Post Reply