Jump to content

PHP Code Completion Issues


theGiNX

Recommended Posts

Hello there, new to the forums as a poster but I'm a frequent visitor. I'm currently troubleshooting a simple PHP form script. The issue is that it always defaults to the error.htm page instead of processing through to the ok.htm page and emailing the information out. What do you guys need so that I can possibly get some help.

 

3 People have looked at the code and say it's fine. Any suggestions on what else it could be if the PHP script is not running through?

 

I appreciate any assistance as I'm at wits end.

 

Thanks

Link to comment
Share on other sites

Here's the code. It's pretty basic.

 

<?php

 

// get posted data into local variables

$EmailFrom = "xxx@xxt.com";

$EmailTo = "xxx@xxt.com";

$Subject = "EEO Affirmative Action Data ";

$Name = Trim(stripslashes($_POST['Name']));

$Email = Trim(stripslashes($_POST['Email']));

$Female = Trim(stripslashes($_POST['Female']));

$Male = Trim(stripslashes($_POST['Male']));

$PositionApplyingFor = Trim(stripslashes($_POST['PositionApplyingFor']));

$WhereFindthePosition = Trim(stripslashes($_POST['WhereFindthePosition']));

$AmericanIndian = Trim(stripslashes($_POST['AmericanIndian']));

$Asian = Trim(stripslashes($_POST['Asian']));

$Black = Trim(stripslashes($_POST['Black']));

$Latino = Trim(stripslashes($_POST['Latino']));

$OtherPacificIslander = Trim(stripslashes($_POST['OtherPacificIslander']));

$White = Trim(stripslashes($_POST['White']));

$Multiracial = Trim(stripslashes($_POST['Multiracial']));

$Groups = Trim(stripslashes($_POST['Groups']));

$Comments = Trim(stripslashes($_POST['Comments']));

 

// validation

$validationOK=true;

if (Trim($Name)=="") $validationOK=false;

if (Trim($Email)=="") $validationOK=false;

if (Trim($Female)=="") $validationOK=false;

if (Trim($Male)=="") $validationOK=false;

if (Trim($PositionApplyingFor)=="") $validationOK=false;

if (Trim($WhereFindthePosition)=="") $validationOK=false;

if (Trim($AmericanIndian)=="") $validationOK=false;

if (Trim($Asian)=="") $validationOK=false;

if (Trim($Black)=="") $validationOK=false;

if (Trim($Latino)=="") $validationOK=false;

if (Trim($OtherPacificIslander)=="") $validationOK=false;

if (Trim($White)=="") $validationOK=false;

if (Trim($Multiracial)=="") $validationOK=false;

if (Trim($Groups)=="") $validationOK=false;

if (Trim($Comments)=="") $validationOK=false;

if (!$validationOK) {

  print "<meta http-equiv=\"refresh\" content=\"0;URL=errorEEO.htm\">";

  exit;

}

 

// prepare email body text

$Body = "";

$Body .= "Name: ";

$Body .= $Name;

$Body .= "\n";

$Body .= "Email: ";

$Body .= $Email;

$Body .= "\n";

$Body .= "Female: ";

$Body .= $Female;

$Body .= "\n";

$Body .= "Male: ";

$Body .= $Male;

$Body .= "\n";

$Body .= "PositionApplyingFor: ";

$Body .= $PositionApplyingFor;

$Body .= "\n";

$Body .= "WhereFindthePosition: ";

$Body .= $WhereFindthePosition;

$Body .= "\n";

$Body .= "AmericanIndian: ";

$Body .= $AmericanIndian;

$Body .= "\n";

$Body .= "Asian: ";

$Body .= $Asian;

$Body .= "\n";

$Body .= "Black: ";

$Body .= $Black;

$Body .= "\n";

$Body .= "Latino: ";

$Body .= $Latino;

$Body .= "\n";

$Body .= "OtherPacificIslander: ";

$Body .= $OtherPacificIslander;

$Body .= "\n";

$Body .= "White: ";

$Body .= $White;

$Body .= "\n";

$Body .= "Multiracial: ";

$Body .= $Multiracial;

$Body .= "\n";

$Body .= "Groups: ";

$Body .= $Groups;

$Body .= "\n";

$Body .= "Comments: ";

$Body .= $Comments;

$Body .= "\n";

 

// send email

$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

 

// redirect to success page

if ($success){

  print "<meta http-equiv=\"refresh\" content=\"0;URL=okEEO.htm\">";

}

else{

  print "<meta http-equiv=\"refresh\" content=\"0;URL=errorEEO.htm\">";

}

?>

 

Link to comment
Share on other sites

Nope. Still defaults to the errorEEO.htm page.

 

However, a fellow coworker posted just the PHP and the html page by itself and she says it works perfectly. So my question is - if it's not the code what else could be effecting the PHP from running to completion?

 

Thanks for helping :) I really appreciate it.

Link to comment
Share on other sites

The code is going to the same error page if the validation fails or the mail() function call fails. This could be anything, from a error in the form so that an input field does not match, a type, a php/mail problem...

 

You can start by checking your web server log for errors and/or turn on full php error reporting to get php to help you.

 

You also need to produce and output a unique message for each possible error. Each field that does not contain a value needs a message stating exactly what the problem was and the mail() function needs a separate message that states that the mail() function call failed and the mail could not be sent.

 

By lumping all the conditions together, there is no way you will be able to find out which condition is failing and causing the code to goto the error page.

Link to comment
Share on other sites

To try to narrow down the likely culprits:

 

1. Is your script being run on your local computer or a remote webserver?

2. Is a mailserver set up in the php.ini config for PHP to send emails through?

3. When your coworker tested your script, was she using the same webserver? Or another?

4. Is error_reporting on in the php.ini?

5. Replace the meta redirects with an echoed message to tell us exactly where the problem is.

6. Can you do print_r($_POST) and show us the input the script is receiving?

 

That should help loads!

 

EDIT:

Another thing the perfectionist in me won't lay to rest :P, disregard it if it doesn't really matter to you, but here's a way to shorten your script and make it a little more flexible. Also, if it doesn't fix the problem, it should tell you where the problem is!

 

<?php

// get posted data into local variables
$EmailFrom = "xxx@xxt.com";
$EmailTo = "xxx@xxt.com";
$Subject = "EEO Affirmative Action Data ";

// you want to change this to reflect the name of your submit button
unset($_POST['submit']);
$errors = array();

// Iterate over $_POST
$Body = '';
foreach($_POST as $key=>$value)
{
    $input = trim(stripslashes($value));
    if( empty($input) )
    {
        $errors[] = "The field $key was left empty!";
    }
    $Body .= "$key: $value \n";
}

// send email and redirect to success page
if ( mail($EmailTo, $Subject, $Body, "From: $EmailFrom") && empty($errors) ){
    $url = 'okEEO.htm';
} else {
    $errors[] = 'Failed to send out the email!';
    $url = 'errorEEO.htm';
}

// Use the first line for debugging, uncomment the second for actual use
print implode("\n", $errors);
//print "<meta http-equiv=\"refresh\" content=\"0;URL=$url\">";

?>

 

Someone might argue it's a little less secure (like people could fake POST entries), but there are many different ways to fix that while only adding two or three additional lines. If it concerns you, just ask and I'll deliver! But like I said, only if it matters to you!

Link to comment
Share on other sites

To try to narrow down the likely culprits:

 

1. Is your script being run on your local computer or a remote webserver?

2. Is a mailserver set up in the php.ini config for PHP to send emails through?

3. When your coworker tested your script, was she using the same webserver? Or another?

4. Is error_reporting on in the php.ini?

5. Replace the meta redirects with an echoed message to tell us exactly where the problem is.

6. Can you do print_r($_POST) and show us the input the script is receiving?

 

That should help loads!

 

EDIT:

Another thing the perfectionist in me won't lay to rest :P, disregard it if it doesn't really matter to you, but here's a way to shorten your script and make it a little more flexible. Also, if it doesn't fix the problem, it should tell you where the problem is!

 

 

Thanks for the other code. I'll look into it.

 

Well I did identify a few possible things with it - like Trim should be trim. But it's just not cooperating yet.

 

As for the list..

 

1. Is your script being run on your local computer or a remote webserver? - webserver

2. Is a mailserver set up in the php.ini config for PHP to send emails through? Yes

3. When your coworker tested your script, was she using the same webserver? Or another? Another

4. Is error_reporting on in the php.ini? No

5. Replace the meta redirects with an echoed message to tell us exactly where the problem is. see what I can do6. Can you do print_r($_POST) and show us the input the script is receiving? see what I can do

 

I'm determined to figure it out. I'm still a novice with PHP though. Going to need to read up on it more.

 

 

Someone might argue it's a little less secure (like people could fake POST entries), but there are many different ways to fix that while only adding two or three additional lines. If it concerns you, just ask and I'll deliver! But like I said, only if it matters to you!

 

What lines of code would that be?

Link to comment
Share on other sites

 

1. Is your script being run on your local computer or a remote webserver? - webserver

2. Is a mailserver set up in the php.ini config for PHP to send emails through? Yes

3. When your coworker tested your script, was she using the same webserver? Or another? Another

4. Is error_reporting on in the php.ini? No

5. Replace the meta redirects with an echoed message to tell us exactly where the problem is. see what I can do6. Can you do print_r($_POST) and show us the input the script is receiving? see what I can do

 

I'm determined to figure it out. I'm still a novice with PHP though. Going to need to read up on it more.

 

Alright, my suspicion is that the mailserver you've configured in your webserver may be set up correctly or may not be working.

 

You may want to head into php.ini and set the following:

 

display_errors = On
error_reporting  =  E_ALL

 

This should show you any problems (if PHP can see them). The problem is likely inherent to your webserver's PHP config. Also, what version of PHP are you running? If the problem doesn't exist in the $_POSTed data, then it's the configuration.

 

Might want to check the [mail function] section of the php.ini. Another common problem is your SMTP port is blocked and should be changed. It looks like this:

 

[mail function]
; For Win32 only.
SMTP = mail.server.com
smtp_port = 26

; For Win32 only.
sendmail_from = localhost@email.com

 

What lines of code would that be?

 

Here's the revised code:

 

<?php

// get posted data into local variables
$EmailFrom = "xxx@xxt.com";
$EmailTo   = "xxx@xxt.com";
$Subject   = "EEO Affirmative Action Data ";

// you want to change this to reflect the name of your submit button
$allowedFields = array('Name', 'Email', 'Female', 'Male', 'PositionApplyingFor', 'WhereFindthePosition', 'AmericanIndian', 'Asian', 'Black', 'Latino', 'OtherPacificIslander', 'White', 'Multiracial', 'Groups', 'Comments');
$errors = array();

// Iterate over $_POST
$Body = '';
foreach($allowedFields as $value)
{
    $input = trim(htmlentities($_POST[$value]));
    if( empty($input) ){
        $errors[] = "The field $key was left empty!";
    }
    $Body .= "$value: $input \n";
}

// send email and redirect to success page
if ( mail($EmailTo, $Subject, $Body, "From: $EmailFrom") && empty($errors) ){
    $url = 'okEEO.htm';
} else {
    $errors[] = 'Failed to send out the email!';
    $url = 'errorEEO.htm';
}

// Use the first line for debugging, uncomment the second for actual use
print implode("\n", $errors);
//print "<meta http-equiv=\"refresh\" content=\"0;URL=$url\">";

?>

 

I changed stripslashes to htmlentities--and added an $allowedFields array--it's a little messy, but it's one of the better ways.

Link to comment
Share on other sites

 

1. Is your script being run on your local computer or a remote webserver? - webserver

2. Is a mailserver set up in the php.ini config for PHP to send emails through? Yes

3. When your coworker tested your script, was she using the same webserver? Or another? Another

4. Is error_reporting on in the php.ini? No

5. Replace the meta redirects with an echoed message to tell us exactly where the problem is. see what I can do6. Can you do print_r($_POST) and show us the input the script is receiving? see what I can do

 

I'm determined to figure it out. I'm still a novice with PHP though. Going to need to read up on it more.

 

Alright, my suspicion is that the mailserver you've configured in your webserver may be set up correctly or may not be working.

 

You may want to head into php.ini and set the following:

 

display_errors = On
error_reporting  =  E_ALL

 

This should show you any problems (if PHP can see them). The problem is likely inherent to your webserver's PHP config. Also, what version of PHP are you running? If the problem doesn't exist in the $_POSTed data, then it's the configuration.

 

Might want to check the [mail function] section of the php.ini. Another common problem is your SMTP port is blocked and should be changed. It looks like this:

 

[mail function]
; For Win32 only.
SMTP = mail.server.com
smtp_port = 26

; For Win32 only.
sendmail_from = localhost@email.com

 

I changed stripslashes to htmlentities--and added an $allowedFields array--it's a little messy, but it's one of the better ways.

 

Ok. After continuous troubleshooting this. I'm pretty sure it's got to be that the server is not running the up-to-date PHP. It runs fine on an apache web server + php version 4.x - I'm working with IIS. I'll hit up the Network Tech and try to get this worked out. If it works I'll let you guys know - if it doesn't.... well then I'll be back lol.

 

Thank you so much for all your suggestions. And thanks 8Ball for the updated code - I'll look into that more.

 

 

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.