Php, Html5, xampp - php

I was trying execute this PHP file that I have in XAMPP. I am trying to update the data to the database through a form, when I enter the info and give submit button the page just reloads or I get the message all fields are required though I fill up all. Give an it a check to the code. The data I enter just doesn't get updated to the database. I've checked with the database name and table name that if I am mentioning it properly and also I've tried just checking that if I am able to connect to the database. In that case it is getting connected to the database properly.but just not updating the info the table.
<form action="" method="POST">
<div class="main">
<div class="icon-head-wthree">
<h2><span class="fa fa-heart t-w3" aria-hidden="true"></span></h2>
<h4>Donor?</h4>
</div>
<div class="form-left-to-w3l">
<input type="text" name="name" placeholder="First Name">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="lname" placeholder="Last Name">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="city" placeholder="City" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="district" placeholder="District" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="state" placeholder="State" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="Phone" placeholder="Phone 1" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="p1" placeholder="Phone 2">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="pincode" placeholder="Pincode" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="bg " placeholder="Blood Group" >
<div class="clear"></div>
</div>
<div class="form-left-to-add">
<input type="text" name="add1 " placeholder="Address 1" >
<div class="clear"></div>
</div>
<div class="form-left-to-add">
<input type="text" name="add2 " placeholder="Address 2" >
<div class="clear"></div>
</div>
<input type="checkbox" name="check" checked="checked">
<span class="checkmark"></span>
<label class="cont">I agree to the terms and conditions
</label>
<div class="btnn">
<input type="submit" value="submit" name="submit">Submit Request </button><br>
<div class="clear"></div>
</div>
</div>
</form>
<?php
if(isset($_POST["submit"])){
$db= 'bank';
if(!empty($_POST['name']) && !empty($_POST['lname']) && !empty($_POST['city']) && !empty($_POST['district']) && !empty($_POST['state']) && !empty($_POST['Phone']) && !empty($_POST['p1']) && !empty($_POST['pincode']) && !empty($_POST['bg']) && !empty($_POST['add1']) && !empty($_POST['add2']))
{ //added
$name=$_POST['name'];
$lname=$_POST['lname'];
$city= $_POST['city'];
$district= $_POST['district'];
$state= $_POST['state'];
$Phone= $_POST['Phone'];
$p1= $_POST['p1'];
$pincode= $_POST['pincode'];
$bg= $_POST['bg'];
$add1= $_POST['add1'];
$add2= $_POST['add2'];
$con=mysqli_connect('localhost','root','',$db) or die(mysql_error());
$sql="INSERT INTO donor VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2')";
$result=mysqli_query($con,$sql);
if($result){
echo "Account Successfully Created";
} else {
echo "Failure!";
}
}
else {
echo "All fields are required!";
}
}
?>

as the last answer remove the white space in (blood group : name="bg", address1: name="add1", address2: name="add2") some of name of input tag.
<form action="" method="POST">
<div class="main">
<div class="icon-head-wthree">
<h2><span class="fa fa-heart t-w3" aria-hidden="true"></span></h2>
<h4>Donor?</h4>
</div>
<div class="form-left-to-w3l">
<input type="text" name="name" placeholder="First Name">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="lname" placeholder="Last Name">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="city" placeholder="City" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="district" placeholder="District" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="state" placeholder="State" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="Phone" placeholder="Phone 1" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="p1" placeholder="Phone 2">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="pincode" placeholder="Pincode" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="bg " placeholder="Blood Group" > // remoce white space
<div class="clear"></div>
</div>
<div class="form-left-to-add">
<input type="text" name="add1 " placeholder="Address 1" > // remoce white space
<div class="clear"></div>
</div>
<div class="form-left-to-add">
<input type="text" name="add2 " placeholder="Address 2" > // remoce white space
<div class="clear"></div>
</div>
<input type="checkbox" name="check" checked="checked">
<span class="checkmark"></span>
<label class="cont">I agree to the terms and conditions
</label>
<div class="btnn">
<input type="submit" value="submit" name="submit">Submit Request </button><br>
<div class="clear"></div>
</div>
</div>
</form>
<?php
if(isset($_POST["submit"]) == "submit"){
$db= 'bank';
if(!empty($_POST['name']) && !empty($_POST['lname']) && !empty($_POST['city']) && !empty($_POST['district']) && !empty($_POST['state']) && !empty($_POST['Phone']) && !empty($_POST['p1']) && !empty($_POST['pincode']) && !empty($_POST['bg']) && !empty($_POST['add1']) && !empty($_POST['add2']))
{ //added
$name=$_POST['name'];
$lname=$_POST['lname'];
$city= $_POST['city'];
$district= $_POST['district'];
$state= $_POST['state'];
$Phone= $_POST['Phone'];
$p1= $_POST['p1'];
$pincode= $_POST['pincode'];
$bg= $_POST['bg'];
$add1= $_POST['add1'];
$add2= $_POST['add2'];
$con=mysqli_connect('localhost','root','',$db) or die(mysql_error());
$sql="INSERT INTO donor VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2')";
$result=mysqli_query($con,$sql);
if($result){
echo "Account Successfully Created";
} else {
echo "Failure!";
}
}
else {
echo "All fields are required!";
} }
?>
this code copy and paste.
and when ever you use the isset function please user the post's value to perfect condition like
if(isset($_POST["submit"]) == "submit")
if you have many button in one page then it will very help full thanks.

Some of your input tag names has spaces like name="bg " instead of name="bg". Same case for inputs - add1, and add2.
Updated code below. It should be working fine.
<form action="" method="POST">
<div class="main">
<div class="icon-head-wthree">
<h2><span class="fa fa-heart t-w3" aria-hidden="true"></span></h2>
<h4>Donor?</h4>
</div>
<div class="form-left-to-w3l">
<input type="text" name="name" placeholder="First Name">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="lname" placeholder="Last Name">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="city" placeholder="City" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="district" placeholder="District" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="state" placeholder="State" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="Phone" placeholder="Phone 1" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="p1" placeholder="Phone 2">
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="pincode" placeholder="Pincode" >
<div class="clear"></div>
</div>
<div class="form-left-to-w3l">
<input type="text" name="bg" placeholder="Blood Group" >
<div class="clear"></div>
</div>
<div class="form-left-to-add">
<input type="text" name="add1" placeholder="Address 1" >
<div class="clear"></div>
</div>
<div class="form-left-to-add">
<input type="text" name="add2" placeholder="Address 2" >
<div class="clear"></div>
</div>
<input type="checkbox" name="check" checked="checked">
<span class="checkmark"></span>
<label class="cont">I agree to the terms and conditions
</label>
<div class="btnn">
<input type="submit" value="submit" name="submit">Submit Request </button><br>
<div class="clear"></div>
</div>
</div>
</form>
<?php
if(isset($_POST["submit"])){
$db= 'bank';
if(!empty($_POST['name']) && !empty($_POST['lname']) && !empty($_POST['city']) && !empty($_POST['district']) && !empty($_POST['state']) && !empty($_POST['Phone']) && !empty($_POST['p1']) && !empty($_POST['pincode']) && !empty($_POST['bg']) && !empty($_POST['add1']) && !empty($_POST['add2']))
{ //added
$name=$_POST['name'];
$lname=$_POST['lname'];
$city= $_POST['city'];
$district= $_POST['district'];
$state= $_POST['state'];
$Phone= $_POST['Phone'];
$p1= $_POST['p1'];
$pincode= $_POST['pincode'];
$bg= $_POST['bg'];
$add1= $_POST['add1'];
$add2= $_POST['add2'];
$con=mysqli_connect('localhost','root','',$db) or die(mysql_error());
$sql="INSERT INTO donor VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2')";
$result=mysqli_query($con,$sql);
if($result){
echo "Account Successfully Created";
} else {
echo "Failure!";
}
}
else {
echo "All fields are required!";
}
}
?>

It is possible to write the INSERT INTO statement in two ways.
The first way specifies both the column names and the values to be inserted:
Step 1:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. The INSERT INTO syntax would be as follows:
Step 2:
INSERT INTO table_name VALUES (value1, value2, value3, ...);
And in your code I suppose you are trying to use the second method but you are not using all the column. (For example id column which may be auto increment )
So change your this insert code
INSERT INTO donor VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2');
to step 1 as I have shown above which looks like shown below.
INSERT INTO donor (name, lname, city, district, state, phone, p1, pincode, bg, add1, add2) VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2');

Related

html form validation using php

I have created a html form which has signup as well as login. Firstly I want to register the user using signup form and then make him login then redirect to another page. For this I'm using php with apache(xampp) server. The signup panel is working fine, but when I try to login, black column is added to database
My html code is like this
<form method="POST" action="yoga_signup.php">
<div class="row" style=" margin-top:4rem; margin-left:24rem;">
<div class="col-md-6 mx-auto p-0">
<div class="card">
<div class="login-box">
<div class="login-snip"> <input id="tab-1" type="radio" name="tab" class="sign-in" checked><label for="tab-1" class="tab">Login</label> <input id="tab-2" type="radio" name="tab" class="sign-up"><label for="tab-2" class="tab">Sign Up</label>
<div class="login-space">
<div class="login">
<input type="hidden" name="for_page" value="login">
<div class="group"> <label for="user" class="label">Username</label> <input id="user" type="text" class="input" name="username" placeholder="Enter your username" > </div>
<div class="group"> <label for="pass" class="label">Password</label> <input id="pass" type="password" name="password_1" class="input" data-type="password" placeholder="Enter your password" > </div>
<div class="group"> <input id="check" type="checkbox" class="check" checked> <label for="check"><span class="icon"></span> Keep me Signed in</label> </div>
<div class="group"> <input type="submit" class="button" value="Sign In"> </div>
<div class="hr"></div>
</div>
<div class="sign-up-form">
<input type="hidden" name="for_page" value="signup">
<div class="group"> <label for="user" class="label">Username</label> <input id="user" type="text" name="username" class="input" placeholder="Create your Username"> </div>
<div class="group"> <label for="pass" class="label">Password</label> <input id="pass" type="password" name="password_1" class="input" data-type="password" placeholder="Create your password" > </div>
<div class="group"> <label for="pass" class="label">Repeat Password</label> <input id="pass" type="password" name="password_2" class="input" data-type="password" placeholder="Repeat your password"> </div>
<div class="group"> <label for="pass" class="label">Email Address</label> <input id="pass" type="text" class="input" name="email" placeholder="Enter your email address" > </div><br>
<div class="group"> <input type="submit" class="button" value="Sign Up"> </div>
<div class="hr"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
and my php code is this
<?php
session_start();
$conn= new mysqli("localhost","root","","yoga");
if($_SERVER["REQUEST_METHOD"]=="POST"){
$TypeOfRequest=$_POST['for_page'];
if($TypeOfRequest=="signup"){
$user=$_POST['username'];
$pass_1=$_POST['password_1'];
$pass_2=$_POST['password_2'];
$mail=$_POST['email'];
if($pass_1===$pass_2){
$val=" INSERT INTO yoga_login(username,password_1,password_2,email) VALUES('$user','$pass_1','$pass_2','$mail')";
if($conn->query($val)===TRUE){
header('Location: login.html');
}else{
echo "registration unsucessfull";
}
}
if($pass_1!==$pass_2){
echo "please enter the same password";
}
}
$TypeofRequest = $_POST['for_page'];
if($TypeofRequest=="login"){
$user=$_POST['username'];
$pass_1=$_POST['password_1'];
$sql="SELECT * FROM yoga_login WHERE username='$user' AND password='$pass_1'";
$RunQuery = mysqli_query($conn,$sql);
$CheckNumberOfUsers = mysqli_num_rows($RunQuery);
//echo $CheckNumberOfUsers;
if($CheckNumberOfUsers==1){
$_SESSION['username'] = $user;
header('location:blogs.html');
}else{
echo "invalid credential";
}
}
mysqli_close($conn);
}
You can dump out what you are sending in the yoga_signup.php at the start of the file:
die('<pre>' . print_r($_POST, true) . '</pre>');
Then you can see this:
Array
(
[tab] => on
[for_page] => signup
[username] =>
[password_1] =>
[password_2] =>
[email] =>
)
so the for_page field's value is signup - and I was pressing the Sign In button
Then you go back to the HTML file and you can see that you are using the same form for the request, so with the second <input type="hidden" name="for_page" value="signup">, you are overwriting the first for_page value.
So instead of this you should create 2 forms and set them like this:
<div class="row" style=" margin-top:4rem; margin-left:24rem;">
<div class="col-md-6 mx-auto p-0">
<div class="card">
<div class="login-box">
<div class="login-snip">
<input id="tab-1" type="radio" name="tab" class="sign-in" checked>
<label for="tab-1" class="tab">Login</label>
<input id="tab-2" type="radio" name="tab" class="sign-up">
<label for="tab-2" class="tab">Sign Up</label>
<div class="login-space">
<form method="POST" action="yoga_signup.php">
<div class="login">
<input type="hidden" name="for_page" value="login">
<div class="group">
<label for="user" class="label">Username</label>
<input id="user" type="text" class="input" name="username" placeholder="Enter your username" >
</div>
<div class="group">
<label for="pass" class="label">Password</label>
<input id="pass" type="password" name="password_1" class="input" data-type="password" placeholder="Enter your password" >
</div>
<div class="group">
<input id="check" type="checkbox" class="check" checked>
<label for="check">
<span class="icon">
</span> Keep me Signed in
</label>
</div>
<div class="group"> <input type="submit" class="button" value="Sign In"> </div>
<div class="hr"></div>
</div>
</form>
<form method="POST" action="yoga_signup.php">
<div class="sign-up-form">
<input type="hidden" name="for_page" value="signup">
<div class="group"> <label for="user" class="label">Username</label> <input id="user" type="text" name="username" class="input" placeholder="Create your Username"> </div>
<div class="group"> <label for="pass" class="label">Password</label> <input id="pass" type="password" name="password_1" class="input" data-type="password" placeholder="Create your password" > </div>
<div class="group"> <label for="pass" class="label">Repeat Password</label> <input id="pass" type="password" name="password_2" class="input" data-type="password" placeholder="Repeat your password"> </div>
<div class="group"> <label for="pass" class="label">Email Address</label> <input id="pass" type="text" class="input" name="email" placeholder="Enter your email address" > </div><br>
<div class="group"> <input type="submit" class="button" value="Sign Up"> </div>
<div class="hr"></div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
Parameters in the PHP file after modification:
Array
(
[for_page] => login
[username] => asdf
[password_1] => asdf
)
Helpful sidenotes to improve your code:
passwords should be encrypted in the database
the mysql query is vulnerable against SQL injection
the second password field used to be required, because if the input type is password, then you can't see the characters, so it's like a proofread for the user to make sure that you won't miss the password. Because of this you don't need to store it in the database, but use it to check if it was written correctly in the first field.
use the redirection headers with die
close the mysql connection before the redirection
I found some issues at the login part of the code.
Instead of the password filed in the SQL query string you should use an existing password field from your database; for example the password_1 field.
Replacing that and fixing some minor issues, plus a bit of beautifying you will get this code for the login part:
if($TypeofRequest=="login"){
$username = $_POST['username'];
$password = $_POST['password_1'];
$sql="SELECT * FROM yoga_login WHERE username='{$username}' AND password_1='{$password}'";
$result = $conn->query($sql);
$conn->close();
if (empty($result)) {
die('invalid credentials');
}
$_SESSION['username'] = $user;
header('location:blogs.html');
die();
}

how to get the data added verification message at the same page of form

i have this page named courses.php contain the form below (iam using bootstrap):
<form class="" action="coumysql.php" method="post" style="margin-left:100px;">
<input type="hidden" name="act" value="add"/>
<div class="form-group" >
<label class="control-label" for="course_id">Course Code:</label>
<div class="" >
<div class="col-xs-3">
<input type="text" class="form-control" id="email" placeholder="" name="course_id" required="">
</div>
</div>
</div> <br>
<br>
<div class="form-group">
<label class="control-label " for="course_name">Course Name:</label>
<div class="">
<div class="col-xs-3">
<input type="text" class="form-control" id="pwd" placeholder="" name="course_name" required="">
</div>
</div>
</div><br>
<br>
<div class="form-group" style="display: inline;">
<div class="col-sm-offset-1 "><br>
<button type="submit" class="btn btn-default">Add Subject</button>
</div>
</div>
</form>
the form processed at the page named coumysql.php
<?php
include 'connect.php';
if(isset($_POST['act'])){
if($_POST['act'] == 'add'){
if ($mysqli->query("INSERT INTO courses (course_id, course_name) VALUES ('".$_POST['course_id']."', '".$_POST['course_name']."');")) {
echo "data added";
}
}else if($_POST['act'] == 'delete'){
if ($mysqli->query("DELETE FROM courses WHERE course_id= ('".$_POST['course_id']."');")) {
echo "data deleted";
}
}
}
?>
i want the message "data added" or "data deleted" to be shown in courses.php after successfully form submitted to database
You could create a session() variable and pass it to the view. Like so:
<?php
include 'connect.php';
session_start();
if(isset($_POST['act'])){
if($_POST['act'] == 'add'){
if ($mysqli->query("INSERT INTO courses (course_id, course_name) VALUES ('".$_POST['course_id']."', '".$_POST['course_name']."');")) {
$_SESSION['insert']="Data has been successfully created!";
header('Location: courses.php');
}
}else if($_POST['act'] == 'delete'){
if ($mysqli->query("DELETE FROM courses WHERE course_id= ('".$_POST['course_id']."');")) {
$_SESSION['delete']="Data has been successfully deleted!";
header('Location: courses.php');
}
}
}
?>
Then catch them on courses.php like so:
<?php
session_start();
$added = $_SESSION['insert'];
$deleted=$_SESSION['delete'];
?>
<form class="" action="coumysql.php" method="post" style="margin-left:100px;">
<input type="hidden" name="act" value="add"/>
<div class="form-group" >
<label class="control-label" for="course_id">Course Code:</label>
<div class="" >
<div class="col-xs-3">
<input type="text" class="form-control" id="email" placeholder="" name="course_id" required="">
</div>
</div>
</div> <br>
<br>
<div class="form-group">
<label class="control-label " for="course_name">Course Name:</label>
<div class="">
<div class="col-xs-3">
<input type="text" class="form-control" id="pwd" placeholder="" name="course_name" required="">
</div>
</div>
</div><br>
<br>
<div class="form-group" style="display: inline;">
<div class="col-sm-offset-1 "><br>
<button type="submit" class="btn btn-default">Add Subject</button>
</div>
<?php
if(!empty($added)){
?>
<div class="alert aler-success"><p><?php echo $added;?></p></div>
<?php }elseif(!empty($deleted)){
?>
<div class="alert aler-success"><p><?php echo $deleted;?></p></div>
<?php
}?>
</div>
</form>
Note: u can customize div tag as u want..

How to implement form inside a tab panel?

I have written a php code for a form which is inside a tab panel. But the values are not getting inside database. I have written the php code on the top of the php file.After submitting the form submit it goes to the second tab when the php code is written on top of the file before tag and the values obtained from the form is not getting inserted into the database. Why is it like that ? I dont know how to correct this. Can anyone help me on this ?
php code :
<?php
include("database.php");
if(isset($_REQUEST['save'])){
if($_POST['start']!=''&&$_POST['end']!=''&&$_POST['firstname']!=''&&$_POST['lastname']!=''&&$_POST['age']!='')
{
$start = $_POST['start'];
$end = $_POST['end'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$cur = date("d.m.Y");
$j = 'CUS'.rand(1,1000000);
$sql = "insert into tbl_customer(id,customer_id,firstname,lastname,age,rentalfromdate,rentaltodate,date) values(' ','$j','$firstname','$lastname','$age','$start','$end','$cur')";
mysqli_query($con,$sql) or die(mysqli_error($con));
}
}
?>
tab :
<div class="tab-pane active" role="tabpanel" id="step1">
<div class="step1">
<div class="spec">
<div class="col-md-6">
</div>
<div class="col-md-6">
</div>
</div>
<div class="main_cont">
<div class="main-agileits">
<div class="register-box">
<form name="customer" id="customer" method="post" action="#step1">
<div id="flight-datepicker" class="input-daterange input-group">
<div class="col-md-6 s_pad">
<label>First day of rental</label><span class="fontawesome-calendar"></span>
<input type="text" id="start-date" name="start" placeholder="Select Date" data-date-format="DD, MM d" class="input-sm form-control" /><span class="date-text date-depart"></span>
</div>
<div class="col-md-6 s_pad">
<label>Last day of rental</label><span class="fontawesome-calendar"></span>
<input type="text" id="end-date" name="end" placeholder="Select Date" data-date-format="DD, MM d" class="input-sm form-control"/><span class="date-text date-return"></span>
</div>
</div>
<div>
<div class="form-group col-md-12 s_pad">
<label for="exampleInputEmail1">First Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" name="firstname" placeholder="First Name" />
</div>
<div class="form-group col-md-12 s_pad">
<label for="exampleInputEmail1">Last Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" name="lastname" placeholder="Last Name" />
</div>
<div class="form-group col-md-6 s_pad">
<label for="exampleInputEmail1" class="ctred">Age only if under 18</label>
<input type="text" class="form-control" name="age" id="exampleInputName" placeholder="Age">
</div>
</div>
<div>
<button type="submit" name="save" class="btn btn-primary mag_top2 next-step">Continue</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
Do like this. Then you will get the error or success message:-
if (mysqli_query($con, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
I hope id field is primary key so you dont require it in your sql query so try the following query.
$sql = "insert into tbl_customer(customer_id,firstname,lastname,age,rentalfromdate,rentaltodate,date) values('$j','$firstname','$lastname','$age','$start','$end','$cur')";

Recaptcha with Salesforce Web-To-Lead

I have a salesforce web-to-lead form that I have on my website. Everything works fine but there is no recaptcha or any way to prevent spamming. So I downloaded recaptcha, got everything setup, and I also followed the instructions here to take the $_POST data, verify the recaptcha is correct and then submit it over to salesforce.
I got all of that setup but when I submit the form it just goes to my check-this-first.php (I called it contactFormConfirm.php) page and is just blank. It doesn't hit the retURL or actually submit to salesforce.
Any light that can be shed to help me out would be amazing. Thank you in advance.
Here is the code that I am using:
Salesforce Web to Lead:
<form action="/contactFormConfirm.php" method="POST">
<input type=hidden name="oid" value="#############">
<input type=hidden name="retURL" value="http://www.google.com">
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="first_name" class="span3">First Name</label>
<input id="first_name" maxlength="40" name="first_name" size="20" type="text" class="span9" />
</div>
<div class="span6 pull-left">
<label for="last_name" class="span3">Last Name</label>
<input id="last_name" maxlength="80" name="last_name" size="20" type="text" class="span9" /><br>
</div>
</div>
<div class="clearfix"></div>
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="title" class="span3">Title</label>
<input id="title" maxlength="40" name="title" size="20" type="text" class="span9" /><br>
</div>
<div class="span6 pull-left">
<label for="email" class="span3">Email</label>
<input id="email" maxlength="80" name="email" size="20" type="text" class="span9" /><br>
</div>
</div>
<div class="clearfix"></div>
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="phone" class="span3">Phone</label>
<input id="phone" maxlength="40" name="phone" size="20" type="text" class="span9" /><br>
</div>
<div class="span6 pull-left">
<label for="00N4000000231ne" class="span3" style="position:relative; top:-1.2em;">Profile:</label>
<select id="profile" name="00N4000000231ne" title="Profile" class="span9"> <option value="">--None--</option>
**[other options hidden for privacy]**
</select>
</div>
</div>
<div class="clearfix"></div>
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="company" class="span3">Company</label>
<input id="company" maxlength="40" name="company" size="20" type="text" class="span9" /><br>
</div>
<div class="span6 pull-left">
<label for="country" class="span3">Country</label>
<input id="country" maxlength="40" name="country" size="20" type="text" class="span9" /><br>
</div>
</div>
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="street" class="span3">Address</label>
<textarea name="street" class="span9"></textarea><br>
</div>
<div class="span6 pull-left">
<label for="city" class="span3">City</label>
<input id="city" maxlength="40" name="city" size="20" type="text" class="span9" /><br>
</div>
</div>
<div class="clearfix"></div>
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="state" class="span3" style="position:relative; top:-1.2em;">State / Province</label>
<input id="state" maxlength="20" name="state" size="20" type="text" class="span9" /><br>
</div>
<div class="span6 pull-left">
<label for="zip" class="span3">Zip</label>
<input id="zip" maxlength="20" name="zip" size="20" type="text" class="span9" /><br>
</div>
</div>
<div class="clearfix"></div>
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="00N40000001V9wA" class="span3" style="position:relative; top:-1.2em;">Product Interest:</label>
<select id="productInterest" multiple="multiple" name="00N40000001V9wA" title="Product Interest" class="span9" size="5">
<option value="API">API</option>
**[other options hidden for privacy]**
</select><br>
</div>
<div class="span6 pull-left">
<label for="00N40000001V8Yl" class="span3" style="position:relative; top:-1.2em;">Background Notes:</label>
<textarea id="notes" name="00N40000001V8Yl" type="text" wrap="soft" class="span9"></textarea><br>
</div>
</div>
<div class="clearfix"><br /></div>
<div class="span12 offset3">
<div class="span10 pull-right">
<label for="lead_source" class="span8 pull-left">Check here to be contacted by *** for a demonstration.</label>
<input id="demonstration" type="checkbox" name="lead_source" class="span1 pull-left" value="Web w/Demo Request"></input>
</div>
<div class="span10 pull-right">
<label for="00N400000023QE0" class="span8 pull-left">Check here if you have a timeline for this project.</label>
<input id="timeline" type="checkbox" name="00N400000023QE0" class="span1 pull-left" value="Confirmed"></input>
</div>
<div class="span10 pull-right">
<label for="00N400000023QE5" class="span8 pull-left">Check here if you have purchase authority for this project.</label>
<input id="purchaseAuthority" type="checkbox" name="00N400000023QE5" class="span1 pull-left" value="Confirmed"></input>
</div>
</div>
<div class="clearfix"><br /></div>
<div class="row-fluid">
<div class="span9 pull-right offset2">
<div class="span8">
<?php
require_once('recaptcha/recaptchalib.php');
$publickey = "xxxxxxxxxxxxx"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?><br />
</div>
</div>
</div>
<input type="hidden" name="lead_source" value="Web" />
<input type="hidden" name="00N4000000231kf" value="Web Inquiry" />
<div class="span10 offset1 pull-right">
<input type="submit" name="ctl00$ctl00$MainContent$mainContentHolder$btnSubmit" value="Submit Inquiry" class="btn btn-primary span3 pull-right" />
</div>
</form>
This is the contactFormConfirm.php code with the php cURL code:
<?php
ob_start();
session_start();
include_once('[path-to]/recaptcha/recaptchalib.php');
//Recaptcha Settings
$publickey = "xxxxxxxxx"; // you got this from the signup page
$privatekey = "xxxxxxxxx";
//curl method posting
//extract data from the post
extract($_POST);
if (isset($submit)){
$ok = 1;
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
$ok = 0;
}
if ($ok){
//set POST variables
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
$fields = array(
'oid'=>urlencode($oid),
'retURL'=>urlencode($retURL),
'first_name'=>urlencode($first_name),
'last_name'=>urlencode($last_name),
'title'=>urlencode($title),
'email'=>urlencode($email),
'phone'=>urlencode($phone),
'00N4000000231ne'=>urlencode($profile),
'company'=>urlencode($company),
'country'=>urlencode($country),
'address'=>urlencode($address),
'city'=>urlencode($city),
'state'=>urlencode($state),
'zip'=>urlencode($zip),
'00N40000001V9wA'=>urlencode($productInterest),
'00N40000001V8Yl'=>urlencode($notes),
'lead_source'=>urlencode($demonstration),
'00N400000023QE0'=>urlencode($timeline),
'00N400000023QE5'=>urlencode($purchaseAuthority)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//print_r($fields_string);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
} //if ok
else {
echo "<h4>Sorry - Invalid Captcha </h4>";
}
} //if submit.
?>
After working for several hours, me and another developer at work were able to solve this issue using the solution (kind of) above.
Here's how it broke down:
<script type="text/javascript">
var RecaptchaOptions = {
theme : 'clean'
};
</script>
<form action="/contactFormConfirm.php" method="POST" >
<input type=hidden name="oid" value="#########">
<input type=hidden name="retURL" value="http://www.website.com/thank-you/">
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="first_name" class="span3">First Name</label>
<input id="first_name" maxlength="40" name="first_name" size="20" type="text" class="span9" />
</div>
<div class="span6 pull-left">
<label for="last_name" class="span3">Last Name</label>
<input id="last_name" maxlength="80" name="last_name" size="20" type="text" class="span9" /><br>
</div>
</div>
<div class="clearfix"></div>
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="title" class="span3">Title</label>
<input id="title" maxlength="40" name="title" size="20" type="text" class="span9" /><br>
</div>
<div class="span6 pull-left">
<label for="email" class="span3">Email</label>
<input id="email" maxlength="80" name="email" size="20" type="text" class="span9" /><br>
</div>
</div>
<div class="clearfix"></div>
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="phone" class="span3">Phone</label>
<input id="phone" maxlength="40" name="phone" size="20" type="text" class="span9" /><br>
</div>
<div class="span6 pull-left">
<label for="profile" class="span3" style="position:relative; top:-1.2em;">Profile:</label>
<select id="profile" name="profile" title="OverDrive Profile" class="span9">
<option value="">--None--</option>
</select>
</div>
</div>
<div class="clearfix"></div>
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="company" class="span3">Company</label>
<input id="company" maxlength="40" name="company" size="20" type="text" class="span9" /><br>
</div>
<div class="span6 pull-left">
<label for="country" class="span3">Country</label>
<input id="country" maxlength="40" name="country" size="20" type="text" class="span9" /><br>
</div>
</div>
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="street" class="span3">Address</label>
<textarea name="street" class="span9"></textarea><br>
</div>
<div class="span6 pull-left">
<label for="city" class="span3">City</label>
<input id="city" maxlength="40" name="city" size="20" type="text" class="span9" /><br>
</div>
</div>
<div class="clearfix"></div>
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="state" class="span3" style="position:relative; top:-1.2em;">State / Province</label>
<input id="state" maxlength="20" name="state" size="20" type="text" class="span9" /><br>
</div>
<div class="span6 pull-left">
<label for="zip" class="span3">Zip</label>
<input id="zip" maxlength="20" name="zip" size="20" type="text" class="span9" /><br>
</div>
</div>
<div class="clearfix"></div>
<div class="row span12 pull-left">
<div class="span6 pull-left">
<label for="productInterest" class="span3" style="position:relative; top:-1.2em;">Product Interest:</label>
<select id="productInterest" multiple="multiple" name="productInterest" title="Product Interest" class="span9" size="5">
<option value="API">API</option>
</select><br>
</div>
<div class="span6 pull-left">
<label for="notes" class="span3" style="position:relative; top:-1.2em;">Background Notes:</label>
<textarea id="notes" name="notes" type="text" wrap="soft" class="span9"></textarea><br>
</div>
</div>
<div class="clearfix"><br /></div>
<div class="span12 offset3">
<div class="span10 pull-right">
<label for="lead_source" class="span8 pull-left">Check here to be contacted for a demonstration.</label>
<input id="lead_source" type="checkbox" name="lead_source" class="span1 pull-left" value="Web w/Demo Request"></input>
</div>
<div class="span10 pull-right">
<label for="timeline" class="span8 pull-left">Check here if you have a timeline for this project.</label>
<input id="timeline" type="checkbox" name="timeline" class="span1 pull-left" value="Confirmed"></input>
</div>
<div class="span10 pull-right">
<label for="purchaseAuthority" class="span8 pull-left">Check here if you have purchase authority for this project.</label>
<input id="purchaseAuthority" type="checkbox" name="purchaseAuthority" class="span1 pull-left" value="Confirmed"></input>
</div>
</div>
<div class="clearfix"><br /></div>
<div class="row-fluid">
<div class="span9 pull-right offset2">
<div class="span8">
<?php
require_once('recaptcha/recaptchalib.php');
$publickey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?><br />
</div>
</div>
</div>
<div class="span10 offset1 pull-right">
<input type="submit" name="submit" value="Submit Inquiry" class="btn btn-primary span3 pull-right" />
</div>
</form>
And here's the contactFormConfirm file:
<?php
ob_start();
session_start();
include_once('path-to/recaptcha/recaptchalib.php');
//Recaptcha Settings
$publickey = "xxxxxxxxxxxxxxxxxxxxxxxxx"; // you got this from the signup page
$privatekey = "xxxxxxxxxxxxxxxxxxxxxxxx";
//curl method posting
//extract data from the post
extract($_POST);
if ($submit == 'Submit Inquiry'){
$ok = 1;
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
$ok = 0;
}
if ($ok){
//set POST variables
$url = 'salesforce URL';
$fields = array(
'oid'=>urlencode($oid),
'retURL'=>urlencode($retURL),
'first_name'=>urlencode($first_name),
'last_name'=>urlencode($last_name),
'title'=>urlencode($title),
'email'=>urlencode($email),
'phone'=>urlencode($phone),
'XXXXXXXXXXXXXXXX'=>urlencode($profile),
'company'=>urlencode($company),
'country'=>urlencode($country),
'address'=>urlencode($street),
'city'=>urlencode($city),
'state'=>urlencode($state),
'zip'=>urlencode($zip),
'XXXXXXXXXXXXXXXX'=>urlencode($productInterest),
'XXXXXXXXXXXXXXXX'=>urlencode($notes)
);
// replace XXXXXX with custom field ID
// send default "lead source" value if it is not passed in the HTML form
if (isset($lead_source))
{
$fields['XXXXXXXXXXXXXXXXX'] = urlencode('Web w/Demo Request');
}
else
{
$fields['XXXXXXXXXXXXXXXXX'] = urlencode('Web');
}
// add values to array if they are passed in via the HTML form
if (isset($timeline))
{
$fields['XXXXXXXXXXXXXXXXX'] = urlencode($timeline);
}
if (isset($purchaseAuthority))
{
$fields['XXXXXXXXXXXXXXXX'] = urlencode($purchaseAuthority);
}
// add constant fields
$fields['XXXXXXXXXXXXXXX'] = urlencode('Web Inquiry');
$fields['recordType'] = urlencode('XXXXXXXXXXXXXXX');
//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = substr($fields_string, 0, -1); // delete last &
rtrim($fields_string,'&');
//print_r($fields_string);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
} //if ok
else {
echo "<h4>Sorry - Invalid Captcha </h4>";
}
} //if submit.
?>
I know this is an old thread but I came across it while attempting to do the same thing and came up with this solution:
<?php
ob_start();
session_start();
require_once('recaptchalib.php');
$key_public = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$key_private = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$captchaFailed = false;
$response = null;
$reCaptcha = new ReCaptcha($key_private);
if (!function_exists('curl_version')) {
die('Curl package missing.');
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['g-recaptcha-response'])) {
$response = $reCaptcha->verifyResponse(
$_SERVER['REMOTE_ADDR'],
$_POST['g-recaptcha-response']
);
if ($response !== null) {
if ($response->success) {
$fields = $_POST;
$sendURL = $fields['sendURL'];
unset($fields['sendURL']);
unset($fields['g-recaptcha-response']);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sendURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
} else {
$captchaFailed = true;
}
} else {
$captchaFailed = true;
}
} else {
$captchaFailed = true;
}
}
if ($captchaFailed && count(get_included_files()) < 3) {
echo 'reCAPTCHA failed! Please go back and try again.';
}
?>
You will need to create a hidden input field called sendURL with the value of the URL you'd like to send the form to.
You can either set the action of your form to a file with the code above or include it at the top of the page with your form and leave the action blank.
An example of including it all in the same page could be something like this:
<?php require_once('form-submit.php'); ?>
<html>
<body>
<form method="post">
<input type="hidden" name="sendURL" value="https://login.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8&...">
<label for="name">Name:</label>
<input type="text" name="name" value="<?php if (!empty($_POST['name'])) { echo $_POST['name']; } ?>">
<label for="email">E-Mail:</label>
<input type="text" name="email" value="<?php if (!empty($_POST['email'])) { echo $_POST['email']; } ?>">
<label for="message">Message:</label>
<textarea name="message"><?php if (!empty($_POST['message'])) { echo $_POST['message']; } ?></textarea>
<?php if ($captchaFailed): ?>
<p>reCAPTCHA failed! Please try again!</p>
<?php endif; ?>
<div class="g-recaptcha" data-sitekey="<?php echo $key_public; ?>"></div>
</form>
<script src="https://www.google.com/recaptcha/api.js"></script>
</body>
</html>
And you could use this for pretty much any case where you need to validate the reCAPTCHA before sending the form data off somewhere else.

Reset button using JavaScript is not working after validating the form using Ajax

I have a "create user"form with reset and create button. This reset button is not working for some drop-downs in the form.
In addition, reset button is not clearing all the values in the form after validation. It only clears the values entered after validation.
Here is my code:
<div class="form-action clearfix">
<button type="button" onClick="clearForm()" > Reset </button>
<button class="button" type="button" id="button" onclick="doAjax('index.php/users/createUser', convertFormJson('#createUserForm'));">Create</button>
and
function clearForm() {
document.getElementById('createUserForm').reset();
}
Form:
<?php echo validation_errors(); ?>
<div class="container_12 clearfix leading" >
<div class="grid_12">
<!--<form class= "form has-validation">-->
<?php
$attributes = array('class' => 'form has-validation', 'id' => 'createUserForm');
echo form_open('/users/createUser', $attributes);
?>
<div class="clearfix">
<label for="form-name" class="form-label">First Name <em>*</em></label>
<div class="form-input">
<input type="text" id="form-name" name="firstname" required="required" placeholder="Enter the first name" value="<?php echo set_value('firstname'); ?>" />
</div>
</div>
<div class="clearfix">
<label for="form-name" class="form-label">Last Name <em>*</em></label>
<div class="form-input">
<input type="text" id="form-name" name="lastname" required="required" placeholder="Enter the last name" value="<?php echo set_value('lastname'); ?>"/>
</div>
</div>
<div class="clearfix">
<label for="form-email" class="form-label">Email <em>*</em></label>
<div class="form-input"><input type="email" id="form-email" name="email" required="required" placeholder="A valid email address" value="<?php echo set_value('email'); ?>"/></div>
</div>
<!--
<div class="clearfix">
<label for="form-birthday" class="form-label">Birthdate</label>
<div class="form-input"><input type="date" id="form-birthday" name="date" placeholder="mm/dd/yyyy" value="<?php //echo set_value('date'); ?>" class="date"/></div>
</div>
-->
<div class="clearfix">
<label for="form-username" class="form-label">Username <em>*</em></label>
<div class="form-input">
<?php echo form_error('username'); ?>
<input type="text" id="form-username" name="username" value="<?php echo set_value('username'); ?>" required="required" maxlength="30" placeholder="Alphanumeric (max 30 char.)" />
</div>
</div>
<div class="clearfix">
<label for="form-password" class="form-label">Password <em>*</em></label>
<div class="form-input">
<?php echo form_error('password'); ?>
<input type="password" id="form-password" name="password" value="<?php echo set_value('password'); ?>" maxlength="30" placeholder="Min 8 char. containing a capital, number and/or symbol" />
</div>
</div>
<div class="clearfix">
<label for="form-password-check" class="form-label">Password check <em>*</em></label>
<div class="form-input">
<?php echo form_error('passconf'); ?>
<input type="password" id="form-password-check" name="passconf" value="<?php echo set_value('passconf'); ?>" data-equals="password" maxlength="30" placeholder="Re-enter your password" />
</div>
</div>
<div class="clearfix">
<label for="form-group" class="form-label">Group <em>*</em></label>
<div class="form-input">
<?php
$groupsData[' '] = "--";
$groupJs = 'id="form-group"';
echo form_dropdown('groupId', $groupsData,' ',$groupJs);
?>
</div>
</div>
<div class="clearfix">
<label for="form-language" class="form-label" >Language <em>*</em></label>
<div class="form-input">
<select id="form-language" name="language">
<option value ="" > -- </option>
<option value ="1">English</option>
<option value ="2">French</option>
</select>
</div>
</div>
<div class="clearfix">
<label for="form-timezone" class="form-label" >Timezone <em>*</em></label>
<div class="form-input">
<?php echo timezone_menu('UM5', 'form-timezone'); ?>
</div>
</div>
Please help me out.

Categories