Log in

View Full Version : Contact.php problems



Biglad
02-04-2011, 02:51 PM
Hello all I`m having problems with a contact.php file.

I have made up the contact form and seams to work fine but anytime anything is or isnt filled in the form goes straight to the thankyou page, which is going to be a problem because even if the visitor hasn`t filled the form in it`ll go to the thankyou page, i have tried adding different ways to stop this from happening but as i`m a novice i dont know what to do next.

Can anyone help me out?

Thanks

Nile
02-04-2011, 08:51 PM
There are many ways to accomplish form validation - one of them being with php and another being javascript.



<?php
if(isset($_POST['submit'])){ //check to see if the page was submitted
$errors = false; //always assume your user has no errors from the beginning

if(empty($_POST['name'])){ //check if the name field was empty
$errors['name'][] = "Name field was empty.";
}
if(preg_match("/^[a-zA-Z]+$/", $_POST['name'])){ //if the name has other letters then a-z or A-Z
$errors['name'][] = "Name must contain only letters from a-z and A-Z";
}
if(empty($_POST['subject'])){
$errors['subject'][] = "Subject field was empty";
}
if(preg_match("/^[a-zA-Z]+$/", $_POST['subject'])){
$errors['subject'][] = "Name must contain only letters from a-z and A-Z";
}
if(count($errors) > 0){ //if there were errors in the $errors variable
foreach($_POST as $key => $value){
$errors[$key]['value'] = $value;
}
} else {
// NO ERRORS
}

} else { //if the page didn't get a post key with the name 'submit'
echo "No information submitted.";
}

This is how I would approach it in PHP. Check everything you need and then put it in a multidimensional that has the errors. Then, check for errors. If there are errors, add the value of each field to it's own array so that the form doesn't empty when the user fixes his problems. Output all the errors within every array of the parent array that dont have the key 'value'

Biglad
02-04-2011, 09:03 PM
Hi thanks alot for replying i really appreciate your help,

I tried what you gave but it didnt seam to work, It may have something to do with the way the actual for is set up with the name for instance is inside the form instead of out of it if you know what i mean?

here is the code for html and php



<form id="form1" name="form1" method="post" action="http://www.#####.co.uk/contact.php">
<p>For a free, no obligation quote, simply fill in the the short form below:</p>
<p><input name="fullname" type="text" class="default-value" value="Full Name:" /></p>
<p><input name="businessname" type="text" class="default-value" value="Business Name:" /></p>
<p><input name="address" type="text" class="default-value" value="Address:" /></p>
<p><input name="postcode" type="text" class="default-value" value="Postcode:" /></p>
<p><input name="emailaddress" type="text" class="default-value" value="Email Address:" /></p>
<input name="phonenumber" type="text" class="default-value" value="Phone Number:" /></p>
<p><textarea name="quotedetails" id="textarea" cols="45" rows="5" class="default-value">Additional Details:</textarea></p>
<p><input name="verif_box" type="text" id="verif_box" class="default-value" value="Please Describe The Image:" /></p>
<p><img src="verificationimage.php?%3C?php%20echo%20rand(0,9999);?%3E" alt="PLEASE DESCRIBE THE IMAGE" /></p>
<p>
<input type="submit" name="Click for Quote" value="Click for Quote" />
<input type="reset" name="reset" value="Reset" />
</p>
</form>


<?php
$fullname = $_POST["fullname"];
$businessname = $_POST["businessname"];
$address = $_POST["address"];
$postcode = $_POST["postcode"];
$emailaddress = $_POST["emailaddress"];
$phonenumber = $_POST["phonenumber"];
$quotedetails = $_POST["quotedetails"];
$verif_box = $_POST["verif_box"];

$to = "info@#####.co.uk";
$subject = "enquiry";
$headers = "From: $emailaddress\n";
header("location:http://www.#####.co.uk/thankyou.html");


$message = "A visitor to your site has filled out the following information.\n
Full Name: $fullname
Business Name: $businessname
Company Address: $address
Post Code: $postcode
Email Address: $emailaddress
Phone Number: $phonenumber
Quote Details: $quotedetails ";

if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
mail($to,$subject,$message,$headers);
}

?>

Hopefully you can help me out as i dont really know what i`m doing, thanks again

Nile
02-04-2011, 10:17 PM
Yeah - I don't know if that's the best idea though.

You can take a look at the source of this page: http://unlinkthis.net/scripts/user-login/contact.php (which for some reason does not have validation so dont send me empty emails, please!)

AND: http://css-tricks.com/video-screencasts/21-walkthrough-of-contact-form/

But don't put the field labels inside of the actual field.

Biglad
02-04-2011, 10:26 PM
Hi thanks for the reply again,

I wanted to keep it like that with the text inside the field for the look of my site, as i`d have to totally change the site to make the contact form fit, It`s on every page of the site because of the nature of the site and not just on a contact page, i think this is why i`m having the problems because of the text being inside the form.

Is there any other way to do this with the text inside the form?

Thanks

Nile
02-04-2011, 10:32 PM
You could, instead of using the empty() function, use a comparison operator (==)

So:


if(empty($_POST['fullname']) || $_POST['fullname'] == "Full Name:"){ //check if the name field was empty
$errors['fullname'][] = "Name field was empty/unchanged.";
}

But again, I would still suggest a better interface.

Biglad
02-04-2011, 10:36 PM
Hi i`ve just tried that but it still goes to the thankyou page if everything is left blank, it`s really frustrating, seams like i`ve tried everything.

any other options?

Thanks again

Nile
02-04-2011, 10:46 PM
You have to make sure this code is in contact.php - that's where it is, correct?

If I were doing this, I would combine the html with contact.php so that if there were fields that were invalid you could easily say which ones they were without having to set a cookie and back up.

Biglad
02-04-2011, 10:50 PM
Hi, yeah i placed the code below Quote Details: $quotedetails "; when you say combine the html with contact.php do you mean just place the whole html page inside the php file? (above it)

Thanks again

Nile
02-04-2011, 10:52 PM
No - place it after the PHP.


<?php

// PHP CODE HERE

?>

<!--
HTML HERE
-->

Biglad
02-04-2011, 11:06 PM
Hi now its giving me an error

Parse error: syntax error, unexpected $end in \\nas41ent\domains\p\#####.co.uk\user\htdocs\contact.php on line 161

Which is this </html>

Anything i do seams to result in either an error, the form not working or it just going to the thankyou page and not sending an email..

Thanks

Nile
02-06-2011, 12:02 AM
This usually happens when you forget to close a curly bracket, }

Biglad
02-06-2011, 12:06 AM
thanks for that, the php had no errors then i added the html like you said and it had the error, should i off added a } soemwhere even tho there wasnt an error before i added the html?

thanks

Nile
02-06-2011, 12:08 AM
No, paste all your code here.

Biglad
02-06-2011, 12:18 AM
<?php
$fullname = $_POST["fullname"];
$businessname = $_POST["businessname"];
$address = $_POST["address"];
$postcode = $_POST["postcode"];
$emailaddress = $_POST["emailaddress"];
$phonenumber = $_POST["phonenumber"];
$quotedetails = $_POST["quotedetails"];
$verif_box = $_POST["verif_box"];

$to = "info@#####.co.uk";
$subject = "enquiry";
$headers = "From: $emailaddress\n";
header("location:http://www.#####.co.uk/thankyou.html");


$message = "A visitor to your site has filled out the following information.\n
Full Name: $fullname
Business Name: $businessname
Company Address: $address
Post Code: $postcode
Email Address: $emailaddress
Phone Number: $phonenumber
Quote Details: $quotedetails ";

if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
mail($to,$subject,$message,$headers);
}

?>

<!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>Untitled Document</title>
</head>
<body>
<p>Thank you</p>
</body>
</html>

I`ve narrowed it down as the thankyou page i am using was is a full web page with all my details on ect..

Thanks again

Nile
02-06-2011, 12:39 AM
May I please see all the code? I don't see any issues with that.

Biglad
02-06-2011, 08:52 AM
Hi, i`m sure you will understand as my contact form isn`t safe from spammers, i`d like to keep it safe, and not broadcast the details to be picked up by search engines spammers ect, untill i get it sorted at least and as i still get the problem with the code i gave???

Thanks anyway

Nile
02-06-2011, 03:00 PM
I don't really understand. You want me to help you but you won't give me the code? If this is the issue, please PM me the code so i can take a look at it.

Biglad
02-09-2011, 09:44 PM
Hi Nile,

What part of the code do you need? the only thing missing is the site name and my email address, are they needed? or can i add them in later when the problem is solved?

thanks again

Nile
02-09-2011, 10:30 PM
I`ve narrowed it down as the thankyou page i am using was is a full web page with all my details on ect...

Hi, i`m sure you will understand as my contact form isn`t safe from spammers, i`d like to keep it safe, and not broadcast the details to be picked up by search engines spammers ect, untill i get it sorted at least and as i still get the problem with the code i gave???

Thanks anyway
From this, I understand there is more code other then just your email and website name.

Biglad
02-09-2011, 10:42 PM
Sorry i dont understand? which details do you need? that is the complete php file without my web address and email and that is the complete thankyou page.

Is there another part of the site i need to add to this to sort the contact.php out?

thanks again

Nile
02-10-2011, 03:40 AM
Hi now its giving me an error

Parse error: syntax error, unexpected $end in \\nas41ent\domains\p\#####.co.uk\user\htdocs\contact.php on line 161

Which is this </html>
You know what I want, don't do this... If you won't give me all of your code, I simply wont help. In the above quote, it says you have at least 161 lines in your contact.php. When putting the PHP you gave me in my text editor and checking the line count, it definitely does not have 161 lines.

Biglad
02-10-2011, 08:34 AM
I wanted help with the contact.php file not the thankyou page, this is why i dont understand why you need to see the rest of my site to fix it?

I find it very strange that you keep asking for my web address and email and have said i dont want to post them on hereand wont do. Simples

I know for a fact that it wont make any difference what so ever if you have my web address and email will it!!! so why keep asking for them why do you need them???? or is the fact that you don`t need them what so ever..:mad:

This is a really strange request as i said the lines that are missing are not needed... so i`ll find someone else that can help me, that doesn`t ask me for information they don`t NEED.

Thanks for your efforts anyway.

Nile
02-10-2011, 12:55 PM
There is nothing wrong with your code as far as I can see. Nobody will be able to help until they see all 161 lines of contact.php. If you actually aren't pasting your code (might I remind you almost everybody who helps another person on this site comes here FOR FREE, and gives there time to people who need help FOR FREE), then you're not getting help.
Good luck! (Your welcome, I'm glad to help)

Biglad
02-10-2011, 04:30 PM
Oh rite, I know what you mean now! The php file was bigger because I added the HTML file underneath it like you said to do! I just added with a space below the php text! And that is what made up the difference! As I said before that is all the text I used!!!!

So there was a total of 168 lines when the two were combined in the thankyou page was inserted below the contact.php section in the same file!!!

That is all the text there is no more that I will be adding later on!

There is no more text that I have to put in that is everything that I have to use! No more not hiding anything there is nothing missing

That is why I didn't know what you were talking about as I haven't taken anything out and all that I am using in the contact.php file and thankyou.HTML file

Hope you understand that there is no text missing

Thanks

Nile
02-11-2011, 08:23 PM
Oh, okay. I don't see why your getting this error then :/

fileserverdirect
02-13-2011, 11:44 PM
Sorry just to jump in here, but when you get a "$end" error, there is most likely a bracket-stacking problem. Try just adding one} before the last ?>. This is temporary, and you must find where you misplaced the bracket, it comprises your application's security and functionality.