Jump to content

Registration form help


zephyrstudios

Recommended Posts

Hello,
I am trying to create a quick little registration page-trying to learn php. I have the form created and get the response-but now I am trying to get the registrars name back in the return statement and am unable to figure out the code to do that. Any help is appreciated.

Thank you
Anne


<?php // register.php

//This script registers a user by storing their information in a text file and creating a directory for them.


if(isset ($_POST['submit'])){//Handle form

$problem = FALSE; //No problems so far

//Check for each value.
if (empty ($_POST['fname'])) {
$problem = TRUE;
print '<p>Please enter a first name!</p>';
}

if (empty ($_POST['lname'])) {
$problem = TRUE;
print '<p>Please enter a last name!</p>';
}



if (!$problem) {

if ($fp = fopen ('./users.txt', 'ab')) {//Open the file

//Create the data to be written
$dir = time() . rand (0, 4596);
$data = $_POST['fname'] . "\t" . $data = $_POST['lname'] . "\t" . $data = $_POST['address'] . "\t" . $dir ."\r\n";

//Write the data and close the file
fwrite ($fp, $data);
fclose ($fp);

//create the directory
mkdir ("./users/$dir");

//Print a message

print '<p>Congratulations  $fname $lname!  /></p>';

} else {//Couldn't write to the file
print '<p>You could not be registered due to a system error.</p>';
}
} else {//Forgot a field
print '<p> Please try again!</p>';

}
} else {//Display again

//Leave PHP and display the form
?>

<form action="register.php" method="post">
<br />
<table width="100%"  border="0">
  <tr>
    <th width="11%" scope="col"><div align="left"><strong>First Name: </strong></div></th>
    <th width="81%" scope="col"><div align="left">
      <input type="text" name = "fname" size="25" />
      </div></th>
    <th width="4%" scope="col">&nbsp;</th>
    <th width="4%" scope="col">&nbsp;</th>
  </tr>
  <tr>
    <td><div align="left"><strong>Last Name: </strong></div></td>
    <td><div align="left">
      <input type="text" name="lname" size="25" />
    </div></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><div align="left"><strong>Address: </strong></div></td>
    <td><div align="left">
      <input type="text" name="address" size="35"/>
    </div></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><strong>City:</strong></td>
    <td><input type="text" name="city" size="35"/></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
 
</table>
<br />
<br />
<input type="submit" name="submit" value="Register" />
</form>
<?php
Link to comment
Share on other sites

Here you go: [code]<?php // register.php

//This script registers a user by storing their information in a text file and creating a directory for them.


if(isset ($_POST['submit'])) //Handle form
{
$problem = FALSE; //No problems so far

//Check for each value.
if(empty($_POST['fname']))
{
$problem = TRUE;
print '<p>Please enter a first name!</p>';
}

if(empty($_POST['lname']))
{
$problem = TRUE;
print '<p>Please enter a last name!</p>';
}

if(!$problem)
{
if($fp = fopen('./users.txt', 'ab')) //Open the file
{
//Create the data to be written
$dir = time().rand(0, 4596);
$data = $_POST['fname']."\t".$data = $_POST['lname']."\t".$data = $_POST['address']."\t".$dir."\r\n";

//Write the data and close the file
fwrite($fp, $data);
fclose($fp);

//create the directory
mkdir("./users/$dir");

//Print a message
print "<p>Congratulations  {$fname} {$lname}!  /></p>";
}
else { //Couldn't write to the file
print '<p>You could not be registered due to a system error.</p>';
}
}
else { //Forgot a field
print '<p> Please try again!</p>';
}
}
else { //Display again

//Leave PHP and display the form
?>

<form action="register.php" method="post">


<table width="100%"  border="0">
  <tr>
    <th width="11%" scope="col"><div align="left"><strong>First Name: </strong></div></th>
    <th width="81%" scope="col"><div align="left">
      <input type="text" name = "fname" size="25" value='<?=$_POST['fname']?>' />
      </div></th>
    <th width="4%" scope="col">&nbsp;</th>
    <th width="4%" scope="col">&nbsp;</th>
  </tr>
  <tr>
    <td><div align="left"><strong>Last Name: </strong></div></td>
    <td><div align="left">
      <input type="text" name="lname" size="25" value='<?=$_POST['lname']?>' />
    </div></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><div align="left"><strong>Address: </strong></div></td>
    <td><div align="left">
      <input type="text" name="address" size="35" value='<?=$_POST['address']?>'/>
    </div></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><strong>City:</strong></td>
    <td><input type="text" name="city" size="35" value='<?=$_POST['city']?>'/></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>

</table>


<input type="submit" name="submit" value="Register" />
</form>

<?php
}
?>[/code]
Link to comment
Share on other sites

the problem is that unless register_globals is on (which it shouldn't and needn't be), $fname and $lname will not be registered as local variables.  try doing one of the following:

1)  setting $fname = $_POST['fname'] and likewise for last name, or
2)  using {$_POST['fname']} in place of $fname.

i'd suggest the latter, as it is better practice in my opinion (rather than crowding the namespace with useless variables).  note that the braces are needed when using array values, since otherwise the single quotes are taken as literals (as far as i remember).

hope this helps.
Link to comment
Share on other sites

That helped alot!!! :)    This is what worked:

'.($_POST['fname']).'

But now I am trying to add their last name.  I have tried

'.($_POST['fname']['lname']).' 

'.($_POST['fname']).' '.($_POST['lname'].'

I keep getting errors. i am looking in alot of books trying to solve this and I am sure it is very simple-just not simple to a newbie.  Thanks again in advance for your time and patience.

Anne
Link to comment
Share on other sites

perhaps i can go ahead and fix the offending line for you:

[code]print '<p>Congratulations '.$_POST['$fname'].' '.$_POST['lname'].'!  /></p>';[/code]

when exiting single quotes, you may include variables as you normally would, without parentheses.

let me know how this works out.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.