/    Sign up×
Community /Pin to ProfileBookmark

How to setup form validation

I have got a contact form with php validation and the code or something is not working and I don’t understand why. I have researched for a suitable answer, but I have had no luck as when the user uses the contact form, then they receive “This page isn’t working” error. I have supplied a screenshot[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-12-06/1544138949-875701-error.png]

to post a comment
PHP

50 Comments(s)

Copy linkTweet thisAlerts:
@rootDec 07.2018 — Well thats nice but your PHP and HTML would be nice.

PHP code you use tags [code=php] your PHP inhere '[/code] and do similar for HTML form you are using.
Copy linkTweet thisAlerts:
@KyleJ144authorDec 07.2018 — [code=php]<?php

// error_reporting(E_WARNING);

function readURL($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}

$secret = " site-keye-here";
$verificationResponse = $_POST["g-recaptcha-response"];
if( empty($verificationResponse) ) die("Google did not POST the required g-recaptha-response");

$response = readURL("https://www.google.com/recaptcha/api/siteverify" . $secret . "&response=" . $verificationResponse . "");

$responseArray = json_decode($response, true);
if( $responseArray["success"] !== true) die("Invalid reCaptcha <a href="javascript:history.go(-1);">Try Again</a>");

/* Set e-mail recipient */
$myemail = "[email protected]";

/* Check all form inputs using check_input function */
$name = $_POST['name'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$dropdown2 = $_POST['dropdown2'];
$dropdown3 = $_POST['dropdown3'];
$subject = $_POST['subject'];
$message = $_POST['message'];

/* If e-mail is not valid show error message */
if (!preg_match("/([w-]+@[w-]+.[w-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */

$subject = "Contact Message from www.ncmaphia.co.uk";

$message = "

$name has sent you a message using your contact form:

Name: $name
Email: $email
Crew/Gang: $dropdown
Game: $dropdown2
Reason For Contacting Us: $dropdown3
Subject: $subject

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location:thank-you.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}

?>
[/code]

[code=html]
<div class="col-md-8 contact-grid">
<form action="form-process.php" method="POST">
<input name="name" type="text" value="Name" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='name';}">
<br>
<input name="email" type="text" value="Email" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='email';}">
<br>
<select id="contact" name="dropdown" onchange="handleOption(this)">
<option value="crew/gang">Crew/Gang</option>
<option value="nc maphia">NC Maphia</option>
<option value="deadeye/index">Dead Eye</option>
</select><br>
<select id="contact" name="dropdown2">
<option value="game">Game</option>
<option value="grand theft auto v">Grand Theft Auto V</option>
<option value="red dead redemption ii">Red Dead Redemption II</option>
</select><br>
<select id="contact" name="dropdown3">
<option value="reason for contacting us">Reason for contacting us?</option>
<option value="joining nc maphia">Joining NC Maphia</option>
<option value="recruitment requirments">Recruitment Requirments</option>
<option value="events">Events</option>
<option value="report a member">Report A Member</option>
<option value="webmaster">Webmaster</option>
<option value="general inquiry">General Inquiry</option>
</select><br>
<input name="subject" type="text" value="Subject" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='subject';}">
<br>
<textarea name="message" cols="77" rows="6" value=" " onfocus="this.value='';" onblur="if (this.value == '') {this.value = 'message';}">Message</textarea>
<div class="g-recaptcha" data-sitekey="site-keye-here"></div>
<div class="send">
<input type="submit" value="Send" >
</div>
</form>
</div>
[/code]

Sorry I am new to this website. I didn't know how to post the code, but here it is above.

Thanks in advance
Copy linkTweet thisAlerts:
@rootDec 07.2018 — In you HTML

&lt;form action="form-process.php" method="POST"&gt;
should have a form encode type and forms really should have names as should all inputs in a form, the ID is really meant for DOM objects that have no name tag.

The PHP side of things, there is error reporting that you can invoke and gather the errors in the script. That would pinpoint the module or point of failure in your script.

&lt;?php
exit();
}
?&gt;
why this?

Possibly part of the problem, you are running all those commands and then killing the output before it is sent???
Copy linkTweet thisAlerts:
@KyleJ144authorDec 07.2018 — So should it be like below:

[code=html]
<form id="test" action="form-process.php" method="POST">
[/code]
Copy linkTweet thisAlerts:
@KyleJ144authorDec 07.2018 — I have added the ID to the html on the form and deleted the exit bit on the php file and I am still getting the same error :/
Copy linkTweet thisAlerts:
@ginerjmDec 07.2018 — Re-read the initial reply. No id on a form. Name yes on a form. Although personally I don't find a use for either on a form.

Also - you don't have error checking turned on. The one line related to it is commented out. Should use E_ALL as the constant and add a line to display the errors on your client screen until you finish the development. Look it up in the php manual.
Copy linkTweet thisAlerts:
@KyleJ144authorDec 07.2018 — I have now un commented the following:

// error_reporting(E_WARNING);

/* Send the message using mail() function */

And the error is still their
Copy linkTweet thisAlerts:
@rootDec 07.2018 — @KyleJ144#1598733 Yes, but what is the error PHP reports.

Server 500 page errors are server errors and they occur when you are running a script that either violates, takes too long, uses too many resources and the server gives up.

You need to look at each module and what it is doing. What is the maximum time for script execution? I never write a script that takes more than 1/3rd that time because long scripts can get dumped if the server is flooded, the simplest defence of a server is to drop all connections when it is overwhelmed.
Copy linkTweet thisAlerts:
@KyleJ144authorDec 07.2018 — @root#1598734 How do I do that because i am new to php
Copy linkTweet thisAlerts:
@ginerjmDec 07.2018 — You need to read what you are given very closely. As I said - you should use the E_ALL constant in the error reporting setting and add the line that sends the messages to the client. Perhaps you should read a php reference to understand it better too.

Use this for error reporting and be sure to comment it out when done.
<i>
</i> error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');

Then you may see some error messages
Copy linkTweet thisAlerts:
@KyleJ144authorDec 07.2018 — @ginerjm#1598738 Do i put that at the top of the php file
Copy linkTweet thisAlerts:
@KyleJ144authorDec 07.2018 — @ginerjm#1598738 and where does the errors pop up
Copy linkTweet thisAlerts:
@KyleJ144authorDec 07.2018 — Quick up date. I think i have resolved all of the errors, but now when the form is submitted and I test it. Then Invalid reCaptcha Try Again come up
Copy linkTweet thisAlerts:
@rootDec 07.2018 — Make your form so you can submit it irrespective of the captcha, ascertain that the form works first before you go messing around with a recaptcha bot.

Then add in your security check.
Copy linkTweet thisAlerts:
@KyleJ144authorDec 07.2018 — @root yeah the form works without the recaptcha and how to I make my form so I can submit it irrespective of the captcha
Copy linkTweet thisAlerts:
@ginerjmDec 07.2018 — The code I gave you is a good thing to add to the very top of any 'main' script you write until you get it working error free. Then remove it or comment it out.
Copy linkTweet thisAlerts:
@KyleJ144authorDec 07.2018 — @ginerjm#1598756 Done that and their is no errors showing
Copy linkTweet thisAlerts:
@NogDogDec 07.2018 — Start by finding out what the response actually was, then go from there.
[code=php]
$responseArray = json_decode($response, true);
if( $responseArray["success"] !== true) {
die("<pre>Captcha failed; $response was:n".var_export($response, 1).
"n$responseArray was:n".var_export($responseArray, 1).'</pre>');
}
[/code]

You won't want that as your final code, but for now you can see what you got back in your validation request.

PS: Or, wait until you determine there's actually a need for captcha before you start implementing it. ?
Copy linkTweet thisAlerts:
@KyleJ144authorDec 07.2018 — @NogDog#1598758 Oh their is definitely a need because i am receiving inappropriate spam emails through the contact form. Also where do I place that code with my php file
Copy linkTweet thisAlerts:
@KyleJ144authorDec 07.2018 — @NogDog#1598758 The response was:

Captcha failed; $response was:

'




Not Found

Error 404



'

$responseArray was:

NULL
Copy linkTweet thisAlerts:
@NogDogDec 07.2018 — So now you need to figure out why this line didn't work:
[code=php]
$response = readURL("https://www.google.com/recaptcha/api/siteverify" . $secret . "&response=" . $verificationResponse . "");
[/code]

Hint: find out what $verificationResponse and $secret are set to, maybe adding them to the debug output in my prior reply? If they look okay, try plugging them into the URL you're trying to make and test it in your browser or something to see if you have any spelling mistakes, etc. Debugging is often just breaking things down into their component bits until you find the bit that's wrong (and hopefully learning how to convert it into D.R.Y., modularized code that's easier to debug ? ).
Copy linkTweet thisAlerts:
@KyleJ144authorDec 07.2018 — @NogDog#1598761 Okay and how do i debug that line
Copy linkTweet thisAlerts:
@rootDec 08.2018 — Well echo is a brilliant command that you can drop in where you want and echo the output.

so what you can do is make a page that just calls and echos that output. Then the rest of the page script is not interfering if that is the problem...
Copy linkTweet thisAlerts:
@KyleJ144authorDec 08.2018 — @root I have done the echo command and now I have have got this error

[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-12-08/1544269902-214365-error-2.png]

[code=php]
<?php
// error_reporting(E_WARNING);
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');

function readURL($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}

echo $secret = "site-key-here";
echo $verificationResponse = $_POST["g-recaptcha-response"];
if( empty($verificationResponse) ) die("Google did not POST the required g-recaptha-response");

$response = readURL("https://www.google.com/recaptcha/api/siteverify/ " . $secret . "&response=" . $verificationResponse . "");

$responseArray = json_decode($response, true);
if( $responseArray["success"] !== true) {
die("<pre>Captcha failed; $response was:n".var_export($response, 1).
"n$responseArray was:n".var_export($responseArray, 1).'</pre>');
}

/* Set e-mail recipient */
$myemail = "[email protected]";

/* Check all form inputs using check_input function */
$name = $_POST['name'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$dropdown2 = $_POST['dropdown2'];
$dropdown3 = $_POST['dropdown3'];
$subject = $_POST['subject'];
$message = $_POST['message'];

/* If e-mail is not valid show error message */
if (!preg_match("/([w-]+@[w-]+.[w-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */

$subject = "Contact Message from www.ncmaphia.co.uk";

$message = "

$name has sent you a message using your contact form:

Name: $name
Email: $email
Crew/Gang: $dropdown
Game: $dropdown2
Reason For Contacting Us: $dropdown3
Subject: $subject

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location:thank-you.html');
exit();
[/code]
Copy linkTweet thisAlerts:
@rootDec 08.2018 — Well you have your answer, you have malformed the request, so you need to consult google documents about it, you could be missing a step... who knows until you look in to what google says about it...
Copy linkTweet thisAlerts:
@KyleJ144authorDec 08.2018 — @root#1598790 Sorry for the late reply as I was checking google document and comparing what I had and everything is fine, but yet I am getting the malformed error. :/
Copy linkTweet thisAlerts:
@rootDec 09.2018 — Well if its malformed, its malformed, there is something that you may have in that string that is wrong, something that is causing the malformed error...

If you were to assume that it was true as in you had a proper response packet, I take it the script would try to process, if so, then your malformed string is malformed and seriously, if you are being told it is malformed, I would check it carefully.

I have this issue with my son over virtually everything that involves looking at something and taking note of it. Attention to detail is very lacking in people these days, trouble shooting with code is not an art, its a process, you have to go with the error you are given, eliminate it as the error then work backwards up the processes and see if its something in the script you have in the processing that has introduced characters that may not be visible but operate as terminators and that then truncates strings, that leas to malformed errors, the string looks fine on the page but... you have an errant null character in there, thats goodnite for your string as it is truncated at the null, this can be a NUL (null) generated through a introduced as a null character and not as a slash zero.

So check your code for typo's possible sources of a NUL or other terminator.
Copy linkTweet thisAlerts:
@KyleJ144authorDec 09.2018 — After Debugging the code and looking further into the situation. I decided to contact google, but they told me to contact my hosting provider and at the moment. I am currently waiting on a reply
Copy linkTweet thisAlerts:
@KyleJ144authorDec 11.2018 — Sorry for my last post. I didn't really think that it was a duplicate, but any.

I have got my recaptcha working, but at the moment it is useless, because when a user goes onto the contact page then they can click on send withough filling out the form or even ticking the recaptcha box.

My code is

[code=html]
<div class="contact">

<div class="container">
<h2>Contact US</h2>
<div class="contact-form">

<div class="col-md-8 contact-grid">
<form id="test" action="form-process.php" method="POST">
<input name="name" type="text" value="Name" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='name';}">
<br>
<input name="email" type="text" value="Email" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='email';}">
<br>
<select id="contact" name="dropdown" onchange="handleOption(this)">
<option value="Crew/Gang">Crew/Gang</option>
<option value="NC Maphia">NC Maphia</option>
<option value="deadeye/index">Dead Eye</option>
</select><br>
<select id="contact" name="dropdown2">
<option value="Game">Game</option>
<option value="Grand Theft Auto V">Grand Theft Auto V</option>
<option value="Red Dead Redemption II">Red Dead Redemption II</option>
</select><br>
<select id="contact" name="dropdown3">
<option value="Platform">Platform</option>
<option value="Xbox One">Xbox One</option>
<option value="PS4">PS4</option>
</select><br>
<select id="contact" name="dropdown4">
<option value="reason for contacting us">Reason for contacting us?</option>
<option value="Joining NC Maphia">Joining NC Maphia</option>
<option value="Recruitment Requirments">Recruitment Requirments</option>
<option value="Events">Events</option>
<option value="Report A Member">Report A Member</option>
<option value="Webmaster">Webmaster</option>
<option value="General Inquiry">General Inquiry</option>
</select><br>
<input name="subject" type="text" value="Subject" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='subject';}">
<br>
<textarea name="message" cols="77" rows="6" value=" " onfocus="this.value='';" onblur="if (this.value == '') {this.value = 'message';}">Message</textarea>
<div class="g-recaptcha" data-callback="recaptchaCallback" data-sitekey="6Ler9H8UAAAAAElg86pX58oN-G2a125GwNoBY9X4"></div>
<div class="send">
<input type="submit" disabled value="Send" >
</div>
</form>
</div>
<div class="col-md-4 contact-in">

<div class="address-more">
<h4>Address</h4>
<p>Maze Bank Tower,</p>
<p>Los Santos,</p>
<p>America </p>
</div>
<div class="address-more">
<h4>Chat With Us</h4>
<p>PSN: Kyle-The-Gunman9</p>
<p>PSN: NC_Maphia</p>
<p>Email:<a href="mailto:[email protected]"> info[at]ncmaphia.co.uk</a></p>
</div>

</div>
<div class="clearfix"> </div>
</div>
</div>
<script type="text/javascript">
function handleOption(elm) {
if(elm.value == "deadeye/index") { // Check if Dead Eye was selected
console.log("Dead Eye selected, redirecting");
window.location = elm.value + ".html";
}
}
function recaptchaCallback() {
$('submit').removeAttr('disabled');
}
else
alert("Verification Invalid")
};
</script>
[/code]


Any help would be appreciated and thanks in advance
Copy linkTweet thisAlerts:
@ginerjmDec 11.2018 — Without plowing thru your lengthy piece of code I'll ask this. What are you doing with the input when the send/submit button is clicked without the user doing any input? You should have at least one required field on the form so that you could prevent such an occurrence. So - when the script gets triggered, do a check for proper inputs and if none, send the form back.
Copy linkTweet thisAlerts:
@KyleJ144authorDec 11.2018 — @ginerjm#1598868 Like this

[code=html]
<input name="name" type="text" value="Name" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='name';}" required="">
[/code]
Copy linkTweet thisAlerts:
@ginerjmDec 11.2018 — Like this what? Was this a question?
Copy linkTweet thisAlerts:
@KyleJ144authorDec 11.2018 — I added Required at the end
Copy linkTweet thisAlerts:
@ginerjmDec 11.2018 — So it was a question!!

Looks to me like you added nothing. As in a null string to the 'required' attribute. Have you read up on this in an HTML reference? And what is that JS code supposed to accomplish? And - what is the purpose of putting a value into an input field? Isn't the USER supposed to do that? Perhaps you should think of using a label tag instead of forcing a value into the field that the user then has to deal with.
Copy linkTweet thisAlerts:
@KyleJ144authorDec 11.2018 — @ginerjm#1598873 The value has always been their and the contact form works, but it is no verifying the captcha or even checking that the form as been filled in, so at the moment. It allows the user to click on send without filling it out. I have also researched about the required attribute and on my research I found that it is supposed to be null :/
Copy linkTweet thisAlerts:
@ginerjmDec 11.2018 — So STOP putting a value in the field and then let your php script validate it!

Again - have you looked at any html reference??? To apply the 'required' attribute you simply add " required " to the tag. Even W3 schools has it right!
<i>
</i>&lt;form action="/action_page.php"&gt;
&lt;label&gt;Name: &lt;input type="text" name="username" required&gt;&lt;/label&gt;
&lt;input type="submit" value='Send'&gt;
&lt;/form&gt;


As for the capctha problem - I can't help you there but I think what I am trying to tell you fixes the other one.
Copy linkTweet thisAlerts:
@KyleJ144authorDec 11.2018 — @ginerjm#1598876 I have fully updates my code to the following:

[code=html]
<div class="contact">

<div class="container">
<h2>Contact US</h2>
<div class="contact-form">

<div class="col-md-8 contact-grid">
<form id="test" action="form-process.php" method="POST">
<label>Name<span> *</span></label>
<input name="name" type="text" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='name';}" required>
<br>
<label>Email<span> *</span></label>
<input name="email" type="text" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='email';}" required>
<br>
<label>Crew/Gang<span> *</span></label>
<select id="contact" name="dropdown" onchange="handleOption(this)" required>
<option value="NC Maphia">NC Maphia</option>
<option value="deadeye/index">Dead Eye</option>
</select><br>
<label>Game<span> *</span></label>
<select id="contact" name="dropdown2" required>
<option value="Grand Theft Auto V">Grand Theft Auto V</option>
<option value="Red Dead Redemption II">Red Dead Redemption II</option>
</select><br>
<label>Platform<span> *</span></label>
<select id="contact" name="dropdown3" required>
<option value="Xbox One">Xbox One</option>
<option value="PS4">PS4</option>
</select><br>
<label>Reason For Contacting Us<span> *</span></label>
<select id="contact" name="dropdown4" required>
<option value="Joining NC Maphia">Joining NC Maphia</option>
<option value="Recruitment Requirments">Recruitment Requirments</option>
<option value="Events">Events</option>
<option value="Report A Member">Report A Member</option>
<option value="Webmaster">Webmaster</option>
<option value="General Inquiry">General Inquiry</option>
</select><br>
<label>Subject</label>
<input name="subject" type="text" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='subject';}">
<br>
<label>Message<span> *</span></label>
<textarea name="message" cols="77" rows="6" value=" " onfocus="this.value='';" onblur="if (this.value == '') {this.value = 'message';}" required>Tell Us in more detail. Why you are contacting us. The more detail you can give us, then the more quicker, we will be able to assist you.</textarea>
<div class="send">
<input type="submit" value="Send" >
<?php
if(!empty($notification)) //This will display notification after submit
{
echo $notification;
}
?>
</div>
</form>
</div>
[/code]

[code=php]
<?php
if($_POST) //If the form is submitted
{
$notification=""; //Used for catching all your messages
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$dropdown = ($_GET['dropdown']) ?$_GET['dropdown'] : $_POST['dropdown'];
$dropdown2 = ($_GET['dropdown2']) ?$_GET['dropdown2'] : $_POST['dropdown2'];
$dropdown3 = ($_GET['dropdown3']) ?$_GET['dropdown3'] : $_POST['dropdown3'];
$dropdown4 = ($_GET['dropdown4']) ?$_GET['dropdown4'] : $_POST['dropdown4'];
$subject = ($_GET['subject']) ?$_GET['subject'] : $_POST['subject'];
$message = ($_GET['message']) ?$_GET['message'] : $_POST['message'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course,
//you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$dropdown) $errors[count($errors)] = 'Please enter which crew/gang you are attempting to contact.';
if (!$dropdown2) $errors[count($errors)] = 'Please enter which game you are referring too.';
if (!$dropdown3) $errors[count($errors)] = 'Please enter which platform you are currently playing on.';
if (!$dropdown4) $errors[count($errors)] = 'We need too know the reason for your message.';
if (!$message) $errors[count($errors)] = 'We need more detail on why you are contacting us.';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - change this to your name and email
$to = '[email protected]';

//sender
$from = $email;

//subject and the html message
$subject = 'Hello from ' . $name;

$message = '
<!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></head>
<body>
<p>You have recieved a message from $name using your contact form at www.ncmaphia.co.uk
<table>
<tr><td>Name: </td><td>' . $name . '</td></tr>
<tr><td>Email: </td><td>' . $email . '</td></tr>
<tr><td>Crew/Gang: </td><td>' . $dropdown . '</td></tr>
<tr><td>Game: </td><td>' . $dropdown2 . '</td></tr>
<tr><td>Platform: </td><td>' . $dropdown3 . '</td></tr>
<tr><td>Reason For Contacting Us: </td><td>' . $dropdown4 . '</td></tr>
<tr><td>Subject: </td><td>' . $subject . '</td></tr>
<tr><td>Message: </td><td>' . $message . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = sendmail($to, $subject, $message, $from);

//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else $notification.= 'Sorry, unexpected error. Please try again later';

//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
$notification.= $result;

}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
$notification.= '<a href="contact.html">Back</a>';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "rn";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";
$headers .= 'From: ' . $from . "rn";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
} //First If loop
?>
[/code]

And i am getting a bunch of errors. I have supplied 2 screenshots of the errors.

[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-12-11/1544565257-798614-screenshot-2.png]

[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-12-11/1544565257-884052-screenshot-1.png]

And i don't know why I am receiving them as I have checked the code more than once and it looks okay to me or have I missed something?
Copy linkTweet thisAlerts:
@rootDec 12.2018 — @KyleJ144#1598869 RE your &lt;input name="name" type="text" value="Name"
onfocus="this.value='';" onblur="if (this.value == '') {this.value ='name';}" required=""&gt;

if you use HTML5 input then you &lt;input name="personname" type="text" value="" placeholder="Name" required&gt;

ALSO it is NEVER advisable to call the name of something or a property by a reserved word or a key word like name, value, type, and so on...

if you keep in mind that the input will default to a plain text input field if the attributes are not supported because there are rare cases where people use really old systems.

However, you should look at HTML5 as its been around 10 years now, its only safari that is lagging, support for most fields are there, however those that don't will need the various scripts and checks done with javascript as a precaution.
Copy linkTweet thisAlerts:
@KyleJ144authorDec 12.2018 — @root#1598893 I have changed my code as followed:

[code=html]
<div class="contact">

<div class="container">
<h2>Contact US</h2>
<div class="contact-form">

<div class="col-md-8 contact-grid">
<form id="test" action="form-process.php" method="POST">
<input name="personname" type="text" placeholder="Name" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='name';}" required>
<br>
<input name="email" type="text" placeholder="Email" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='email';}" required>
<br>
<select id="contact" name="dropdown" placeholder="Crew/Gang" onchange="handleOption(this)" required>
<option value="NC Maphia">NC Maphia</option>
<option value="deadeye/index">Dead Eye</option>
</select><br>
<select id="contact" name="dropdown2" placeholder="Game" required>
<option value="Grand Theft Auto V">Grand Theft Auto V</option>
<option value="Red Dead Redemption II">Red Dead Redemption II</option>
</select><br>
<select id="contact" name="dropdown3" Placeholder="Platform" required>
<option value="Xbox One">Xbox One</option>
<option value="PS4">PS4</option>
</select><br>
<select id="contact" name="dropdown4" placeholder="Reason For Contacting Us" required>
<option value="Joining NC Maphia">Joining NC Maphia</option>
<option value="Recruitment Requirments">Recruitment Requirments</option>
<option value="Events">Events</option>
<option value="Report A Member">Report A Member</option>
<option value="Webmaster">Webmaster</option>
<option value="General Inquiry">General Inquiry</option>
</select><br>
<input name="subject" type="text" placeholder="Subject" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='subject';}">
<br>
<textarea name="message" cols="77" rows="6" placeholder="Message" onfocus="this.value='';" onblur="if (this.value == '') {this.value = 'message';}" required></textarea>
<div class="send">
<input type="submit" value="Send" >
<?php
if(!empty($notification)) //This will display notification after submit
{
echo $notification;
}
?>
</div>
</form>
</div>
[/code]

[code=php]
<?php
if($_POST) //If the form is submitted
{
$notification=""; //Used for catching all your messages
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$personname = ($_GET['personname']) ? $_GET['personname'] : $_POST['personname'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$dropdown = ($_GET['dropdown']) ?$_GET['dropdown'] : $_POST['dropdown'];
$dropdown2 = ($_GET['dropdown2']) ?$_GET['dropdown2'] : $_POST['dropdown2'];
$dropdown3 = ($_GET['dropdown3']) ?$_GET['dropdown3'] : $_POST['dropdown3'];
$dropdown4 = ($_GET['dropdown4']) ?$_GET['dropdown4'] : $_POST['dropdown4'];
$subject = ($_GET['subject']) ?$_GET['subject'] : $_POST['subject'];
$message = ($_GET['message']) ?$_GET['message'] : $_POST['message'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course,
//you should validate the email
if (!$personname) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$dropdown) $errors[count($errors)] = 'Please enter which crew/gang you are attempting to contact.';
if (!$dropdown2) $errors[count($errors)] = 'Please enter which game you are referring too.';
if (!$dropdown3) $errors[count($errors)] = 'Please enter which platform you are currently playing on.';
if (!$dropdown4) $errors[count($errors)] = 'We need too know the reason for your message.';
if (!$message) $errors[count($errors)] = 'We need more detail on why you are contacting us.';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - change this to your name and email
$to = '[email protected]';

//sender
$from = $email;

//subject and the html message
$subject = 'Hello from ' . $name;

$message = '
<!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></head>
<body>
<p>You have recieved a message from $name using your contact form at www.ncmaphia.co.uk
<table>
<tr><td>Name: </td><td>' . $name . '</td></tr>
<tr><td>Email: </td><td>' . $email . '</td></tr>
<tr><td>Crew/Gang: </td><td>' . $dropdown . '</td></tr>
<tr><td>Game: </td><td>' . $dropdown2 . '</td></tr>
<tr><td>Platform: </td><td>' . $dropdown3 . '</td></tr>
<tr><td>Reason For Contacting Us: </td><td>' . $dropdown4 . '</td></tr>
<tr><td>Subject: </td><td>' . $subject . '</td></tr>
<tr><td>Message: </td><td>' . $message . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = sendmail($to, $subject, $message, $from);

//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else $notification.= 'Sorry, unexpected error. Please try again later';

//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
$notification.= $result;

}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
$notification.= '<a href="contact.html">Back</a>';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "rn";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";
$headers .= 'From: ' . $from . "rn";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
} //First If loop
?>
[/code]

And I am receiving the same errors, also the placeholders are not showing up for the dropdowns.
Copy linkTweet thisAlerts:
@rootDec 12.2018 — What I am suggesting here is that you use HTML5 inputs, you use PHP to check browser and if support is needed, you can include a script library to assist with tat field type.

You then once you have a basic HTML5 form submitting to the server, you include the things like the recaptcha.

Now, if adding recaptcha is giving you errors, we can;t help unless someone here has had same problem and can assist, it is generally code issues, not software or library or their implementation issues beyond us guiding you to its proper use, if that fails, it really is in the hands of the vendor and that community that built the tool.

SO I take it this is the situation, you take it out, the form submits fine, you put the library in, it fails and gives errors.

If thats the case with errors, then you need to speak to whoever it is that is reporting an error.

IF google are saying something is malformed, then it is malformed and you need to speak to them about it.

If PHP is saying something, then you should read the error message, it tells you what is wrong and from that you work on how to fix the problem by looking up the error or the function that raises the error.

If SERVER is giving error and is a result of adding in recaptcha... see previous comment about speaking to that community.

Unless someone here has had the same issue, the general assistance here is to guid people with information to help, brief examples, point out errors, etc, improvements..!!!
Copy linkTweet thisAlerts:
@KyleJ144authorDec 13.2018 — @root I have now replaced this line [code=php]if($_POST) //If the form is submitted[/code] with this [code=php]if(isset)($_POST["submit"])) //If the form is submitted[/code] and now i am receiving the following:

This page isn’t working demo.ncmaphia.co.uk is currently unable to handle this request.

HTTP ERROR 500
Copy linkTweet thisAlerts:
@ginerjmDec 13.2018 — 1 - stop using $_GET since your form is set to use POST!!!!

2 - I told you how the REQUIRED attribute is suppose to be typed but you refused to change. Why? It's not "required=" !!!!

3 - Look up in the MANUAL under the "isset" function to see how it is supposed to be typed. You will perhaps even enjoy using that manual!!!

http://php.net/manual/en/function.isset.php

When you get an error you may want to include that line in your post so that we can see what you are seeing and what the php interpreter is seeing. Perhaps even a line or two above and below.
Copy linkTweet thisAlerts:
@KyleJ144authorDec 14.2018 — @ginerjm#1598967 & @root#1598916 Got the form working as I added required and removed $_get, but now as I have added the recaptcha back, then the form has stopped working again
Copy linkTweet thisAlerts:
@ginerjmDec 14.2018 — you removed the $_GET!!!

If as you say the script is working without the captcha, then show us the code that tries to implement that feature (AND ONLY THAT CODE!) so that we can see what you have done.

BTW - when you say it doesn't work, what errors are you seeing, if you have php error checking enabled that is.
Copy linkTweet thisAlerts:
@rootDec 14.2018 — @KyleJ144#1598950 You obviously don't understand how to properly use the isset function.

You put if(isset)($_POST["submit"])) //If the form is submitted when it is if( isset( $_POST["submit"]) ) //If the form is submitted and as pointed out many times to used CHECK FOR TYPO'S and READ THE ERROR MESSAGES and it tells you more often than not what the problem is.
Copy linkTweet thisAlerts:
@KyleJ144authorDec 14.2018 — @ginerjm#1599007 The form was not working with the $_GET function and here is the updated code below:

HTML

[code=html]
<div class="contact">

<div class="container">
<h2>Contact US</h2>
<div class="contact-form">

<div class="col-md-8 contact-grid">
<form id="test" action="form-process.php" method="POST">
<input name="name" type="text" placeholder="Name" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='name';}" required>
<br>
<input name="email" type="text" placeholder="Email" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='email';}" required>
<br>
<select id="contact" name="dropdown" onchange="handleOption(this)" required>
<option value="" disabled selected>Crew/Gang</option>
<option value="NC Maphia">NC Maphia</option>
<option value="deadeye/index">Dead Eye</option>
</select><br>
<select id="contact" name="dropdown2" required>
<option value="" disabled selected>Game</option>
<option value="Grand Theft Auto V">Grand Theft Auto V</option>
<option value="Red Dead Redemption II">Red Dead Redemption II</option>
</select><br>
<select id="contact" name="dropdown3" required>
<option value="" disabled selected>Platform</option>
<option value="Xbox One">Xbox One</option>
<option value="PS4">PS4</option>
</select><br>
<select id="contact" name="dropdown4" required>
<option value="" disabled selected>Reason For Contacting Us</option>
<option value="Joining NC Maphia">Joining NC Maphia</option>
<option value="Recruitment Requirments">Recruitment Requirments</option>
<option value="Events">Events</option>
<option value="Report A Member">Report A Member</option>
<option value="Webmaster">Webmaster</option>
<option value="General Inquiry">General Inquiry</option>
</select><br>
<input name="subject" type="text" placeholder="Subject" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='subject';}">
<br>
<textarea name="message" cols="77" rows="6" placeholder="Message" onfocus="this.value='';" onblur="if (this.value == '') {this.value = 'message';}" required></textarea>
<div class="g-recaptcha" data-sitekey="6Ler9H8UAAAAAElg86pX58oN-G2a125GwNoBY9X4"></div>
<div class="send">
<input type="submit" name="submit" value="Send" >
</div>
</form>
</div>
[/code]


PHP
[code=php]
<!--A Design by Kyle Logan
Author: Kyle Logan
Author URL: http://www.kylejamelogan.co.uk
License: Creative Commons Attribution 3.0 Unported
License URL: http://www.kylejameslogan.co.uk
-->
<!--
************************************************************
* Copyright Policy *
************************************************************
* Copyright © 2017 Kyle Logan. All Rights Reserved *
************************************************************
* This is the copyright policy for the two parties *
* involved and they include the license holder and *
* the client. All of the documents and everything that *
* is joint to this website must not be copyrighted *
* under the copyright laws of the U.K and U.S.A as it *
* can result in criminal proceedings being held against *
* you. If you also get this code then you must not *
* remove the developed by Kyle Logan within the footer *
* if this does occur then you will frace criminal *
* proceedings and/or the website may be destroyed. *
************************************************************
-->
<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');

if(isset($_POST["submit"])) //If the form is submitted
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
//your site secret key
$secret = 'My-Key-Here';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success):
{
$notification=""; //Used for catching all your messages
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$errors = [];

$name = $_POST['name'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$dropdown2 = $_POST['dropdown2'];
$dropdown3 = $_POST['dropdown3'];
$dropdown4 = $_POST['dropdown4'];
$subject = $_POST['subject'];
$message = $_POST['message'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course,
//you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$dropdown) $errors[count($errors)] = 'Please enter which crew/gang you are attempting to contact.';
if (!$dropdown2) $errors[count($errors)] = 'Please enter which game you are referring too.';
if (!$dropdown3) $errors[count($errors)] = 'Please enter which platform you are currentlyplaying on.';
if (!$dropdown4) $errors[count($errors)] = 'We need too know the reason for your message.';
if (!$message) $errors[count($errors)] = 'We need more detail on why you are contacting us.';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - change this to your name and email
$to = '[email protected]';

//sender
$from = $email;

//subject and the html message
$subject = 'Hello from ' . $name;

$message = '
<!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></head>
<body>
<p>You have recieved a message from $name using your contact form at www.example.co.uk
<table>
<tr><td>Name: </td><td>' . $name . '</td></tr>
<tr><td>Email: </td><td>' . $email . '</td></tr>
<tr><td>Crew/Gang: </td><td>' . $dropdown . '</td></tr>
<tr><td>Game: </td><td>' . $dropdown2 . '</td></tr>
<tr><td>Platform: </td><td>' . $dropdown3 . '</td></tr>
<tr><td>Reason For Contacting Us: </td><td>' . $dropdown4 . '</td></tr>
<tr><td>Subject: </td><td>' . $subject . '</td></tr>
<tr><td>Message: </td><td>' . $message . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = mail($to, $subject, $message, $from);

//if POST was used, display the message straight away
if ($_POST) {
if ($result) header('location: thank-you.html', true, 303);
else:
$errMsg = 'Robot verification failed, please try again.';
endif;
else:
$errMsg = 'Please click on the reCAPTCHA box.';
endif;
else:
$errMsg = '';
$succMsg = '';
endif;

//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
$notification.= $result;

}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
$notification.= '<a href="contact.html">Back</a>';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "rn";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";
$headers .= 'From: ' . $from . "rn";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
} //First If loop
?>
[/code]


And the error I am receiving is the following:


This page isn’t working www.example.co.uk is currently unable to handle this request.

HTTP ERROR 500


Also PHP error reporting is on
Copy linkTweet thisAlerts:
@rootDec 14.2018 — I think you are overoptimistic in being able to just cobble HTML in a format and email it, takes a lot more than just putting HTML together, you have to compose a message both in plain text and in HTML and have boundaries and extra bits in the headers that allow HTML and ensures that your able to read the email.

Try reading this... https://css-tricks.com/sending-nice-html-email-with-php/
Copy linkTweet thisAlerts:
@KyleJ144authorDec 14.2018 — @root#1599017 The form works fine, but when i add the recapture then i receive the error, so it is the capture that is the problem
Copy linkTweet thisAlerts:
@KyleJ144authorDec 15.2018 — @root#1599017 & @ginerjm#1599007 I got it fully working. I had no option,l but to use javascript for the recapture
Copy linkTweet thisAlerts:
@rootDec 15.2018 — And if you used HTML5 inputs when supported (which is most browsers) then you would reduce the need for recaptcha as html5 inputs are more robust, if your input has pattern set, that will only allow inputs matching that regular expression.

Very powerful step up in HTML and web forms.

Those browsers that lack specific support for fields like Safari and their date picker, you would need to detect safari browser, use PHP to write in support scripting for that field to work around the problem.

Then people will be saying, but yeah, what if they are spoofing their browser type... SO, thats their issue, if they want to dick around with their browsers and expect them to function when people go to the effort of supporting their browser, stuff them, why bother breaking in to a sweat over 0.00000001% of internet users?

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;


only needs to be

&lt;!DOCTYPE html&gt;
&lt;html&gt;


and this is dangerous $name = $_POST['name'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$dropdown2 = $_POST['dropdown2'];
$dropdown3 = $_POST['dropdown3'];
$dropdown4 = $_POST['dropdown4'];
$subject = $_POST['subject'];
$message = $_POST['message'];
no filtering with filter_var to validate the input.

$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?
secret='.$secret.'&amp;response='.$_POST['g-recaptcha-response']);

You should use CURL and there are a few tutorials as well as help threads over the years on the subject on here and the internet... If you don't use the appropriate functions and calls, your not going to get the result you are looking for.
×

Success!

Help @KyleJ144 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...