PHP Form Validation with Redirect

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
techieg
Forum Newbie
Posts: 5
Joined: Sat Dec 19, 2009 12:26 am

PHP Form Validation with Redirect

Post by techieg »

Hello All,

As an IT engineer for a while now, I am now diversifying my skills and delving into the web developer world. I have so far implemented the following PHP form validation script (as sendmail.php) on my website but have a few expectations as well as issues. I have so far figured out everything myself except for one issue. FYI, my html form does a POST to this sendmail.php file:

Code: Select all

<?php
  $firstname = $_REQUEST['firstname'] ;
  $lastname = $_REQUEST['lastname'] ;
  $email = $_REQUEST['email'] ;
  $phone = $_REQUEST['phone'] ;
  $department = $_REQUEST['department'] ;
  $howdidyouhear = $_REQUEST['howdidyouhear'] ;
  $details = $_REQUEST['details'] ;
 
  if (!isset($_REQUEST['email'])) {
    header( "Location: http://www.mysite.com/error.html" );
  }
  elseif (empty($firstname) || empty($lastname) || empty($email) || empty($phone) || empty($department) || empty($howdidyouhear) || empty($details)) {
    header( "Location: http://www.mysite.com/error.html" );
  }
  else {
    //mail( "support@mysite.com", "Online Communications", $message, "From: $online@mysite.com" );
          
    mail( "info@techworxs.com", "Online Communications", "First Name: $firstname \n, Last Name: $lastname \n, Phone: $phone \n, Department: $department \n, How Did You Hear About Us: $howdidyouhear \n, Details: $details \n", "From: $email" );
    header( "Location: http://www.mysite.com/thanx.html" );
  }
 
?>
Expectations:

1. Each of the fields listed has been made required which means users have to enter a value before they are sent to the thank you page otherwise they get an error page.
2. The value of every entered field should be included in the email delivered to my email address.

Current Issues:

1. Everything works as expected including email deliveries, but I need to remove the comma after each user entry in the email delivered.

How do I remove the comma after each user entry delivered to my email address? Any help will be appreciated.

Thanx.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: PHP Form Validation with Redirect

Post by daedalus__ »

so you wrote this code by yourself, huh?

try the backspace key.
techieg
Forum Newbie
Posts: 5
Joined: Sat Dec 19, 2009 12:26 am

Re: PHP Form Validation with Redirect

Post by techieg »

Thanx for your response. However, I need you to please be more detailed with your response if you really want to help.
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: PHP Form Validation with Redirect

Post by Griven »

daedalus__ wrote:so you wrote this code by yourself, huh?

try the backspace key.
You could be a bit more constructive, Daedalus...


Sorry about that, Techieg.

I would recommend removing the commas that you have after all of the "\n"s in your mail() function. Let us know how that works for you.
techieg
Forum Newbie
Posts: 5
Joined: Sat Dec 19, 2009 12:26 am

Re: PHP Form Validation with Redirect

Post by techieg »

Thanx Griven, I'm not sure if I tried that already but let me give it a shot and update here.
techieg
Forum Newbie
Posts: 5
Joined: Sat Dec 19, 2009 12:26 am

Re: PHP Form Validation with Redirect

Post by techieg »

Thanx Griven, your suggestion works perfectly. I must have ignored the possibility of removing the commas since as a newbie to this I thought each field had to be separated by commas for the code to actually work, I guess I should have tried it as I did with other aspects that worked. Perhaps the enclosure in quotes suffices to remove the commas after each field. Is there something I missed or need to know when it comes to this for future use?
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: PHP Form Validation with Redirect

Post by Griven »

Code: Select all

mail( "info@techworxs.com", "Online Communications", "First Name: $firstname \n, Last Name: $lastname \n, Phone: $phone \n, Department: $department \n, How Did You Hear About Us: $howdidyouhear \n, Details: $details \n", "From: $email" );
mail() is a function. Parameters are passed to the function inside the parentheses. In the case of the mail() function, the type of parameters that are being passed are string values.

String values are letters, numbers, and symbols that are enclosed in quotes. Most everything inside the quotes is interpreted as text by the browser, with the exception of escaped characters, such as \n (the newline character). Since you put commas into the string, they were interpreted as part of the input, and rendered as such in the email message.
techieg
Forum Newbie
Posts: 5
Joined: Sat Dec 19, 2009 12:26 am

Re: PHP Form Validation with Redirect

Post by techieg »

Now, that's talking more like a teacher to a good student. I appreciate the explanation it sure helps helps me understand it better.
Post Reply