help a newbie with code pleeeeaaassse!!!

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
rrcole2
Forum Newbie
Posts: 3
Joined: Tue Jan 27, 2004 9:04 pm

help a newbie with code pleeeeaaassse!!!

Post by rrcole2 »

I made this form for users to register for a user name and password. It worked great until I tried to add validation code. As you can tell I am new at this so be patient, please. The form is filled out by the user and then it adds them to the db, mails me and then redirects the user to a thankyou page. Like I said it was working fine. I get an error on the last php script. I have tried moving the code around and still nothing...I m at the end of my rope. Any help you can give I would appreciate. Thanks in advance and here is a copy of my code.

<?php
if ($submit) {
if (!$first||!$last||!$bname||!$baddress||!$telephone||!$email) {
$error = "Sorry! You didn't fill in all the fields!";
}else{
// process form
echo "Thank You!";
}
if (!$submit || $error) {
echo $error;

$db = mysql_connect("localhost", "username","password");
mysql_select_db("mydb",$db);
$sql = "INSERT INTO Customers (first,last,bname,baddress,saddress,telephone,email,acomments,ila,time) VALUES
('$first','$last','$bname','$baddress','$saddress','$telephone','$email','$acomments',curdate(),curtime())";

$result = mysql_query($sql);
mail("rrcole2@shaw.ca","user name and password request",$sql,"From: phpFormGenerator");
header("Refresh: 0;url=http://www.shannonslittlesoapco.com/thnx.htm");

} else{

// display form
?>
<p>
<form method="post" action="<?php echo $PHP_SELF?>">
<table width="75%" border="0" align="center">
<tr>
<td>First name:</td>
<td><input type="Text" name="first" value="<?php echo $first?>"></td>
</tr>
<tr>
<td>Last name:</td>
<td><input type="Text" name="last" value="<?php echo $last ?>"></td>
</tr>
<tr>
<td>business name:</td>
<td><input name="bname" type="text" id="bname2" maxlength="20" value="<?php echo $bname ?>"></td>
</tr>
<tr>
<td>Billing Address:</td>
<td><textarea name="baddress" cols="30" rows="3" id="baddress" accesskey="<?php echo $baddress ?>"></textarea></td>
</tr>
<tr>
<td> Shipping Address:</td>
<td><textarea name="saddress" cols="30" rows="3" id="saddress">same as billing address</textarea></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input name="email" type="text" id="email2" size="20" value="<?php echo $email ?>"></td>
</tr>
<tr>
<td>Telephone Number:</td>
<td><input name="telephone" type="text" id="telephone2" value="<?php echo $telephone ?>"></td>
</tr>
<tr>
<td>Additional Comments:</td>
<td><textarea name="acomments" cols="30" rows="3" id="textarea3"></textarea></td>
</tr>
</table>
<div align="center"><br>
<input type="Submit" name="submit" value="Enter information">
</div>
</form>

<?php
} // end if
?>
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Re: help a newbie with code pleeeeaaassse!!!

Post by m3rajk »

1: please read about titleing questions: http://www.catb.org/~esr/faqs/hacker-howto.html

there's more good info there for you.

2: please use the php tags in the future. not having it here isn't an issue since you're a newb, but as you can see, it's much easier to read with the php tags.

rrcole2 wrote:I made this form for users to register for a user name and password. It worked great until I tried to add validation code. As you can tell I am new at this so be patient, please. The form is filled out by the user and then it adds them to the db, mails me and then redirects the user to a thankyou page. Like I said it was working fine. I get an error on the last php script. I have tried moving the code around and still nothing...I m at the end of my rope. Any help you can give I would appreciate. Thanks in advance and here is a copy of my code.

Code: Select all

<?php
if ($submit) {
if (!$first||!$last||!$bname||!$baddress||!$telephone||!$email) {
$error = "Sorry! You didn't fill in all the fields!";
}else{
// process form
echo "Thank You!";
}
if (!$submit || $error) {
echo $error;

$db = mysql_connect("localhost", "username","password");
mysql_select_db("mydb",$db);
$sql = "INSERT INTO Customers (first,last,bname,baddress,saddress,telephone,email,acomments,ila,time) VALUES
('$first','$last','$bname','$baddress','$saddress','$telephone','$email','$acomments',curdate(),curtime())";

$result = mysql_query($sql);
mail("rrcole2@shaw.ca","user name and password request",$sql,"From: phpFormGenerator");
header("Refresh: 0;url=http://www.shannonslittlesoapco.com/thnx.htm");

} else{

// display form
?>
<p>
<form method="post" action="<?php echo $PHP_SELF?>">
  <table width="75%" border="0" align="center">
    <tr>
      <td>First name:</td>
      <td><input type="Text" name="first" value="<?php echo $first?>"></td>
    </tr>
    <tr>
      <td>Last name:</td>
      <td><input type="Text" name="last" value="<?php echo $last ?>"></td>
    </tr>
    <tr>
      <td>business name:</td>
      <td><input name="bname" type="text" id="bname2" maxlength="20" value="<?php echo $bname ?>"></td>
    </tr>
    <tr>
      <td>Billing Address:</td>
      <td><textarea name="baddress" cols="30" rows="3" id="baddress" accesskey="<?php echo $baddress ?>"></textarea></td>
    </tr>
    <tr>
      <td> Shipping Address:</td>
      <td><textarea name="saddress" cols="30" rows="3" id="saddress">same as billing address</textarea></td>
    </tr>
    <tr>
      <td>E-mail:</td>
      <td><input name="email" type="text" id="email2" size="20" value="<?php echo $email ?>"></td>
    </tr>
    <tr>
      <td>Telephone Number:</td>
      <td><input name="telephone" type="text" id="telephone2" value="<?php echo $telephone ?>"></td>
    </tr>
    <tr>
      <td>Additional Comments:</td>
      <td><textarea name="acomments" cols="30" rows="3" id="textarea3"></textarea></td>
    </tr>
  </table>
  <div align="center"><br>
    <input type="Submit" name="submit" value="Enter information">
  </div>
</form>

<?php
} // end if
?>
now i'll go and when i see it easier to read i'll look at the issue
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Re: help a newbie with code pleeeeaaassse!!!

Post by m3rajk »

first: this sends everythig to you., second, if you require email is validated, i'd have them give an email and aggree to any terms and conditions, possibly make a username, tnhen come back from a link sent to validate the email to create hte profile. and make a validation code that's uniique to each user


secondly i'd use a heredoc (hwww.php.net, look up echo, then look at heredoc format) for the main printout body.

last,: i'm suprised it works, you don't make sure that the variables that are passed arent empty strings, just that they are set.


my suggestion is to figure out how you wanna do validation? they start to sign up, they get an email, then have the user enter the code, or the way i suggested earlier.

once you've decided that we can help you further. until then i feel any help may become mooted by your future decision on how to handle validation,
rrcole2
Forum Newbie
Posts: 3
Joined: Tue Jan 27, 2004 9:04 pm

Post by rrcole2 »

Thanks for the suggestions, I will have to give that some serious thought.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

rrcole2 wrote:Thanks for the suggestions, I will have to give that some serious thought.
no problem. i originally did the validation for a site i made by having multiple steps to the sign up and sending the code with the first one, but that got the person into a number of database tables, making clean up on unvalidated ones messy.

so i switched to the other way. now it's one table, but i had to rework the sign up into two pages. so i knwo this one from experience. i also know that if i did the db differntly there'd only be one table. n the first method for me..

once you decide how the differnt ways to validate will effect you, and which to go with, you can build the sign up more efficiently

also remember that when using post, if you have a feild called "blah" then even if it's left blank, there's still something in $_POST['blah']so if you set a variable to that you will have soemthing. so you need ot make sure the variable isn't an empty string (what post sets it to when empty)
Post Reply