Code stopped working

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
ggrant3
Forum Newbie
Posts: 10
Joined: Wed Jan 16, 2008 10:37 am

Code stopped working

Post by ggrant3 »

I made this basic order form and confirmation page adn tested it the other day. When I tested the form, I got the results I wanted (it emailed me the infomation and re-directed me to a confirmation page which displayed the information I selected back to me.

I was getting ready to work on another section of code (adding a receipt number) and I wanted to test the previous script results again, but this time they didn't work.

Now the problem I get is on the confirmation page the information that was entered on the form isn't displayed on the confirmation page. I just get the standard message without the persons name, email, topic, or comments. I get the email with the forms information, so I am sure the issue is with my confirmation page code itself, but I don't know what I did. I might have changed something on accident, but I have stared and stared at it and I can't see what is missing.

I am going to post both the form page and the confirmation page just in case both are needed.

This is the form page

Code: Select all

<?php
include('includes/corefuncs.php');
if (function_exists('nukeMagicQuotes')) {
    nukeMagicQuotes();
    }
    
// Process the email
if (array_key_exists('send', $_POST)) {
    $to = 'my email address goes here'; 
    $subject = 'Contact Us Form Feedback';
    
// List expected fields
    $expected = array('name', 'email', 'topic', 'comments');
    
// Set required fields
    $required = array('name', 'email', 'topic', 'comments');
    
// Create empty array for any missing fields
    $missing = array ();
    
// Assume that there is nothing suspect
    $suspect = false;
// Create a pattern to locate suspect phrases
    $pattern = '/Content-Type:|Bcc:|Cc:/i';
    
// Function to check for suspect phrases
    function isSuspect($val, $pattern, &$suspect) {
    
    // If the variable is an array, loop through each element
    // And pass it recursively back to the same function
    if (is_array($val)) {
        foreach ($val as $item) {
            isSuspect($item, $pattern, $suspect);
            }
        }
        else {
    // If one of the suspect phrases is found, set Boolean to True
        if (preg_match($pattern, $val)) {
        $suspect = true;
        }
        }
        }
    // Check the $_POST array and any subarrays for suspect content
        isSuspect($_POST, $pattern, $suspect);
    
    if ($suspect) {
        $mailSent = false;
        unset($missing);
        }
    else {      
    
// Process the $_POST variables
    foreach ($_POST as $key => $value) {
    
// Assign to temporary variable and strip whitespace in not an array
    $temp = is_array($value) ? $value : trim($value);
    
// if empty and required, add to $missing array
    if (empty($temp) && in_array($key, $required)) {
        array_push($missing, $key);
        }
// Otherwise, assign to a variable of the same name as $key
    elseif (in_array($key, $expected)) {
    ${$key} = $temp;
    }
   }
  } 
  
// Validate the email address
    if (!empty($email)) {
     // Regex to ensure no illegal characters in email address
     $checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
     // Regect the email address if it doesn't match
     if (!preg_match($checkEmail, $email)) {
        array_push($missing, 'email');
        }
    }
  
// Go ahead only if not suspect and all requried fields OK
    if (!$suspect && empty($missing)) { 
 
 
// Build the message
$message = "Name: $name\n\n";
$message .= "Email: $email\n\n";
$message .= "Topic: $topic\n\n";
$message .= "Comments: $comments";
 
// Limit line length to 70 characters
$message = wordwrap($message, 70);
 
// Create additional headers
$additionalHeaders = "From: Contact Us<my email address>\r\n";
    
// Send it
$mailSent = mail($to, $subject, $message, $additionalHeaders);
if ($mailSent) {
// redirect the page with a fully qualified URL
    header('Location: TestContactConfirmation.php');
    exit;
}
}
}
    ?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Contact Us</title>
</head>
<body>
<h1>Contact Us</h1>
<?php
if ($_POST && isset($missing) && !empty($missing)) {
?>
    <p class="warning">Please complete the missing item(s) indicated.</p>
    <?php
    }
elseif ($_POST && !$mailSent) {
?>
    <p class="warning">Sorry, there was a problem sending your message.  Please try again.</p>
    <?php
    }
    elseif ($_POST && $mailSent) {
    ?>
    <p><strong>Your message has been sent.  Thank You for your feedback.</strong></p>
    <?php } ?>
    
<form action="" method="post">
<table width="650" border="0" align="center" cellpadding="10" cellspacing="5">
  <tr>
    <td height="31" colspan="2"><font size="2">(<font color="red">*</font> = a required field)</font></td>
    </tr>
  <tr>
    <td width="300" height="31" bgcolor="#F5F5F5"><div align="right"><font size="2" color="red">*</font><strong>Name:</strong>:
    </div></td>
    <td width="300" bgcolor="#F5F5F5">
    <input id="name" name="name" type="text" class="formbox" 
    <?php
    if (isset($missing)) {
    echo 'value="'.htmlentities($_POST['name']).'"';
    } ?> />
 <?php
    if (isset($missing) && in_array('name', $missing)) { ?>
    <span class="warning"><div style= "color:red">Please enter your name</div>
    <?php } ?></td>
  </tr>
  <tr>
    <td bgcolor="#CECECE"><div align="right"><font size="2" color="red">*</font><strong>Email:</strong></div></td>
    <td bgcolor="#CECECE">
    <input name="email" type="text" size="40" maxlength="60" <?php if (isset($missing)) {
    echo 'value="'.htmlentities($_POST['email']).'"';
    } ?>/>
    
    <?php
    if (isset($missing) && in_array('email', $missing)) { ?>
    <span class="warning"><div style= "color:red">Please enter your email address</div>
    <?php } ?></td>
  </tr>
 
 
<tr>
    <td bgcolor="#F5F5F5"><div align="right"><font size="2" color="red">*</font><strong>Topic:</strong></div></td>
    <td bgcolor="#F5F5F5">
    <select name="topic" id="topic">
    <option value=""
    <?php 
    if (!$_POST || $_POST['topic'] == '') { ?>
    selected="selected"
    <?php } ?>
     >Please select a topic</option>
<option value="Order Status"
<?php if (isset($missing) && $_POST['topic'] == 'Order Status') { ?>
selected="selected"
<?php } ?>
>Order Status</option>
<option value="Shipping Questions"
<?php if (isset($missing) && $_POST['topic'] == 'Shipping Questions') { ?>
selected="selected"
<?php } ?>
>Shipping Questions</option>
<option value="Product Information"
<?php if (isset($missing) && $_POST['topic'] == 'Product Information') { ?>
selected="selected"
<?php } ?>
>Product Information</option>
<option value="Other"<?php if (isset($missing) && $_POST['topic'] == 'Other') { ?>
selected="selected"
<?php } ?>
>Other</option>
</select>
 
<?php
    if (isset($missing) && in_array('topic', $missing)) { ?>
    <span class="warning"><div style= "color:red">Please enter your topic</div><?php } ?>
</td>
  </tr>
  <tr>
    <td bgcolor="#CECECE"><div align="right"><font size="2" color="red">*</font><strong>Comments:</strong></div></td>
    <td bgcolor="#CECECE">
    <textarea name="comments" cols="60" rows="7"><?php
    if (isset($missing)) {
    echo htmlentities($_POST['comments']);
    } ?></textarea>
    
    <?php
    if (isset($missing) && in_array('comments', $missing)) { ?>
    <span class="warning"><div style= "color:red">Please enter your comments</div><?php } ?>    </td>
  </tr>
  
  
  <tr>
    <td colspan="2">
    <div align="center"> 
    <input name="send" id="send" type="submit" value="Submit My Comments" />
</div></td>
    </tr>
</table>
</form>
</body>
</html>

And this is the confirmation page

Code: Select all

<?php
include('includes/corefuncs.php');
    if (function_exists('nukeMagicQuotes')) {
    nukeMagicQuotes();
    }
    ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TestContactConfirmation</title>
</head>
 
<body>
<?php 
#TestContactConfirmation.php
 
// Create a shorthand for the form data.
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$topic = $_REQUEST['topic'];
$comments = $_REQUEST['comments']; 
 
// Print the submitted information.
echo "<p>Thank you, <strong>$name</strong>, for the following comments: <br />
<tt>$comments</tt></p>
<p>We will reply to you at <em>$email</em>.</p>\n"; 
?>  
</body>
</html>
Any ideas/advice is appreciated
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: Code stopped working

Post by jimthunderbird »

You might want to check your form's action, your form's action is empty.

Also, you might want to read the documents for $_POST and $_REQUEST, your form is sending post vars, so your confirmation page can not receive request vars.

Hope this gives you some hints.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Code stopped working

Post by RobertGonzalez »

$_REQUEST encapsulates all request variables, POST, GET, SESSION and COOKIE. That is not the problem.

Perhaps you can turn on error_reporting and display errors on the posted page to see what PHP is telling about it?

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
 
// add the rest of the code here
?>
ggrant3
Forum Newbie
Posts: 10
Joined: Wed Jan 16, 2008 10:37 am

Re: Code stopped working

Post by ggrant3 »

Okay when I added the "display error" code that you posted (below)

Code: Select all

<?php
include('includes/corefuncs.php');
    if (function_exists('nukeMagicQuotes')) {
    nukeMagicQuotes();
    }
    ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TestContactConfirmation</title>
</head>
 
<body>
<?php 
 
   
    ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 1);
     
    
#TestContactConfirmation.php
 
// Create a shorthand for the form data.
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$topic = $_REQUEST['topic'];
$comments = $_REQUEST['comments']; 
 
// Print the submitted information.
echo "<p>Thank you, <strong>$name</strong>, for the following comments: <br />
<tt>$comments</tt></p>
<p>We will reply to you at <em>$email</em>.</p>\n"; 
?>  
</body>
</html>


I get the following:
---------------------------------------
Notice: Undefined index: name in /home/content/g/g/r/ggrant3/html/TestContactConfirmation.php on line 26

Notice: Undefined index: email in /home/content/g/g/r/ggrant3/html/TestContactConfirmation.php on line 27

Notice: Undefined index: topic in /home/content/g/g/r/ggrant3/html/TestContactConfirmation.php on line 28

Notice: Undefined index: comments in /home/content/g/g/r/ggrant3/html/TestContactConfirmation.php on line 29

Thank you, , for the following comments:

We will reply to you at .
---------------------------------------------

Now line 26, 27, 28, & 29, refer to all of my $_REQUEST information. Is that wrong? Should I be placing $name, $email, $topic, & $comments (lines 32-34) in some sort of brackets like { }, [ ], ??
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Code stopped working

Post by RobertGonzalez »

What you should be doing is checking if they are set. If there are a ton of items in the $_REQUEST array then you could just check empty() on the $_REQUEST array.

On a side note, if you know things are coming from $_POST (forms), reference the $_POST array. If data is coming from $_GET (querystring) reference $_GET, from $_COOKIE (cookies - go figure) use $_COOKIE and $_SESSION use $_SESSION.

Another thing you could do is conditional check each array member to see if they are set using isset(). In any event, you should always check the existance of variable and array members that you did not create yourself.
ggrant3
Forum Newbie
Posts: 10
Joined: Wed Jan 16, 2008 10:37 am

Re: Code stopped working

Post by ggrant3 »

Everah wrote:What you should be doing is checking if they are set. If there are a ton of items in the $_REQUEST array then you could just check empty() on the $_REQUEST array.

On a side note, if you know things are coming from $_POST (forms), reference the $_POST array. If data is coming from $_GET (querystring) reference $_GET, from $_COOKIE (cookies - go figure) use $_COOKIE and $_SESSION use $_SESSION.

Another thing you could do is conditional check each array member to see if they are set using isset(). In any event, you should always check the existance of variable and array members that you did not create yourself.

Well I added the $_SESSION variable to the top of my script

Code: Select all

// initiate session
session_start();
    
// Check that the form has been submitted and the name is not empty
    if ($_POST && !empty($_POST['name'])) {
        // Set session name variable
    $_SESSION['name'] = $_POST['name'];
    }
    if ($_POST && !empty($_POST['email'])) {
        // Session email variable
    $_SESSION['email'] = $_POST['email'];
    }
    if ($_POST && !empty($_POST['topic'])) {
        // Session topic variable
    $_SESSION['topic'] = $_POST['topic'];
    }
    if ($_POST && !empty($_POST['comments'])) {
        // Session comments variable
    $_SESSION['comments'] = $_POST['comments'];
    }
and I am now getting the results I wanted. Thank You
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Code stopped working

Post by RobertGonzalez »

You are very welcome.
ggrant3
Forum Newbie
Posts: 10
Joined: Wed Jan 16, 2008 10:37 am

Re: Code stopped working

Post by ggrant3 »

Although, since I have done the $_SESSION variable info my form has not operated right.

My whole plan is for someone to fill out my form and then after they submit the form (and it is emailed to me) for them to be redirected to another page with a thank you message which will also echo the information that they entered in.

But on my form I want some required fields and I can't seem to get the required fields and the visitors echo'd message on the confirmation page to all work together.

For example before I did the $_SESSION variable my form would display an error message if one or all of the required fields was left blank, but now I don't get any error messages if fields are left blank.

I don't know if I explained that well enough here is the other thread which I thought was different from this one, but the more I think about it, I think it is related because I didn't have this problem before the $_SESSION variable information was added, this problem came up after $_SESSION was added

viewtopic.php?f=1&t=77697
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Code stopped working

Post by RobertGonzalez »

In a situation like this, var_dump() is your friend. I would make sure you are using var_dump() on the $_POST and $_SESSION arrays on all pages you are working on. THIS IS JUST FOR TESTING AND SHOULD BE USED IN PRODUCTION.

var_dump() dumps all of the data in a variable to the screen, so you may not want that open to the public. But it is useful for telling you what is in a variable.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Code stopped working

Post by Jonah Bron »

Also, on line 72, I think, there was an illigal apostrophe in the regex string:

Code: Select all

$checkEmail = '/^[^@]+@[^\s\r\n>>'<<";,@%]+$/';
Post Reply