Page 1 of 1

PHP Form Validation with Redirect

Posted: Sat Dec 19, 2009 1:53 pm
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.

Re: PHP Form Validation with Redirect

Posted: Sat Dec 19, 2009 6:26 pm
by daedalus__
so you wrote this code by yourself, huh?

try the backspace key.

Re: PHP Form Validation with Redirect

Posted: Sun Dec 20, 2009 1:26 am
by techieg
Thanx for your response. However, I need you to please be more detailed with your response if you really want to help.

Re: PHP Form Validation with Redirect

Posted: Sun Dec 20, 2009 3:38 am
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.

Re: PHP Form Validation with Redirect

Posted: Sun Dec 20, 2009 4:15 am
by techieg
Thanx Griven, I'm not sure if I tried that already but let me give it a shot and update here.

Re: PHP Form Validation with Redirect

Posted: Sun Dec 20, 2009 4:27 am
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?

Re: PHP Form Validation with Redirect

Posted: Sun Dec 20, 2009 4:46 pm
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.

Re: PHP Form Validation with Redirect

Posted: Mon Dec 21, 2009 12:44 pm
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.