PHP form not displaying feedback - php

I'm making a process form in PHP and I'm trying to display the feedback but it's not wanting to show. The code that I have is:
<html>
<head>
<style type="text/css">
.error{color: #FF0000;}
</style>
</head>
<body>
<h1>Customer Feedback</h1>
<p1>Please tell us what you think</p1><br><br>
<?PHP
$name = trim($_POST[fullname]);
$email = trim($_POST[email]);
$text = trim($_POST[feedback]);
?>
<form method='POST' action='<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>' >
<p1>Your name:</p1><br>
<input type="text" name="fullname" value="<?php echo $fullname; ?>" required><br><br>
<p1>Your email address:</p1><br>
<input type="text" name="email" value="<?php echo $email; ?>" required><br><br>
<p1>Your feedback:</p1><br>
<textarea rows="5" cols="50" name="feedback"><?php echo nl2br($text);?></textarea>
<textarea><?php echo $text;?></textarea><br><br>
<input type="submit" Value="Send Feedback"><br><br>
</form>
<?php
if(isset($_POST[fullname]) && $_POST[fullname] != "" && !empty($_POST[fullname])) {
echo "Hi " . $name . ".<br>";
}
else{
echo "Please enter a name....";
}
if(isset($_POST[email]) && $_POST[email] != "" && !empty($_POST[email]) {
echo "Your email is " . $email . ".<br>";
}
else{
echo "Please enter a email address.";
}
if(isset($_POST[feedback]) && $_POST[feedback] != "") {
echo "Your feedback is:" . $feedback . "<br>";
}
else{
echo "No feedback.";
}
?>
</body>
</html>
When I run the page, it shows the name, email and 'Your feedback is: ' but not the feedback that was entered into the textarea.
EDIT
I want to use the nl2br() function round the text box.

Change following line to:
echo "Your feedback is:" . $text . "<br>";
Because you've never assign a value to $feedback variable.

Your code had too many errors I have corrected it, please fin the working code below:
<html>
<head>
<style type="text/css">
.error{color: #FF0000;}
</style>
</head>
<body>
<h1>Customer Feedback</h1>
<p1>Please tell us what you think</p1><br><br>
<?PHP
$name = trim($_POST[fullname]);
$email = trim($_POST[email]);
$text = trim($_POST[feedback]);
?>
<form method='POST' action='<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>' >
<p1>Your name:</p1><br>
<input type="text" name="fullname" value="<?php echo $fullname; ?>" required><br><br>
<p1>Your email address:</p1><br>
<input type="text" name="email" value="<?php echo $email; ?>" required><br><br>
<p1>Your feedback:</p1><br>
<textarea rows="5" cols="50" name="feedback"><?php echo nl2br($text);?></textarea>
<textarea><?php echo $text;?></textarea><br><br>
<input type="submit" Value="Send Feedback"><br><br>
</form>
<?php
if(isset($_POST['fullname']) && $_POST['fullname'] != "" && !empty($_POST['fullname'])) {
echo "Hi " . $name . ".<br>";
}
else{
echo "Please enter a name....";
}
if($_POST['email'] && $_POST['email'] != "" && $_POST['email']) {
echo "Your email is " . $email . ".<br>";
}
else{
echo "Please enter a email address.";
}
if(isset($_POST['feedback']) && $_POST['feedback'] != "") {
echo "Your feedback is:" . $_POST['feedback'] . "<br>";
}
else{
echo "No feedback.";
}
?>
</body>
</html>

Related

How to not redirect and display the error if the user enter invalid input

I'm new to PHP and I just want to make some form like a basic form. But I have trouble in the values entered by the user. For example if they enter a empty field it should not redirect or if in the email they didn't enter a correct format the form should not redirect. But in my case it always redirect even though the input is invalid. Can you help me out on how can I stop redirecting to another page if the value entered by the user is invalid?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FG4</title>
</head>
<style>
.error {color: red}
</style>
<body>
<?php
$fname = $lname = $fgender = $mail = $dob = $address = "";
$fnameErr = $lnameErr = $genderErr = $mailErr = $dobErr = $addressErr = "";
if ($_SERVER["REQUEST_METHOD"] == "GET") {
if (empty($_GET["fname"])) {
$fnameErr = "Please enter your first name.";
} else {
$fname = input($_GET["fname"]);
// check if name only contains letters and space
if (!preg_match("/^[a-zA-Z-' ]*$/",$fname)) {
$fnameErr = "Please enter a valid name";
}
}
if (empty($_GET["lname"])) {
$lnameErr = "Please enter your last name.";
} else {
$lname = input($_GET["lname"]);
// check if name only contains letters and space
if (!preg_match("/^[a-zA-Z-' ]*$/",$lname)) {
$lnameErr = "Please enter a valid name";
}
}
if (empty($_GET["gender"])) {
$genderErr = "Please select a gender.";
} else{
$gender = input($_GET["gender"]);
}
if (empty($_GET["mail"])) {
$mailErr = "Please enter your email.";
} else {
$mail = input($_GET["mail"]);
// check if email contain gmail.com or yahoo.com
if (!preg_match("/#gmail.com|#yahoo.com/", $mail)) {
$mailErr = "Please enter a valid email.";
}
}
if (empty($_GET["dob"])) {
$dobErr = "Please select your date of birth.";
} else{
$lname = input($_GET["lname"]);
}
if (empty($_GET["address"])) {
$addressErr = "Please enter your address.";
} else {
$address = input($_GET["address"]);
// check if address contain the following characters
if (!preg_match(" /#|[0-9]|[a-z]|[A-Z]/ ",$address)) {
$address = "Please enter a valid address";
}
}
}
function input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="get" action="trial.php">
First Name: <input type="text" name="fname">
<span class="error">* <?php echo $fnameErr;?></span>
<br><br>
Larst Name: <input type="text" name="lname">
<span class="error">* <?php echo $lnameErr;?></span>
<br><br>
Gender: <input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
Email: <input type="text" name="mail">
<span class="error">* <?php echo $mailErr;?></span>
<br><br>
Date of Birth: <input type="date" name="dob">
<span class="error">* <?php echo $dobErr;?></span>
<br><br>
Address: <br><textarea type="text" name="address" rows="5" cols="40"></textarea>
<span class="error"><?php echo $addressErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
Here is the other code where it just print the values entered by the user
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET") {
// collect value of input field
$fname = $_GET['fname'];
$lname = $_GET['lname'];
$gender = $_GET['gender'];
$mail = $_GET['mail'];
$dob = $_GET['dob'];
$address = $_GET['address'];
echo "<h2> Final Output:</h2>";
echo "First Name :$fname";
echo "<br>";
echo "Last Name :$lname";
echo "<br>";
echo "Gender :$gender";
echo "<br>";
echo "Email :$mail";
echo "<br>";
echo "Date of Birth :$dob";
echo "<br>";
echo "Address :$address";
}
?>
</body>
</html>
There are many ways to do what you want.
One of them is to use a hidden form and submit it only if there is no error found after the validation.
Hence the amended code (based on your original code) will be:
trial.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FG4</title>
</head>
<style>
.error {color: red}
</style>
<body>
<?php
$fname = $lname = $fgender = $mail = $dob = $address = "";
$fnameErr = $lnameErr = $genderErr = $mailErr = $dobErr = $addressErr = "";
if ($_SERVER["REQUEST_METHOD"] == "GET") {
if (empty($_GET["fname"])) {
$fnameErr = "Please enter your first name.";
} else {
$fname = input($_GET["fname"]);
// check if name only contains letters and space
if (!preg_match("/^[a-zA-Z-' ]*$/",$fname)) {
$fnameErr = "Please enter a valid name";
}
}
if (empty($_GET["lname"])) {
$lnameErr = "Please enter your last name.";
} else {
$lname = input($_GET["lname"]);
// check if name only contains letters and space
if (!preg_match("/^[a-zA-Z-' ]*$/",$lname)) {
$lnameErr = "Please enter a valid name";
}
}
if (empty($_GET["gender"])) {
$genderErr = "Please select a gender.";
} else{
$gender = input($_GET["gender"]);
}
if (empty($_GET["mail"])) {
$mailErr = "Please enter your email.";
} else {
$mail = input($_GET["mail"]);
// check if email contain gmail.com or yahoo.com
if (!preg_match("/#gmail.com|#yahoo.com/", $mail)) {
$mailErr = "Please enter a valid email (only #gmail.com or #yahoo.com).";
}
}
if (empty($_GET["dob"])) {
$dobErr = "Please select your date of birth.";
} else{
$lname = input($_GET["lname"]);
}
if (empty($_GET["address"])) {
$addressErr = "Please enter your address.";
} else {
$address = input($_GET["address"]);
// check if address contain the following characters
if (!preg_match(" /#|[0-9]|[a-z]|[A-Z]/ ",$address)) {
$address = "Please enter a valid address";
}
}
}
function input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form id="form_id" method="get" action=#>
First Name: <input type="text" name="fname" value="<?php echo $_GET["fname"];?>">
<span class="error">* <?php echo $fnameErr;?></span>
<br><br>
Larst Name: <input type="text" name="lname" value="<?php echo $_GET["lname"];?>">
<span class="error">* <?php echo $lnameErr;?></span>
<br><br>
Gender: <input type="radio" name="gender" value="male"
<?php if ($_GET["gender"]=="male") { echo " checked ";} ?>
> Male
<input type="radio" name="gender" value="female"
<?php if ($_GET["gender"]=="female") { echo " checked ";} ?>
> Female
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
Email: <input type="text" name="mail" value="<?php echo $_GET["mail"];?>">
<span class="error">* <?php echo $mailErr;?></span>
<br><br>
Date of Birth: <input type="date" name="dob" value="<?php echo $_GET["dob"];?>">
<span class="error">* <?php echo $dobErr;?></span>
<br><br>
Address: <br><textarea type="text" name="address" rows="5" cols="40"><?php echo $_GET["address"];?></textarea>
<span class="error"><?php echo $addressErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if ($fnameErr=="" && $lnameErr=="" && $genderErr=="" && $mailErr=="" && $dobErr=="" && $addressErr=="") { ?>
<form id="form_id2" method=GET action="trial2.php">
<input type=hidden name="fname" value="<?php echo $_GET["fname"];?>">
<input type=hidden name="lname" value="<?php echo $_GET["lname"];?>">
<input type=hidden name="gender" value="<?php echo $_GET["gender"];?>">
<input type=hidden name="mail" value="<?php echo $_GET["mail"];?>">
<input type=hidden name="dob" value="<?php echo $_GET["dob"];?>">
<textarea name=address style="display:none;"><?php echo $_GET["address"];?></textarea>
</form>
<script>
document.getElementById("form_id2").submit();
</script>
<?php } ?>
trial2.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET") {
// collect value of input field
$fname = $_GET['fname'];
$lname = $_GET['lname'];
$gender = $_GET['gender'];
$mail = $_GET['mail'];
$dob = $_GET['dob'];
$address = $_GET['address'];
echo "<h2> Final Output:</h2>";
echo "First Name :$fname";
echo "<br>";
echo "Last Name :$lname";
echo "<br>";
echo "Gender :$gender";
echo "<br>";
echo "Email :$mail";
echo "<br>";
echo "Date of Birth :$dob";
echo "<br>";
echo "Address :$address";
}
?>
</body>
</html>

php form 2 step confirmation

i try to challenge my self but i stuck(
I try to create a php form with 2 steps confirmation:
When the user fill up the form and hit Submit, it checks all the conditions(name, pass etc.). If everything ok automatically redirecting the user.
After redirecting (to the same page) the user can check all the details again.
If they ok, hit again the submit button which redirects to the final page.
I stuck on the 2nd phase...how to redirect to the final page?
I'm very beginner so i'm curios what could be done better or any advise.
<?php
// the php code
session_start();
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// setting up the variables
$title = $_POST['title'];
$fName = trim(filter_input(INPUT_POST,'fName', FILTER_SANITIZE_STRING));
$lName = trim(filter_input(INPUT_POST,'lName',FILTER_SANITIZE_STRING));
$age = intval($_POST['age']);
$_SESSION['title'] = $title;
$_SESSION['fName'] = $fName;
$_SESSION['lName'] = $lName;
$_SESSION['age'] = $age;
//checking for possible errors
if ( $fName == "" || strlen($fName) <= 2 ) {
$errorMsg1 = "<span>Provide your First name!(minimum 3 characters)</span>";
$status = false;
}
else if ( $lName == "" || strlen($lName) <= 2 ) {
$errorMsg2 = "<span>Provide your Last name!(minimum 3 characters)</span>";
$status = false;
}
else if ( $age < 18 ) {
$errorMsg3 = "<span>You must be 18 or above!</span>";
$status = false;
}
else { $status = true; }
// redirecting to done page
if ($status) {
header("Location:TEST ZONE.php?status=awaiting");
}
}
?>
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<div id="wrapper">
<?php
if ( isset($_GET['status']) && $_GET['status'] == "awaiting" ) {
echo "<form>"
. "Check your Details!<br>"
. $_SESSION['title'] . "<br>"
. $_SESSION['fName'] . "<br>"
. $_SESSION['lName'] . "<br>"
. $_SESSION['age'] . "<br>"
// **NOW WHEN I'M in the awaiting phase, i don't know what to do(**
. "<input type='submit' name='submit'/>";
echo "</form>";
}
else { ?>
<form action="TEST ZONE.php" method="post">
<h3>Register Form </h3>
<label for="title">Title </label>
<select name="title">
<option name="mr">Mr</option>
<option name="ms">Ms</option>
</select><br><br><br>
<label for="fName">First Name</label><br>
<input type="text" name="fName" id="fName" value="<?php if (isset($fName)) { echo $fName; } ?>"><br><?php
if (isset( $errorMsg1 )) {
echo $errorMsg1;
}
?><br><br>
<label for="lName">Last Name</label><br>
<input type="text" name="lName" id="lName" value="<?php if (isset($lName)) { echo $lName; } ?>"><br><?php
if (isset( $errorMsg2 )) {
echo $errorMsg2;
}
?><br><br>
<label for="age">Age</label><br>
<input type="text" name="age" id="age" value="<?php if (isset($age)) { echo $age; }?>"><br><?php
if (isset($errorMsg3)){
echo $errorMsg3;
} ?><br><br>
<input type="submit" value="Submit"><input type="reset">
</form> <?php } ?>
</div>
</body>
</html>
Add action in your form to redirect final page.
You already have all values in session so you can access it in final page also
<?php
// the php code
session_start();
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// setting up the variables
$title = $_POST['title'];
$fName = trim(filter_input(INPUT_POST,'fName', FILTER_SANITIZE_STRING));
$lName = trim(filter_input(INPUT_POST,'lName',FILTER_SANITIZE_STRING));
$age = intval($_POST['age']);
$_SESSION['title'] = $title;
$_SESSION['fName'] = $fName;
$_SESSION['lName'] = $lName;
$_SESSION['age'] = $age;
//checking for possible errors
if ( $fName == "" || strlen($fName) <= 2 ) {
$errorMsg1 = "<span>Provide your First name!(minimum 3 characters)</span>";
$status = false;
}
else if ( $lName == "" || strlen($lName) <= 2 ) {
$errorMsg2 = "<span>Provide your Last name!(minimum 3 characters)</span>";
$status = false;
}
else if ( $age < 18 ) {
$errorMsg3 = "<span>You must be 18 or above!</span>";
$status = false;
}
else { $status = true; }
// redirecting to done page
if ($status) {
header("Location:TEST ZONE.php?status=awaiting");
}
}
?>
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<div id="wrapper">
<?php
if ( isset($_GET['status']) && $_GET['status'] == "awaiting" ) {
echo "<form action='final_page.php'>"
. "Check your Details!<br>"
. $_SESSION['title'] . "<br>"
. $_SESSION['fName'] . "<br>"
. $_SESSION['lName'] . "<br>"
. $_SESSION['age'] . "<br>"
// **NOW WHEN I'M in the awaiting phase, i don't know what to do(**
. "<input type='submit' name='submit'/>";
echo "</form>";
}
else { ?>
<form action="TEST ZONE.php" method="post">
<h3>Register Form </h3>
<label for="title">Title </label>
<select name="title">
<option name="mr">Mr</option>
<option name="ms">Ms</option>
</select><br><br><br>
<label for="fName">First Name</label><br>
<input type="text" name="fName" id="fName" value="<?php if (isset($fName)) { echo $fName; } ?>"><br><?php
if (isset( $errorMsg1 )) {
echo $errorMsg1;
}
?><br><br>
<label for="lName">Last Name</label><br>
<input type="text" name="lName" id="lName" value="<?php if (isset($lName)) { echo $lName; } ?>"><br><?php
if (isset( $errorMsg2 )) {
echo $errorMsg2;
}
?><br><br>
<label for="age">Age</label><br>
<input type="text" name="age" id="age" value="<?php if (isset($age)) { echo $age; }?>"><br><?php
if (isset($errorMsg3)){
echo $errorMsg3;
} ?><br><br>
<input type="submit" value="Submit"><input type="reset">
</form> <?php } ?>
</div>
final_page.php
<?php
session_start();
$title = $_SESSION['title'];
$fName = $_SESSION['fName'];
$lName = $_SESSION['lName'];
$age = $_SESSION['age'];
?>

Problems inserting data into MYSQL database using PHP

I am still a beginner at PHP/MYSQL and I am having difficulties inserting data into my MYSQL database. (I've originally tried using my localhost database but once i moved to an online server, everything seems to stop working.)
Right now, as soon as i submit the data from my index.php page.. it only refreshes the page and doesn't add any data.
However, when I go to submit.php, everything works fine and it adds an empty set of data to my results.php.
My codes are as follows. Any help will be greatly appreciated. Thank you!
Index.php
<html>
<head>
<title>POST variables</title>
<link rel="stylesheet" type="text/css" href="css/style.css" media="all">
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'anas12_test', 'a1b2c3d4', 'anas12_test');
if (!$con) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo '<div class="container">
<form id="profiles">
<div class="header">
<h3>Hello there!</h3>
<p>We want to know more about you! Share a few interesting details about yourself using the form below!</p>
</div>
<div class="sep"></div>
<div class="inputs">
<form action="submit.php" method="post">
<input id="name" name="name" placeholder="Full Name" required="" autofocus="" autocomplete="on" type="text">
<input id="email" name="email" placeholder="Email Address" required="" autofocus="" autocomplete="on" type="text">
<input id="colour" name="colour" placeholder="Favourite Colour" required="" autofocus="" autocomplete="on" type="text">
<input id="music" name="music" placeholder="Favourite Song" required="" autofocus="" autocomplete="on" type="text">
<input id="superpower" name="superpower" placeholder="If you had a superhero ability, what would it be?" required="" autofocus="" autocomplete="on" type="text">
<button id="submit" type="submit"name="submit" value="added">Submit!</button>
</form> </div>
</div>';
?>
</body>
</html>
Submit.php
$con = mysqli_connect('localhost', 'anas12_test', 'a1b2c3d4', 'anas12_test');
if(isset($_POST["name"])){
$name = $_POST["name"];
} else {
$name = "";
}
if(isset($_POST["email"])){
$email = $_POST["email"];
} else {
$email = "";
}
if(isset($_POST["colour"])){
$colour = $_POST["colour"];
} else {
$colour = "";
}
if(isset($_POST["music"])){
$music = $_POST["music"];
} else {
$music = "";
}
if(isset($_POST["superpower"])){
$superpower = $_POST["superpower"];
} else {
$superpower = "";
}
$sql = "INSERT INTO profiles (name, email, colour, music, superpower) VALUES ('$name', '$email', '$colour', '$music', '$superpower')";
if(mysqli_query($con, $sql)){
header ('location: results.php'.$query_string);
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
}
if($name !== "" && $email !== "" && $colour !== "" && $music !== "" && $superpower !== "") {
$query_string = '?name=' . $name.'&email='.$email.'&colour='.$colour.'&music='.$music.'&superpower='.$superpower;
header('HTTP/1.1 303 See Other');
header ('location: results.php'.$query_string);
}
?>
And my results page.
<html>
<head>
<title>POST Success</title>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'anas12_test', 'a1b2c3d4', 'anas12_test');
if(isset($_GET["name"])){
$name = $_GET["name"];
} else {
$name = "no name";
}
if(isset($_GET["email"])){
$email = $_GET["email"];
} else {
$email = "no email";
}
if(isset($_GET["colour"])){
$colour = $_GET["colour"];
} else {
$colour = "no colour:";
}
if(isset($_GET["music"])){
$music = $_GET["music"];
} else {
$music = "music";
}
if(isset($_GET["superpower"])){
$superpower = $_GET["superpower"];
} else {
$superpower = "superpower";
}
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM profiles");
echo "<div style='border:solid 1px #ccc;padding:10px;float:left;margin-top:10px;'>";
echo "<table border='1'> <tr> <th>Name</th> <th>Email</th> <th>Favourite Colour</th>
<th>Favourite Music</th>
<th>Superhero Ability</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['colour'] . "</td>";
echo "<td>" . $row['music'] . "</td>";
echo "<td>" . $row['superpower'] . "</td>";
echo "</tr>";}
echo "</table>";
echo "</div>";
mysqli_close($con);
?>
</body>
</html>
Your form has no action, so it'll submit the form to the URL you loaded the page from, which will be index.php.
You need this:
<form id="profiles" action="Submit.php" method="POST">
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Note the method portion as well - with no method, forms default to using GET
Be careful you have two form in your index.php.
<form id="profiles">
and
<form action="submit.php" method="post">
I think the first one is useless.

How to echo an error div if the reCaptcha isn't confirm

I have a script and it runs very well, but I want know how to echo an error div if recaptcha isn't confirmed. In this script if recaptcha wasn't confirmed the page will reload and nothing will be send to my mail, but I don't know how to display an error that tells the user: "You must verificate that you aren't a robot".
Can you help me?
CODE:
<?php
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"../support/style/contactform.css\">\n";
$emailpattern="^[^# ]+#[^# ]+\.[^# \.]+$";
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<form method="post" id="contactformall">
<p>Nome</p>
<input type="text" name="name" class="contactformimput"/>
<p>Email (obbligatorio)</p>
<input type="text" name="email" class="contactformimput"/>
<p>Numero</p>
<input type="text" name="number" class="contactformimput"/>
<p>Messaggio (obbligatorio)</p>
<input type="text" name="message" onkeyup="adjust_textarea(this)" class="contactformimput" id="contactformtext">
<div class="g-recaptcha" data-sitekey="____key____"></div>
<div id="divcontactbutton">
<input type="reset" name="send" value="Resetta" class="button" id="resetmessage"/>
<input type="submit" name="send" value="Invia Messaggio" class="button" id="sendmessage"/>
</div>
</form>';
exit;
}
$responsejson=file_get_contents("google.com/recaptcha/api/…);
$response = json_decode($responsejson);
if($response->success==false) { echo "<div class=\"emailerror\" id=\"emailnoninviata\"><div><span>•</span> Email non inviata</div></div>";
**/////THIS part DON'T RUN**
}else
{
if (isset($_POST['send'])) {
if(!ereg($emailpattern,$_POST['email'])) {
$emailerror = true;
echo "<div class=\"emailerror\"><div><span>6</span> Email non valida</div></div>";
} if ($_POST['message'] == "") {
$emailerror = true;
echo "<div class=\"emailerror\"><div><span>6</span> Inserisci un messaggio</div></div>";
} elseif ($_POST['message'] != "" and ereg($emailpattern,$_POST['email'])){
$emailerror = false;
};
if ($emailerror == true) {
echo '<form method="post">
<p>Nome</p>
<input type="text" name="name" class="contactformimput"/>
<p>Email*</p>
<input type="text" name="email" class="contactformimput"/>
<p>Numero</p>
<input type="text" name="number" class="contactformimput"/>
<p>Messaggio*</p>
<input type="text" name="message" onkeyup="adjust_textarea(this)" class="contactformimput" id="contactformtext">
<div class="g-recaptcha" data-sitekey="___KEY___"></div>
<div id="divcontactbutton">
<input type="reset" name="send" value="Resetta" class="button" id="resetmessage"/>
<input type="submit" name="send" value="Invia Messaggio" class="button" id="sendmessage"/>
</div>
</form>';
}
if (isset($_POST['send']) and $emailerror == false) {
$to = "mail#gmail.com";
$subject = "B&B";
$user_name = 'Name: ' . $_POST['name'] . "\n";
$user_email = 'Email: ' . $_POST['email'] . "\n";
$user_ip = 'IP: ' . $_SERVER['REMOTE_ADDR'] . "\n";
$user_message = 'Message: ' . $_POST['message'];
$message = $user_name . $user_email . $user_ip . $user_message;
$success = mail($to, $subject, $message);
echo "<div class=\"emailerror\" id=\"emailinviata\"><div> <span>5</span> Email inviata correttamente</div></div>";}
}
}
?>
You are mixing javascript code with php code. Javascripts uses '.' to access class vars/functions, php uses '->'.
Please read this page in order to use Recaptcha with php: https://developers.google.com/recaptcha/old/docs/php
The recaptcha documentation shows that this API request should return JSON. You will need to decode the JSON response to refer to its contents. So do this after you use file_get_contents() to get the response:
$response = json_decode($response);
and then you should be able to check for success with a little adjustment to your syntax.
if($response->success==false)

Validate Empty / Radio Fields - Php

<?php
$name = $_POST["name"];
$email = $_POST["email"];
$gender = $_POST["gen"];
$age = $_POST["age"];
$comments = $_POST["comments"];
if(empty($_POST["name"])){
echo "empty name";
}
if(empty($_POST["email"])){
echo "empty email";
}
///
if(empty($_POST["gen"])){
echo "empty gender";
}
if(empty($_POST["comments"])){
echo "empty comments";
}
if(!isset($_POST['submit'])) {
?>
<html>
<head>
<title>Lab6 : P1</title>
</head>
<body>
<fieldset>
<legend><h4>Enter your information in the fields below</h4></legend>
<form name="info" method="post" action="<?php echo $PHP_SELF;?>" method="post">
<strong>Name:</strong> <input type="text" name="name" id="name" /><br>
<strong>Email:</strong> <input type="text" name="email" id="email" /><br>
<br>
<strong>Gender</strong><br>
<input type="radio" name="gen" value="Male">Male</input><br>
<input type="radio" name="gen" value="Female">Female</input><br>
<br>
<select name="age">
<option value="Under 30">Under 30</option><br>
<option value="Between 30 and 60">Between 30 and 60</option><br>
<option value="60+">60+</option>
</select>
<br>
Comments: <textarea name="comments" cols="20" rows="5"> </textarea>
</fieldset>
<input type="submit" name="submit" value="Submit my Information" />
</form>
</body>
</html>
<?
}
else {
echo "Thank you, ".$name." for your comments: " . "<strong>" . $comments . "</strong>";
echo "<br>We will reply to you at:" . "<em>" . $email . "</em>";
}
?>
I need it to validate the fields: name, email, comments for empty strings (not allowed) and not allow unselected radio buttons for gender and age selection.
Can anyone help
<?php
// to eventually re-fill the fields
$name = "";
$email = "";
$gender = "";
$age = "";
$comments = "";
// to re-select a radio button and select option
$Mchecked = "";
$Fchecked = "";
$selectMinus_30="";
$select30_to_60="";
$select60_plus="";
// to display errors
$error = "";
$done=false;
if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["age"])){
if($_POST["name"]==""){
$error = "empty name <br/>";
}
if($_POST["email"]==""){
$error = $error . "empty mail <br/>";
}
if(!isset($_POST["gen"])){
$error = $error . "empty gender <br/>";
}
else{
$gender = $_POST["gen"];
if ($gender == "Male"){
$Mchecked = "checked";
}
else if ($gender == "Female"){
$Fchecked = "checked";
}
}
if($_POST["comments"]==""){
$error = $error . "error: empty comments <br/>";
}
$name = $_POST["name"];
$email = $_POST["email"];
$comments = $_POST["comments"];
$age = $_POST["age"];
if ($age == "Under 30"){
$selectMinus_30 = "selected";
}
else if ($age == "Between 30 and 60"){
$select30_to_60 = "selected";
}
else if ($age == "60+"){
$select60_plus = "selected";
}
if ($error==""){
$done=true;
}
}
?>
<html>
<head>
<title>Lab6 : P1</title>
</head>
<body>
<?php if (!$done){ ?>
<fieldset>
<legend><h4>Enter your information in the fields below</h4></legend>
<p class="error" style="color:red;"><?php echo $error;?></p>
<form name="info" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
<strong>Name:</strong> <input type="text" name="name" id="name" value="<?php echo $name; ?>" /><br/>
<strong>Email:</strong> <input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br/>
<br/>
<strong>Gender</strong><br/>
<input type="radio" name="gen" value="Male" <?php echo $Mchecked;?>>Male</input><br/>
<input type="radio" name="gen" value="Female" <?php echo $Fchecked;?>>Female</input><br/>
<br/>
<select name="age" value="<?php echo $age;?>">
<option value="Under 30" <?php echo $selectMinus_30;?>>Under 30</option><br/>
<option value="Between 30 and 60" <?php echo $select30_to_60;?>>Between 30 and 60</option><br/>
<option value="60+" <?php echo $select60_plus;?>>60+</option>
</select>
<br/>
Comments: <textarea name="comments" cols="20" rows="5"><?php echo $comments; ?></textarea>
</fieldset>
<input type="submit" name="submit" value="Submit my Information" />
</form>
<?php }else{
echo "Thank you, ".$name." for your comments: " . "<strong>" . $comments . "</strong>";
echo "<br/>We will reply to you at:" . "<em>" . $email . "</em>";
} ?>
</body>
</html>
Here I did proper validation, and also re-fill the form when you submit with any empty field.
P.S. Thank you Marc B , I didn't realize that my first post was aweful.

Categories