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!
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: Posting Code in the Forums to learn how to do it too.
Hey. I'm a true novice. Web designer by day, i'm doing a website for myself now (finally) which means i've turned developer overnight and i'm way out of my comfort zone.
It's a very simple contact form - here's the code:
This works, but it doesn't work exactly how i'd like it to since it's essentially a cobbled together first attempt. Currently if there's an error you're directed to a new page. Instead, I'd like the error to be highlighted in the form page, with the other fields retaining the text already input. I searched until the wee small hours of the morning for some answers, and found nothing I could get my head around. If someone doesn't mind helping me out i'd be so grateful! Thankyou!!!
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: Posting Code in the Forums to learn how to do it too.
It seems that this is not the whole page code - I imagine your HTML form is also on this page?
There are a few things I'd fix here - all novice problems - certainly nothing to be ashamed of. One of the biggest problems early users of PHP have is spaghetti code: the mixture of business logic (code that works on data) and display logic (code that displays something to the user). There's a little bit of that here. To me at least, it looks like you're jumping around a bit. Here's how I'd re-arrange the file:
Declare your variables to be used to send the email and to insert into the form. 1 variable per datum is sufficient. Also declare an $error string for storing your error message. All these variables will be used to transfer information from the business logic, to the display logic.
Check if the form has been submitted. If Yes:
Yes: clean up any slashes that have been added. get_magic_quotes_gpc() will need to be used - a standard call to stripslashes() could remove legitimate slashes. Store the cleaned up strings in your variables you declared above.
Call checkInput (convention says use 'camel case' rather than your check_input format). Have it return boolean TRUE if the input was fine, FALSE if not. checkInput() should be business logic & not concern itself with communicating to the user.
Where there any problems with the input? If yes, generate your error message, but don't display it.
If no, send your email, forward the user, and exit() [It is not necessary to exit() all the time. Some browsers don't behave 100% nicely with headers though, so it's always a good idea to do what you've done & put an exit() statement after any forwarding headers].
If the form hasn't been submitted, no special action needs to be done
Now, into the display logic. Your page can be split up like this: