-
Need some help with simple PHP script!
@ thanks for the advice and yes this certainly answers my question regarding server-side validation and the filter_var is the route I need to take. Just checking for "blank values" and echoing any errors, is not as helpful to me as actually checking that say, a validly formatted email address has been added into the field as opposed to just "something has been added into that field". @@NOCK thanks for the advice and yes I am a novice (does it show?!) - but what I was looking for was really some form of explanation of what I had done right and wrong and indeed what could be done or added to improve - so such as webdesigner93 - those were the kind of suggestions I was after. @@Lyndsey yes personally I have chosen to have all my html on one page (the contact form page) and run the php code separately within a .php page thereby keeping the two separate. I know others prefer to place the php on the same page as html..does it matter or just a personal preference?
-
-
Need some help with simple PHP script!
I have been teaching myself PHP for the last month and have come up with this script. I'm sure its not perfect but that's the point. I was really hoping for some folk on this forum who actually know more than I about php who can can look at my script and critique or make suggestions giving me indications of whether it needs tweaking. Telling me to watch some videos on youtube is quite unhelpful.
- Need some help with simple PHP script!
- Need some help with simple PHP script!
-
Need some help with simple PHP script!
Hi Nock, thanks for the response. So the url field is hidden and intended for anti-spam. Basically any bot can fill this in but I don't want the form then emailed to me if this happens. Are you saying that my code for that was wrong and that your code if(!isset($_POST['url'])){ will ensure that if this field is ever filled in the form won't get sent?
-
Need some help with simple PHP script!
I am new to PHP (please be gentle!) and so getting my head around it. I have just created my contact form and the code looks like this <form action="/sendform.php" method="post"> <p>Your name: <input type="text" name="name"></p> <p>Your email: <input type="text" name="email"></p> <p>Your telephone: <input type="text" name="telephone"></p> <p class="nospam">Leave this empty: <input type="text" name="url"></p> <p><textarea name="message"></textarea></p> <p><input type="submit" value="Send"></p> </form> I have created a separate sendform.php file and the code within this looks as follows:- <?php if(isset($_POST['url']) && $_POST['url'] == ''){ $youremail = 'me@example.com'; $body = "This is the form that was just submitted: Name: $_POST[name] E-Mail: $_POST[email] Message: $_POST[message]"; if( $_POST['email'] && !preg_match( "/[\r\n]/", $_POST['email']) ) { $headers = "From: $_POST[email]"; } else { $headers = "From: $youremail"; } mail($youremail, 'Contact Form', $body, $headers ); } header('Location: http://www.example.com/thanks/'); ?> So the first line of code (if I have got this correct) I am tryint to tell the form not to send the form to me, if the url field is filled in [as this is intended as a hidden field]. Further down I added the [!preg_match] to help protect the script from email injection. I have heard this is the correct way to do this? Is this form okay as it stands? And in terms of validation, is there a simple piece of code I can add somewhere within this script to ensure that the user fills in all the required fields ie Name, Email, Telephone & Message? Thanks so much for any help and advice on this.. I have been researching for days!