Jump to content

PHP Form


JaymeNYC

Recommended Posts

I'm still pretty new to PHP, and been gettin some help on my PHP form but I can't get past this one part, so don't know what to do! I'm trying to create a valid form that has 2 fields that are required, "Name, Email" and 1 field that is not and thats "Comments". Am I doin this the right way?

 

<div id="contactform"><?php
	  


if (isset($_SESSION['error'])) {

   echo $_SESSION['error'];

   unset($_SESSION['error']); // this will reset the $_SESSION['error'] container

} ?>
  <form method="post" action="thanks.php">
        <div class="grp"><label for="name">Your Name</label><br />
<input class="" id="name" type="text" name="name" onfocus="this.style.backgroundColor='#f7ffe3'
" onblur="this.style.backgroundColor='#fff'" /></div>
<div class="grp"><label for="email">Your Email</label><br />
<input class="" id="email" type="text" name="email" onfocus="this.style.backgroundColor='#f7ffe3'
" onblur="this.style.backgroundColor='#fff'" /></div>
    <div class="grp"><label for="txt">Your Message</label><br />
<textarea id="txt" name="txt" rows="" cols="" onfocus="this.style.backgroundColor='#f7ffe3'
" onblur="this.style.backgroundColor='#fff'" ></textarea></div>
<div class="grp2"><input class="formsubmit" type="submit" value="Submit" name="submit" /></div>
</form>
</div>

 

Above is the contact form from the contact.php form page. It said I was missing somethin in the php file so my whole contact page wouldnt load and i saw that the unset didnt have a ")" on the end so it looked like this before I added

unset($_SESSION['error'];

 

 

thanks.php is below

<?php
session_start();



// this works well with redux  -  ie. form action = $_SERVER['PHP_SELF']



if(isset($_POST['submit'])) { 

   // set a function that will clean your data

   function clean($value) {

      $var = strip_tags($value);

      $var = trim($value);



      return $var;

   }



   // still clean all data that is on the form

   $name = clean($_POST['name']);

   $email = clean($_POST['email']);

   $txt = clean($_POST['txt']);



   // check to see if field has been filled in at all

   // this is the spot to place the *manditory fields*

   // to validate that data has been entered. 

   // If you don't require it as a manditory field then get rid of it.

   $_SESSION['error'] = NULL;

   if (empty($name)) {

      $_SESSION['error'] .= "You forgot to enter your name!<br />";

      $name = FALSE;

   }

   if (empty($email)) {

      $_SESSION['error'] .= "You forgot to enter your email!<br />";

      $e = FALSE;

   }

   

   // if all is true then send the email   

   if ($name && $email) {

      $to = "email@gmail.com";

      $subject = "Company Form Filled";

      $body = "

         Name: $name\n\n

         Email: $email\n\n

         Body: $txt";



      $sent = mail($to, $subject, $body);



      if ($sent) {

         header ("Location: http://www.website.com/thankyou.php");

      } // end if $sent

   } // end if all var are true

} // end if post submit





// then within your form page where you want

// the error message to display, have the following code:



?>

 

When I hit the submit button and I leave a field blank, it goes to a blank page. Is how I did this right so far? Whats the best way to report errors? I would like to use this on my other sites but don't know if I'm coding this the right way to use it over and over again.

Thanks

Link to comment
Share on other sites

lol the answer was easy(took me 20 minutes to figure out)...

 

basically... it goes blank because you say "if everything is great, then email... " you have no else statement in the case that email or name was blank.

 

p.s. you shoud also make sure the textarea wasn't left blank

Link to comment
Share on other sites

it seems ok and you would expect to go to a blank page.

 

You are saying the following:

 

1. if everything is ok send an email - there is no html below this for the browser to show i.e. thanks for contacting us etc.

 

You are not asking it to do anything if one of the fields has been left blank.

 

if ($name && $email) {

      $to = "email@gmail.com";

      $subject = "Company Form Filled";

      $body = "

         Name: $name\n\n

         Email: $email\n\n

         Body: $txt";



      $sent = mail($to, $subject, $body);



      if ($sent) {

         header ("Location: http://www.website.com/thankyou.php");

      } // end if $sent

   } // end if all var are true

---- there should be an else here to catch the fact that the user may have missed a filed out. ----

} // end if post submit

 

also be careful with your variables

 

if (empty($name)) {

      $_SESSION['error'] .= "You forgot to enter your name!<br />";

      $name = FALSE; ---- maybe you should call this variable something different so you dont get confused between the two see below for email

   }

   if (empty($email)) {

      $_SESSION['error'] .= "You forgot to enter your email!<br />";

      $e = FALSE; --- here you have called the variable $e however below you check to see if $email is false

   }

 

hope this helps a bit

Link to comment
Share on other sites

it seems ok and you would expect to go to a blank page.

 

You are saying the following:

 

1. if everything is ok send an email - there is no html below this for the browser to show i.e. thanks for contacting us etc.

 

You are not asking it to do anything if one of the fields has been left blank.

 

if ($name && $email) {

     $to = "email@gmail.com";

     $subject = "Company Form Filled";

     $body = "

        Name: $name\n\n

        Email: $email\n\n

        Body: $txt";



     $sent = mail($to, $subject, $body);



     if ($sent) {

        header ("Location: http://www.website.com/thankyou.php");

     } // end if $sent

  } // end if all var are true

---- there should be an else here to catch the fact that the user may have missed a filed out. ----

} // end if post submit

 

also be careful with your variables

 

if (empty($name)) {

     $_SESSION['error'] .= "You forgot to enter your name!<br />";

     $name = FALSE; ---- maybe you should call this variable something different so you dont get confused between the two see below for email

  }

  if (empty($email)) {

     $_SESSION['error'] .= "You forgot to enter your email!<br />";

     $e = FALSE; --- here you have called the variable $e however below you check to see if $email is false

  }

 

hope this helps a bit

 

A little confused, mostly with the changing of $email to $e. What does that do? I'm very new to php so sorry if I don't pickup on the easy stuff!

Link to comment
Share on other sites

Where do I put it though? I'm REALLY new to php I was gettin help with this but haven't been able to lately so I'm lookin for help here. I did come across this page http://mobygreen.com/contact/#usermessageb which has the fields light up if they were not filled out correctly. Is this a whole different type of form?

 

Also, where can I find some basic->intermediate php tutorials so I can learn faster? I feel handicapped.

 

 

Link to comment
Share on other sites

<?php
session_start();



// this works well with redux  -  ie. form action = $_SERVER['PHP_SELF']



if(isset($_POST['submit'])) { 

   // set a function that will clean your data

   function clean($value) {

      $var = strip_tags($value);

      $var = trim($value);



      return $var;

   }



   // still clean all data that is on the form

   $name = clean($_POST['name']);

   $email = clean($_POST['email']);

   $txt = clean($_POST['txt']);



   // check to see if field has been filled in at all

   // this is the spot to place the *manditory fields*

   // to validate that data has been entered. 

   // If you don't require it as a manditory field then get rid of it.

   $_SESSION['error'] = NULL;

   if (empty($name)) {

      $_SESSION['error'] .= "You forgot to enter your name!<br />";

      $name = FALSE;

   }

   if (empty($email)) {

      $_SESSION['error'] .= "You forgot to enter your email!<br />";

      $e = FALSE;

   }

   

   // if all is true then send the email   

   if ($name && $email) {

      $to = "Contact@enwisedesign.com.com";

      $subject = "Company Form Filled";

      $body = "

         Name: $name\n\n

         Email: $email\n\n

         Body: $txt";



      $sent = mail($to, $subject, $body);



      if ($sent) {

         header ("Location: http://www.site.com/36/thankyou.php");

      } // end if $sent

   } // end if all var are true

if (empty($name)) {

      $_SESSION['error'] .= "You forgot to enter your name!<br />";

      $name = FALSE;

   }

   if (empty($email)) {

      $_SESSION['error'] .= "You forgot to enter your email!<br />";

      $e = FALSE;
   }

} // end if post submit


else { 
ini_set('error_reporting',E_ALL); // error reporting
	}


?>


 

 

Ahh I'm tired. I have alot more I have to have done by the morning and would just rather have some1 do this lil part so is 15$ enough for 2 PHP cross-browser valid forms?

 

Required field errors reporting to a div ("Name field Required.")

email field must have @ (whatever email validation requires)

Completed form sends to email

Field color change if not completed properly

Random answerable question (Captcha?)

Thank you message appearing after form is validated/sent

 

 

Anyone Interested? Just tired of lookin at code

Paypal of course

:D :D :D :D :D

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.