Jump to content

Code is complete...but doesn't run..get 500 error


Luke Martin

Recommended Posts

Hi

 

I am using this form www.gruffe.co.uk/contact.htm

 

It should pass a variable called 'return' to the php form (www.gruffe.co.uk/contact.php)

 

But whenever I run it I get a server 500 error! The code in the PHP is as follows :

 

<?php

// Website Contact Form Generator

// http://www.tele-pro.co.uk/scripts/contact_form/

// This script is free to use as long as you 

// retain the credit link 

 

// get posted data into local variables

$EmailFrom = "info@gruffe.co.uk";

$EmailTo = "info@gruffe.co.uk";

$Subject = "Webmail Enquiry";

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

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

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

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

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

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

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

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

 

// validation

$validationOK=true;

if (!$validationOK) {

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

  exit;

}

 

// prepare email body text

$Body = "";

$Body .= "First: ";

$Body .= $First;

$Body .= "\n";

$Body .= "Last: ";

$Body .= $Last;

$Body .= "\n";

$Body .= "Company: ";

$Body .= $Company;

$Body .= "\n";

$Body .= "Address: ";

$Body .= $Address;

$Body .= "\n";

$Body .= "Telephone: ";

$Body .= $Telephone;

$Body .= "\n";

$Body .= "Mobile: ";

$Body .= $Mobile;

$Body .= "\n";

$Body .= "Website: ";

$Body .= $Website;

$Body .= "\n";

$Body .= "Message: ";

$Body .= $Message;

$Body .= "\n";

 

// send email

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

 

 

header('Location: '.$_POST['return']);

exit();

 

}

?>

 

Link to comment
Share on other sites

firstly, please use the code or php bbcodes in your post. I dislike having to read a script in normal text, as do others here.

 

First thing i notice is that your using Trim() instead of trim(), functions in PHP are case sensitive so this shouldn't actually be working. I also question why your a printing a <meta> refresh tag when a header('location') call would work quite as well (as at the bottom of the script), also, im not sure if it makes a difference (i don't think it does) but the action attribute in your contact form is set to POST in uppercase. This probably has no affect but having <body> half way down the page after the form has been created could affect something, move <body onload="setUrl()"> right under </head>

see if that works

Link to comment
Share on other sites

I will adjust this going forward...sorry

 

But I know it works (although it is scruffy) in its current...the elements that are failing me are :

 

In the HTM file :

 

<input type="hidden" name="return" id="return" />

 

I know the variable is being recorded correctly as the onSubmit action production an alert, verifying the URL (for testing).

 

But it doesn't seem to be making it through to the PHP file...as dumps me at 500 error

 

header('Location: '.$_POST['return']);

exit();

 

The script worked fine, i just wanted to add this so the user would be redirected back to then page they came from after submit...

Link to comment
Share on other sites

Hmmm i created it...

 

<?php

 

echo out $_POST['return']

 

}

?>

 

I get server 500 error again

 

Every other variable feeds through but this return clearly isn't, although it is present in the HTM prior to POST

 

Something to do with fasthosts being poo as usual?

 

(Not sure what that <meta> refresh thing does, just came off a generator...this is my first PHP)

Link to comment
Share on other sites

In fact....I just loaded the below which has the desired effect....re-directs me back ;

 

<?php

header('Location: '.$_POST['return']);

exit();

?>

 

So it must be something else....the script above it works fine on its own (email post script) but the two dont work together!

Link to comment
Share on other sites

First thing i notice is that your using Trim() instead of trim(), functions in PHP are case sensitive so this shouldn't actually be working.

 

Functions in PHP are case-insensitive.

 

wow really? i always thought they were lol.

 

perhaps the error is something to do with mail(). Im not sure if there is a difference but perhaps you can't have < and > around your From: header.

Link to comment
Share on other sites

No idea why you need to be using exit() when after redirecting.

 

Try this:

 

<?php
// Website Contact Form Generator 
// http://www.tele-pro.co.uk/scripts/contact_form/ 
// This script is free to use as long as you  
// retain the credit link  

function sanitize($input) {
  $input = stripslashes($input);
  return trim($input);
}

// get posted data into local variables
$EmailFrom = "info@gruffe.co.uk";
$EmailTo   = "info@gruffe.co.uk";
$Subject   = "Webmail Enquiry";

$First     = sanitize($_POST['First']); 
$Last      = sanitize($_POST['Last']); 
$Company   = sanitize($_POST['Company']); 
$Address   = sanitize($_POST['Address']); 
$Telephone = sanitize($_POST['Telephone']); 
$Mobile    = sanitize($_POST['Mobile']); 
$Website   = sanitize($_POST['Website']); 
$Message   = sanitize($_POST['Message']); 

// prepare email body text
$Body  = "";
$Body .= "First: ";
$Body .= $First;
$Body .= "\n";
$Body .= "Last: ";
$Body .= $Last;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Address: ";
$Body .= $Address;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "Mobile: ";
$Body .= $Mobile;
$Body .= "\n";
$Body .= "Website: ";
$Body .= $Website;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

if (mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>")) {
  header('Location: ' . $_POST['return']); 
}
else {
  // Error occurred, mail did not send
}
?>

Link to comment
Share on other sites

How would yours vary to mine...other than being more compact...(now works) :

 

// send email

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

 

if ($success){header('Location: '.$_POST['return']);

exit();

 

}

else{

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

 

}

?>

 

So where I have an IF statement....yours would just run and if it runs successfully, it will do the redirect?

 

Sorry im learning as i go, first PHP!

 

Link to comment
Share on other sites

Also somebody said I should put my variable like this ?

 

$Company = (isset($_POST['Company'])) ? trim(stripslashes($_POST['Company'])) : FALSE;

 

As there is no validation.....am i correct in thinking I will never have a need for an error page?

 

If i ever developed such a need...would it be possible to tell the user in a message box where the errors are before posting the HTM to PHP ?

Link to comment
Share on other sites

I'm unsure as to why you had the $validateOK variable and then you had an IF statement use that variable. It didn't seem to be doing anything so I removed that. I removed the second exit() down the bottom, since there really is no need for it.

 

And yes, as you said.. I put the mail() function within an IF statement, so if it succeeds proceed with the redirect, otherwise produce an error.

Also somebody said I should put my variable like this ?

 

$Company = (isset($_POST['Company'])) ? trim(stripslashes($_POST['Company'])) : FALSE;

 

As there is no validation.....am i correct in thinking I will never have a need for an error page?

 

If i ever developed such a need...would it be possible to tell the user in a message box where the errors are before posting the HTM to PHP ?

 

If you have no requirement for validation then no, you don't need this. However, it is good practice to use it.

Link to comment
Share on other sites

Ok

 

The reason I didnt want validation it that this form will be voluntary....so i dont want to force potential clients to enter an email address etc if they dont have one...thus preventing them from submitting the sheet.

 

This script should work every time (as there is no validation) so an error event would only occur if the server was down, correct?

 

I would ideally like it so that if it was a success "Thank you for your enquiry" message box, else produce "Error : Please try again" (which should never occur) message box using the alert(document.getElementById('return').value) style popup that is currently sat on the HTM...but may be better in the PHP file where you do the IF statement ???

 

How would this look?

 

nearly done...at least it works now!

Link to comment
Share on other sites

Validation doesn't have to force the user to do anything if you don't want.

 

For example, a user could easily enter anything they want into the e-mail address field, and it may not be a valid e-mail address. So you'd be trying to send an e-mail to an invalid e-mail address. Validation can help solve this issue by checking to see whether or not a user has entered a valid e-mail address.

 

Or another example would be to check whether or not they've entered a legit first name and last name (by legit I don't mean it has to be their actual name because there is no way to validate that) but to check to see wether the user has entered any illegal characters such as "@$^£% etc..

 

Also you can validate the phone numbers and website addresses by checking for specifics. A phone number should only contain spaces, numbers and hyphens. And a website address should (but not always) contain "http://" and ".com, .net, .org" etc..

Link to comment
Share on other sites

Hmmmm....could you provide an example based on my script.

 

My only fear is that they will enter something like "mobile 07725635620" and then the script will dump them back...and they wont be bothered to correct

 

Would rather just allow them to blindly submit...everything is captured and sent to my info@ mailbox.

 

UNLESS!!!

 

I'm guessing if I wanted validation it would be more difficult....as i would like it to tell the user what was incorrect....in a message box and then make them correct it before the form submits to pHP

 

Wouldnt this need to be java....as I want to keep the file as HTM

 

I struggled to get the script to this stage. I would validation but have no web program knowledge

Link to comment
Share on other sites

No idea why you need to be using exit() when after redirecting.

 

The header redirect does NOT end the PHP script.  It is a function that places an HTML header into the queue to be sent with the rest of the headers. As a result, if you do NOT put an exit() after this header() function call, the script will continue to run.  If the programmer "thinks" the header() call ended the script and sent the user away, there may be sensitive code that is otherwise unprotected.

 

if ($_SESSION['user'] == 'The Boss') {
    header('Location: http://www.mysite.com/IamWorking.php');
}

echo 'The Boss is a stupid jerk!';

 

In this case, even though you have sent a header for "The Boss" the echo will still be executed. If there is some delay in the redirect, that text may actually make it to the screen. If, instead of an echo, we perform some database manipulations because we think we have a valid user, those manipulations will be executed either way. Headers are (apparently) sent to the browser in one chunk, not one at a time as you call header().

 

In the OP's script, since the header() is the last thing in the script, it is technically not necessary to include the exit(). However, it is not a good habit to get into. If some code were later added after that part of the script, the header() would be left hanging.  Just be aware, the header() call DOES NOT END THE SCRIPT.

 

Link to comment
Share on other sites

How would I generate a Msgbox from the PHP. I tried this but gives me a server 500 error

 

if ($success){header('Location: '.$_POST['return']);

alert(document.getElementById('return').value);

exit();

 

}

 

This obviously work within the HTM file onsubmit...but id like one message box for "Message success" and a different one for "Message fail" (which in theory, given the current lack of validation, would never appear...but want there for future devel)

 

Thoughts?

 

Link to comment
Share on other sites

You cannot call a javascript function from PHP.  There are a couple options here:

 

1) redirect to one page for success and a different page for failure. You can provide a link on that page to take the user where you actually want them to go.  You can also put an HTML META refresh tag on that page to cause the page to load their ultimate destination after a few seconds.  However, include the link because I have heard that the META refresh is not reliable.

 

2) Echo the success or failure to the current page (get rid of the headers) and include a link to their ultimate destination. You can also include the META refresh on this page.

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.