I want to send my html form data to my database which I have created using XAMPP. But, for some reason, the submit button is not working.
This is my html code:-
<form id="contact" action="contact.php" method="post" class="php-email-form">
<div class="row">
<fieldset class="col-md-6 form-group">
<input type="text" name="name2" class="form-control" id="name" placeholder="Your Name" required>
</fieldset>
<fieldset class="col-md-6 form-group mt-3 mt-md-0">
<input type="email" class="form-control" name="email2" id="email" placeholder="Your Email" required>
</fieldset>
</div>
<fieldset class="form-group mt-3">
<input type="text" class="form-control" name="phone2" id="phone" placeholder="Your Phone Number" required>
</fieldset>
<fieldset class="form-group mt-3">
<input type="text" class="form-control" name="subject2" id="subject" placeholder="Subject" required>
</fieldset>
<fieldset class="form-group mt-3">
<textarea class="form-control" name="message2" rows="5" placeholder="Message" required></textarea>
</fieldset>
<fieldset class="text-center">
<button type="submit" name="submit" id="contact-submit">Send Message</button>
</fieldset>
</form>
This is my contact.php code:-
<?php
$name = $_POST['name2'];
$fromEmail = $_POST['email2'];
$phone = $_POST['phone2'];
$subject = $_POST['subject2'];
$message = $_POST['message2'];
//Database Connection
$conn = new mysqli('localhost','root','','contactus');
if ($conn->connect_error) {
die('Connection Failed : '.$conn->connect_error);
} else {
$stmt = $conn->prepare("insert into contact(Name, Email, Phone No., Subject, Message)
values(?,?,?,?,?)");
$stmt->bind_param("ssiss",$name,$fromEmail,$phone,$subject,$message);
$execval = $stmt->execute();
echo $execval;
echo "Sent Successfully";
$stmt->close();
$conn->close();
}
?>
In order for the system to properly process the data field Phone No. (which contains a space character) in db operation, Please enclose it by backticks in your insert query prepared statement
Change
$stmt = $conn->prepare("insert into contact(Name, Email, Phone No., Subject, Message) .....
to
$stmt = $conn->prepare("insert into contact(Name, Email, `Phone No.`, Subject, Message) .....
Your submit button ID is different than what is identified in the form.
<form id="contact" action="contact.php" method="post" class="php-email-form">
<button type="submit" name="submit" id="contact">Send Message</button>
Related
I want to save the input select option in the database table using php. I want to save it in a table with existing attributes
I know how to add it when using alone table for it but I want to add it in existing table with few other attributes
ps first question bear me
<?php
if(isset($_POST['signup'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$password = $_POST['password'];
$department = $_POST['deptlist'];
echo $name;
$connection = mysqli_connect('localhost', 'root', '', 'filetracking');
if($connection) {
echo "We are connected";
} else {
die("Database connection failed");
}
$query = "INSERT INTO faculty(name,email,contactno,password,office) ";
$query .= "VALUES ('$name', '$email', '$phone', '$password', '$department')";
$result = mysqli_query($connection, $query);
if(!$result) {
die('Query FAILED' . mysqli_error($connection));
} else {
echo "Record Create";
}
}
?>
<form method="post" action="signup.php">
<div class="form-group">
<label for="name" class="control-label">Name</label>
<input type="text" name="name" id="Name" class="inputmd textinput form-control required" placeholder="Full Name" style="margin-bottom: 10px" required="true">
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="email" name="email" id="Email" class="inputmd textinput form-control required" placeholder="Email" style="margin-bottom: 10px" required="true">
</div>
<div class="form-group">
<label for="phone" class="control-label">Phone</label>
<input type="number" name="phone" id="Phone" class="inputmd textinput form-control required" placeholder="Phone No" style="margin-bottom: 10px" required="true">
</div>
<div class="form-group">
<label for="password" class="control-label">Password</label>
<input type="password" name="password" id="Password" class="inputmd textinput form-control" placeholder="Password" style="margin-bottom: 10px" required="true">
</div>
<div class="form-group">
<label for="deptlist" class="control-label">Department</label>
<select name="deptlist" form="deptform" class="inputmd form-control" style="margin-bottom: 10px">
<option value="DCSE">Department of Computer Systems Engineering</option>
<option value="DME">Department of Mechanical Engineering</option>
<option value="DEE">Department of Electrical Engineering</option>
<option value="DCS">Department of Computer Science</option>
</select>
<div class="form-group">
<input type="submit" name="signup" id="Singup" value="Sign Up" class="btn btn-info col-md-4 col-md-offset-4">
</div>
</div>
You've mistakenly referred to a form named deptform <select name="deptlist" form="deptform" but there is no such form. This prevents the script from receiving this data.
Here I am using a contact form. When I click on the submit button instead of displaying "message sent successfully" on the same page, it redirects to blank page.
html
<div class="form">
<div style = "font-size:11px; color:#cc0000; margin-top:10px"></div>
<div id="sendmessage" style="display:none">Your message has been sent successfully.</div>
<div id="errormessage"></div>
<form action="contact.php" method="post" role="form" id="myForm" class="contactForm">
<div class="form-group">
<input type="text" name="name" required="required" class="form-control input-text" id="clientname" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="email" class="form-control input-text" name="email" required="required" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="tel" class="form-control input-text" min="0" maxlength="10" required="required" name="phone" id="subject" placeholder="Phone Number" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validation"></div>
</div>
<div class="form-group">
<textarea class="form-control input-text text-area" name="msg" rows="5" required="required" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validation"></div>
</div>
<div class="text-center"><button type="submit" class="input-btn" onclick="sendMsg(2000)">Send Message</button></div>
</form>
</div>
contact.php
<?php
include("config.php");
if(!$db)
echo mysql_error($db);
$name = mysqli_real_escape_string($db, $_POST['name']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$phone = mysqli_real_escape_string($db, $_POST['phone']);
$message = mysqli_real_escape_string($db, $_POST['msg']);
$id = uniqid() . sha1($name);
$sql="INSERT INTO contact (id, Name, Email, Phone, Message) VALUES ('$id', '$name', '$email','$phone','$message');";
if (!mysqli_query($db,$sql))
{
die('Error: ' . mysqli_error($db));
}
?>
contact.js
function sendMsg(duration)
{
var usr = document.getElementById().value;
var email = document.getElementById().value;
var phone = document.getElementById().value;
var msg = document.getElementById().value;
if(usr.length < 1 && email.length < 1 && phone.length < 1 && msg.length < 1)
{
document.getElementById('sendmessage').style.display = "block";
setTimeout(function() {
$('#sendmessage').fadeOut('fast');
}, 5000);
var form = document.getElementById("myForm");
form.reset();
}
}
i've added the js code now.
if you don't want form to redirect to another page, you have two ways, 1. use php_self as a form action or use AJAX(Page will not even refresh with this one), also use prepared statements to prevent sql injections as Jay have suggested above.
Option 1 : PHP_SELF
<?php
include("config.php");
$successMessage = "";
if (isset($_POST['submit'])) {
//validated your inputs fields
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['msg'];
$id = uniqid() . sha1($name);
$sql = "INSERT INTO contact (id, Name, Email, Phone, Message) VALUES ('$id', '$name', '$email','$phone','$message');";
//prepare and bind
$sql = $db->prepare("INSERT INTO contact (id, Name, Email, Phone, Message) VALUES(?,?,?,?,?)");
$sql->bind_param("sssss", $id, $name, $email, $phone, $message);
if ($sql->execute()) {
$message = "Your message has been sent successfully.";
}
$sql->close();
$db->close();
}
?>
<div class="form">
<div style = "font-size:11px; color:#cc0000; margin-top:10px"></div>
<div id="sendmessage"><?php echo $message;?></div>
<div id="errormessage"></div>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" role="form" id="myForm" class="contactForm">
<div class="form-group">
<input type="text" name="name" required="required" class="form-control input-text" id="clientname" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="email" class="form-control input-text" name="email" required="required" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="tel" class="form-control input-text" min="0" maxlength="10" required="required" name="phone" id="subject" placeholder="Phone Number" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validation"></div>
</div>
<div class="form-group">
<textarea class="form-control input-text text-area" name="msg" rows="5" required="required" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validation"></div>
</div>
<div class="text-center"><button type="submit" name="submit" class="input-btn" onclick="sendMsg(2000)">Send Message</button></div>
</form>
</div>
Option 2 : AJax
<div class="form">
<div style = "font-size:11px; color:#cc0000; margin-top:10px"></div>
<div id="sendmessage"></div>
<div id="errormessage"></div>
<form action="contact" method="post" role="form" id="myForm" class="contactForm">
<div class="form-group">
<input type="text" name="name" required="required" class="form-control input-text" id="clientname" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="email" class="form-control input-text" name="email" required="required" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="tel" class="form-control input-text" min="0" maxlength="10" required="required" name="phone" id="subject" placeholder="Phone Number" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validation"></div>
</div>
<div class="form-group">
<textarea class="form-control input-text text-area" name="msg" rows="5" required="required" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validation"></div>
</div>
<div class="text-center"><button type="submit" name="submit" class="input-btn" id="mybtn">Send Message</button></div>
</form>
</div>
<script type="text/javascript">
$('document').ready(function(){
$('#myForm').on('submit',function(e){
e.preventDefault(); //prevent default form submition
var FormData = $('#myForm').serialize();
$.ajax({
type : 'post',
url : 'contact.php';
data : FormData,
dataTYpe : 'json',
encode : true,
beforeSend : function(){
$('$mybtn').html('<span class="glyphicon glyphicon-repeat fast-right-spinner"></span> Sending');
},
success : function(response){
response = JSON.parse(response);
if(response== "ok"){
$('sendmessage').html('Your message has been sent successfully.');
}else{
$('errormessage').html(response);
}
}
});
});
});
</script>
contact.php
<?php
include("config.php");
$message = "";
if (isset($_POST['submit'])) {
//validated your inputs fields
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['msg'];
$id = uniqid() . sha1($name);
$sql = "INSERT INTO contact (id, Name, Email, Phone, Message) VALUES ('$id', '$name', '$email','$phone','$message');";
//prepare and bind
$sql = $db->prepare("INSERT INTO contact (id, Name, Email, Phone, Message) VALUES(?,?,?,?,?)");
$sql->bind_param("sssss", $id, $name, $email, $phone, $message);
if ($sql->execute()) {
$message = "ok";
}else{
$message = "could not send/ insert";
}
echo json_encode($message);
}
?>
Well this can be possible by targeting iframe and hide it.
something like this
<iframe name="myframe" id="frame1" src="thankyou.php" style="display:none"></iframe>
<form action="../thankyou.php" method="post" target="myframe">
<input type="submit" name="DoIt" value="DoIt">
</form>
I have to create a form where answers get sent to the database but when I fill in the form the database is not updating and I have getting the self made error :"something went wrong". Can anyone see anything wrong? Thanks.
Form:
<form id="contact-form" method="post" action="sentEnquiries.php" name="enquiries">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="name">
Name</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span>
</span>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" required="required" /></div>
</div>
<div class="form-group">
<label for="email">
Email Address</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>
</span>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" required="required" /></div>
</div>
<div class="form-group">
<label for="phoneNumber">
Phone Number</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-earphone"></span>
</span>
<input type="tel" class="form-control" id="phoneNumber" name="phone" placeholder="Enter phone number" required="required" /></div>
</div>
<div class="form-group">
<label for="partySize">
Party Size</label>
<input type="number" min="1" max="6" class="form-control" id="partySize" name="partySize" required="required" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="arrivalDate">
Arrival Date</label>
<input type="date" class="form-control" id="arrivalDate" name="arrivalDate" />
</div>
<div class="form-group">
<label for="departureDate">
Departure Date</label>
<input type="date" class="form-control" id="departureDate" name="departureDate"/>
</div>
<div class="form-group">
<label for="name">
Message</label>
<textarea name="message" id="message" class="form-control" rows="9" cols="25" required="required"
placeholder="Message"></textarea>
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-skin pull-right" id="btnContactUs">
Send enquiry</button>
</div>
</div>
</form>
Answers:
<?php
include("conn.php");
$sentName = $_POST['name'];
$sentEmail = $_POST['email'];
$sentPhone = $_POST['phone'];
$sentPartySize = $_POST['partySize'];
$sentArrivalDate = $_POST['arrivalDate'];
$sentDepartureDate = $_POST['departureDate'];
$sentMessage = $_POST['message'];
$insertQuery = "INSERT INTO Enquiries(enquiryID, name, email, phone, partySize, arrivalDate, departureDate, message) VALUES(NULL, '$sentName', '$sentEmail', '$sentPhone, '$sentPartySize', $sentArrivalDate, '$sentDepartureDate', '$sentMessage')";
?>
Further down answers doc:
<div class="descriptions">
<?php
if(mysqli_query($conn, $insertQuery)) {
echo "<p>Thank you for your enquiry.</p>";
mysqli_close($conn);
} else {
echo "<p>Something went wrong.</p>";
mysqli_close($conn);
}
?>
</div>
could you make sure as below :
enquiryID column, is this column is auto increment if not better you set the id become auto increment primary key
For debug purposes, can you echo first in $_POST['name'] then exit; to make sure that the post data from your html is work correctly.
if that not work, you should try to select first from database with simple query, so you will know the connection to database table is work correctly
Here is how i'd do it.
Change yours as required but here's my example:
PDO class:
class form
{
public function sendForm($name, $email, $phone, $party, $date, $depart, $message)
{
$stmt = $this->conn->prepare("INSERT INTO `enquiries` (`name`,`email`,`phone`,`partySize`,`arrivalDate`,`departureDate`,`message`) VALUES (:fname, :email, :phone, :party, :arrive, :depart, :msg)");
$stmt->bindParam(array(':fname' => $name, ':email' => $email, ':phone' => $phone, ':party' => $party, ':arrive' => $date, ':depart' => $depart, ':msg' => $message));
$stmt->execute();
}
}
Then within your form page:
$form = new form();
if (isset($_POST['sendIt']))
{
$form->sendForm($_POST['Fname'],$_POST['email'],$_POST['phone'],$_POST['size'],$_POST['date'],$_POST['depart'],$_POST['message']);
}
Then my basic form without the div tagging so you'll need to tweak slightly:
<form action="" method="post">
<input type="text" name="Fname">
<input type="email" name="email">
<input type="text" name="phone">
<input type="text" name="size">
<input type="text" name="date">
<input type="text" name="depart">
<textarea name="message" id="" cols="30" rows="10"></textarea>
<input type="submit" name="sendIt">
</form>
This is a very basic one with no thank you message or conditions but you can easily add conditions before running the query by doing
if (!empty($var)
{
// do something
} else {
echo "you didnt fill this in...";
Final Edit:
Thank you everyone for the help. I have been trying to write all the code related to connection in index.php rather than submit.php. It is resolved now.
Edit:
I have updated the code based on your feedback.
I am able to get the values to the database now but the thing is it is showing only empty results. here is the updated code.
<form action="submit.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="name" name="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="email" name="email" placeholder="Enter your email address" required>
</div>
</div>
</div> <div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="subject" name="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1">
</div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
PHP Code:
<?php
if (isset($_POST)) {
$conn = mysqli_connect($servername, $username, $password, $db_name);// Establishing Connection with Server
mysqli_set_charset($conn, 'utf8');
if (!$conn) {
die("Database connection failed: " . mysqli_error($conn));
}
else
echo "connected successfully";
//Escaping string, not 100% safe, also consider validating rules and sanitization
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$subject = mysqli_real_escape_string($conn, $_POST['subject']);
$message = mysqli_real_escape_string($conn, $_POST['message']);
$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')");
}
?>
Here is the snapshot of the database
I have a form made using HTML. I want to store the results when i submit the form in the database. The connection was successful but the data is not being stored in the database.
Basically what submit.php does is just sent the text "Successfully submited the form".
Here's my code:
<form action="submit.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="email" placeholder="Enter your email address" required>
</div>
</div>
</div> <div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1">
</div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
PHP code:
$conn = mysqli_connect($servername, $username, $password, $db_name);// Establishing Connection with Server
mysqli_set_charset($conn, 'utf8');
if (!$conn) {
die("Database connection failed: " . mysqli_error($conn));
}
else
echo "connected successfully";
if (isset($_POST['submit'])) {
//Escaping string, not 100% safe, also consider validating rules and sanitization
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$subject = mysqli_real_escape_string($conn, $_POST['subject']);
$message = mysqli_real_escape_string($conn, $_POST['message']);
$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message');");
if ($result) {
$message="successfully sent the query!!";
}
else
{$message="try again!!";}
}
?>
None of your input fields have a name="" attribute, including the button. So none of these fields will be sent in the $_POST array.
Add a name="" attribute like this to all the fields you want sent to PHP
<form action="submit.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="name" name="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="email" name="email" placeholder="Enter your email address" required>
</div>
</div>
</div>
<div class="col-lg-1"></div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="subject" name="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1"></div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" id ="message" name="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1"></div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" class="btn name="submit" btn-primary">Submit</button>
</div>
</div>
</form>
Also in your code in submit.php change this so you see an actual error message if one occurs.
if ($result) {
$message="successfully sent the query!!";
} else {
$message="Insert failed : " . mysqli_error($conn);
}
echo $message;
Although this does assume you are actually showing the $message value somewhere in your code that you have not shown us.
You have to add name attribute to your button element so that if (isset($_POST['submit'])) will be true.
Please change
<button type="submit" class="btn btn-primary">Submit</button>
to
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
or
<input type="submit" name="submit" value="Submit" class="btn btn-primary" />
First of all you must need to provide name attribute for each input tags and button tags for a better approach :
<form action="submit.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="name" name ="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" name ="email" id ="email" placeholder="Enter your email address" required>
</div>
</div>
</div> <div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" class="form-control" name ="subject" id ="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" name ="message" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1">
</div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" name ="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Php Code for Insert data in DB :
$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('".$name."', '".$email."', '".$subject."', '".$message."')");
Try this code :-
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$conn = mysqli_connect($servername, $username, $password, $db_name);// Establishing Connection with Server
mysqli_set_charset($conn, 'utf8');
if (!$conn) {
die("Database connection failed: " . mysqli_error($conn));
}
else
echo "connected successfully";
//Escaping string, not 100% safe, also consider validating rules and sanitization
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$subject = mysqli_real_escape_string($conn, $_POST['subject']);
$message = mysqli_real_escape_string($conn, $_POST['message']);
$result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')");
echo "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')";//die;
if ($result) {
$message="successfully sent the query!!";
}
else
{$message="try again!!";}
}
?>
<form action="index.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="name" class ="col-lg-2 control-label" > Name</label>
<div class="col-lg-7">
<input type="text" class="form-control" name ="name" id ="name" placeholder="Enter your Name" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="email" class ="col-lg-2 control-label" > Email</label>
<div class="col-lg-7">
<input type="text" class="form-control" id ="email" name="email" placeholder="Enter your email address" required>
</div>
</div>
</div> <div class="col-lg-1">
</div>
<div class="form-horizontal" >
<div class="form-group">
<label for="subject" class ="col-lg-2 control-label" > Subject</label>
<div class="col-lg-7">
<input type="text" name="subject" class="form-control" id ="subject" placeholder="Your Subject" required>
</div>
</div>
</div>
<div class="col-lg-1">
</div>
<div class="form-horizontal">
<div class="form-group">
<label for="message" class ="col-lg-2 control-label" > Message</label>
<div class="col-lg-7">
<textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
</div>
</div> <!-- end form -->
<div class="col-lg-1">
</div>
<div class="form-group">
<div class="col-lg-7 col-lg-offset-2">
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</div>
</div>
</form>
I'm having trouble figuring out what is wrong here. I have Bootstrap 3.0.
My form.php
<form role="form" action="../../sgm/leadinsert" method="post">
<div class="form-group">
<label for="fName">First Name</label>
<input type="text" class="form-control" id="fName" name="fName" placeholder="Enter first name" />
</div>
<div class="form-group">
<label for="lName">Last Name</label>
<input type="text" class="form-control" id="lName" name="lName" placeholder="Enter last name" />
</div>
<div class="form-group">
<label for="pNo">Phone Number</label>
<input type="text" class="form-control" id="pNo" name="pNo" placeholder="Ex: 555-555-5555" />
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="example#gmail.com" required />
</div>
<div class="form-group">
<label for="comm">Comments</label>
<textarea class="form-control" rows="3" id="comm" name="comm" placeholder="Enter any comments here; max size 500 chars."></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-default" value="Next"/>
</div>
</form>
Here is my leadinsert.php
<?php
$con=mysqli_connect("localhost","user","pass","db");
$fName = $_POST[ "fName" ];
$lName = $_POST[ "lName" ];
$pNo = $_POST[ "pNo" ];
$email = $_POST[ "email" ];
$comm = $_POST[ "comm" ];
$sql="INSERT INTO Leads (fName, lName, pNo, email, comm)
VALUES ('$fName', '$lName', '$pNo', '$email', '$comm')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con) . print_r($_POST));
}
echo "1 record added";
mysqli_close($con);
?>
Note: on my action= I have my .htaccess set to interpret that. I have never had a problem getting forms to submit to my db, until I implemented bootstrap. It is submitting to my db table, but it is an empty row.
Installing Bootstrap shouldn't play any effect onto how you process forms, since Bootstrap is simply a template of css and javascript. I would place some checks in your insert.php such as issets and see if there is actually any information in the posts. You can also check this in the Chrome Developer Console.
in your action you have this file
action="../../sgm/leadinsert"
while you provide insert.php ?
you should have this
action="path/insert.php"
and also you forgot value = '' in all your inputs , write them like that:
<input type="text" value="" class="form-control" id="fName" name="fName" placeholder="Enter first name" />
Please correct you from action then it will work
missing </div> and the action as already said
<div class="form-group">
<input type="submit" name="submit" class="btn btn-default" value="Next"/>
</div>
</form>