473,396 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to adding variable to conditional?

Patty Kowaleski
I have this:

// If the form fields are empty, redirect to the error page.
elseif (empty($email_address) || empty($comments)) {
header( "Location: error_message.html" );
}

I want to add another field to the string. How to do this? The additional field is "name".

Sorry. I'm a novice when it comes to PHP. I have an email form using a file "send_email.php". My contact form simply contains:

Name
Email Address
Comments

If the fields are left empty on the contact form, the form returns an error page asking to answer all questions and resubmit. The php sample file I copied listed only 2 fields if "empty":

email address
comments

I want to add "name" to the string. Not knowing php code, I tried logic...which didn't work for me. Here's the contents of the php file:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3. This first bit sets the email address that you want the form to be submitted to.
  4. You will need to change this value to a valid email address that you can access.
  5. */
  6. $webmaster_email = "emailname@example.com";
  7.  
  8. /*
  9. This bit sets the URLs of the supporting pages.
  10. If you change the names of any of the pages, you will need to change the values here.
  11. */
  12. $contact_page = "contact.htm";
  13. $error_page = "error_message.html";
  14. $thankyou_page = "thank_you.html";
  15.  
  16. /*
  17. This next bit loads the form field data into variables.
  18. If you add a form field, you will need to add it here.
  19. */
  20. $name = $_REQUEST['name'] ;
  21. $email_address = $_REQUEST['email_address'] ;
  22. $comments = $_REQUEST['comments'] ;
  23.  
  24. /*
  25. The following function checks for email injection.
  26. Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
  27. */
  28. function isInjected($str) {
  29.     $injections = array('(\n+)',
  30.     '(\r+)',
  31.     '(\t+)',
  32.     '(%0A+)',
  33.     '(%0D+)',
  34.     '(%08+)',
  35.     '(%09+)'
  36.     );
  37.     $inject = join('|', $injections);
  38.     $inject = "/$inject/i";
  39.     if(preg_match($inject,$str)) {
  40.         return true;
  41.     }
  42.     else {
  43.         return false;
  44.     }
  45. }
  46.  
  47. // If the user tries to access this script directly, redirect them to the feedback form,
  48. if (!isset($_REQUEST['email_address'])) {
  49. header( "Location: $contact_page" );
  50. }
  51.  
  52. // If the form fields are empty, redirect to the error page.
  53. elseif (empty($email_address) || empty($comments)) {
  54. header( "Location: error_message.html" );
  55. }
  56.  
  57. // If email injection is detected, redirect to the error page.
  58. elseif ( isInjected($email_address) ) {
  59. header( "Location: $error_page" );
  60. }
  61.  
  62. // If we passed all previous tests, send the email then redirect to the thank you page.
  63. else {
  64. mail( "$webmaster_email", "Contact Form Submission",
  65.   $comments, "From: $email_address" );
  66. header( "Location: $thankyou_page" );
  67. }
  68. ?>
Jan 29 '11 #1
6 1848
Markus
6,050 Expert 4TB
Add an additional field to what string? Having trouble understanding the issue.

Also, your thread title and thread content seem to differ.
Jan 29 '11 #2
Markus
6,050 Expert 4TB
I see. You can just extend the ifelse to contain the $name variable:
Expand|Select|Wrap|Line Numbers
  1. if ( ... || empty( $name ) )
  2. {
  3.  // Whatever
  4. }
  5.  
Jan 29 '11 #3
To line 53, this is how I added the $name variable:

elseif (empty($email_address) || empty($comments) || empty(&name)) {

It returns this error message:

"Parse error: syntax error, unexpected '&', expecting T_STRING or T_VARIABLE or '$' in /home/drmosko/public_html/send_mail.php on line 53"

Am I still missing something?
Jan 29 '11 #4
Markus
6,050 Expert 4TB
The error message gives you all the information you need. Look at the name variable you added, and see if you can spot how it is different to the email_address and comments variables (hint: one character is different).
Jan 29 '11 #5
OMG. The characters looked soooo similar in code. Thanks for all the help. Have a wonderful weekend!!
Jan 30 '11 #6
Markus
6,050 Expert 4TB
You, too, Patty. Be sure to come back if you have any more questions.

Mark.
Jan 30 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Christopher Richards | last post by:
I have a variable that stores a temporary password. I'm just learning about arrays. I know this is wrong $pwList= ($newPW); I want to add the value of the $newPW which keeps changing to a unique...
8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
62
by: Reinhold Birkenfeld | last post by:
Hi, after Guido's pronouncement yesterday, in one of the next versions of Python there will be a conditional expression with the following syntax: X if C else Y which is the same as today's...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
2
by: Steve Jorgensen | last post by:
To begin with an example... Let's say you were wanting to write code usign early binding to the MSXML library, but then be able to switch between early and late binding at will. Conditional...
6
by: Chris Dunaway | last post by:
Consider this code (.Net 2.0) which uses a nullable type: private void button1_Click(object sender, System.EventArgs e) { DateTime? nullableDate; nullableDate = (condition) ? null :...
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
6
by: maxwell | last post by:
I'm trying to use the gpp utility (Gnu points to http://en.nothingisreal.com/wiki/GPP) to do conditional compilation in Python, and I'm running into a problem: the same '#' character introduces...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.