Page 1 of 1

How can I keep data in text fields

Posted: Thu Mar 29, 2007 5:43 pm
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?

Posted: Thu Mar 29, 2007 5:45 pm
by aaronhall
Set the value attribute of each input field to it's respective $_POST value after running each value through htmlspecialchars().

Posted: Thu Mar 29, 2007 6:19 pm
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]

Posted: Thu Mar 29, 2007 6:22 pm
by aaronhall
htmlspecialchars() returns a string, but doesn't put that string on the output buffer. You need to echo each returned string.

Posted: Thu Mar 29, 2007 6:25 pm
by dapa
Can you show me where to inser the echo please?

Posted: Thu Mar 29, 2007 6:30 pm
by aaronhall
Echo the return value of htmlspecialchars:

Code: Select all

<?php echo htmlspecialchars($pLasttName) ?>

Posted: Thu Mar 29, 2007 9:39 pm
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!