PHP $_POST vars empty with Bootstrap Forms - php

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>

Related

PHP form is falling through the first if($_POST) [duplicate]

I am using Twitter Bootstrap to build a web application. I have a form where user should enter his name and last name and click submit. The problem is, that the $_POST comes back empty.
I am using WAMP on Windows 8 machine.
<?php
if(isset($_POST["send"])) {
print_r($_POST)
} else {
?>
<form class="form-horizontal" action="" method="post">
<div class="control-group"> <!-- Firstname -->
<label class="control-label" for="inputEmail">First name</label>
<div class="controls">
<input type="text" id="inputName" placeholder="First name">
</div>
</div>
<div class="control-group"> <!-- Lastname -->
<label class="control-label" for="inputEmail">last name</label>
<div class="controls">
<input type="text" id="inputLastname" placeholder="Last name">
</div>
</div>
<div class="form-actions">
<button type="submit" name="send" class="btn btn-primary">Submit data</button>
Cancel
</div>
</form>
<?php
}
?>
Add names to the input types like
<input type="text" id="inputLastname" name="inputLastname" placeholder="Last name">
Now check the $_POST variable.
Your inputs don't have name attributes. Set those:
<input type="text" id="inputName" placeholder="First name" name="first_name" />
<input type="text" id="inputLastname" placeholder="Last name" name="last_name" >
Also, look at the way you detect a form submission:
if(isset($_POST['send']))
You check if the submit button is present in the post data. This is a bad idea because in the case of Internet Explorer, the submit button won't be present in the post data if the user presses the enter key to submit the form.
A better method is:
if($_SERVER['REQUEST_METHOD'] == 'POST')
Or
if(isset($_POST['first_name'], $_POST['last_name']))
More info - Why isset($_POST['submit']) is bad.
The problem is that you're checking if $_POST['send'] is set, but it won't be. The "send" field in your form is the button, but the button doesn't have a value, so it won't be included in $_POST.
Hope that helps.
<?php
if(isset($_POST["send"])) {
echo $_POST["inputName"];
echo $_POST["inputLastname"];
}
else {
?>
<form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST">
<div class="control-group"> <!-- Firstname -->
<label class="control-label" for="inputEmail">First name</label>
<div class="controls">
<input type="text" id="inputName" placeholder="First name" name="inputName">
</div>
</div>
<div class="control-group"> <!-- Lastname -->
<label class="control-label" for="inputEmail">last name</label>
<div class="controls">
<input type="text" id="inputLastname" name="inputLastname" placeholder="Last name">
</div>
</div>
<div class="form-actions">
<input type="submit" name="send" class="btn btn-primary">Submit data</button>
Cancel
</div>
</form>
<?php
}
?>
I have notice that using 'name' attribute doesn't work well with Bootstrap Validator for (if(isset($_POST["send"]))).So in case if you like to add validation on your form then I recommend you to follow this way!
Solution
<button type="submit" class="btn btn-primary">Submit data</button>
php
if($_SERVER['REQUEST_METHOD'] == 'POST')
Add the location of the file in action or use:
<form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">

Run php file when submit button in another html file

I would like to work on registration process for which with the help of Google and Youtube i have created "Sign-in & Sing-up" page togather with toggle option however unable to run registration.php file once user provide registration info at login.html file. Codes are as follows :
<form id="login" class="input-group">
<input type="text" class="input-field" placeholder="User Id" required>
<input type="password" class="input-field" placeholder="Enter Password" required>
<input type="checkbox" class="check-box"><span>Remember Password</span>
<button type="submit" class="submit-btn">Sign-In</button>
</form>
<form Id="register" class="input-group">
<input type="text" class="input-field" placeholder="User Id" required>
<input type="email" class="input-field" placeholder="Email Id" required>
<input type="password" class="input-field" placeholder="Enter Password" required>
<input type="password" class="input-field" placeholder="Confirm Password" required>
<input type="phone-number" class="input-field" placeholder="Mobile Number" required>
<input type="checkbox" class="check-box"><span>I agree to the terms & conditions</span>
<button type="submit" class="submit-btn">Sign-Up</button>
</form>
How to execute registration.php file when Sign-up button clicked at login.html file? Same goes for login option too.
Make sure to add method="POST" and action="path/function.php" and name="desiredName"in your forms like this:
<form id="login" class="input-group" method="POST" action="file_path/login.php">
<input type="text" class="input-field" placeholder="User Id" name ="user" required>
<input type="password" class="input-field" placeholder="Enter Password" name="password" required>
<input type="checkbox" class="check-box"><span>Remember Password</span>
<button type="submit" class="submit-btn">Sign-In</button>
</form>
And then in PHP to "catch" the data from the post, you'll use something like this:
$this->getpost['user'];
Or
$_POST['user'];
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute).
The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post").
check here for more details w3schools
<form id="login" class="input-group" method="POST" action="file_path/login.php">
<input type="text" class="input-field" placeholder="User Id" required>
<input type="password" class="input-field" placeholder="Enter Password" required>
<input type="checkbox" class="check-box"><span>Remember Password</span>
<input type="submit" class="submit-btn" value="Sign-In">
</form>
<form Id="register" class="input-group" method="POST" action="file_path/register.php">
<input type="text" class="input-field" placeholder="User Id" required>
<input type="email" class="input-field" placeholder="Email Id" required>
<input type="password" class="input-field" placeholder="Enter Password" required>
<input type="password" class="input-field" placeholder="Confirm Password" required>
<input type="phone-number" class="input-field" placeholder="Mobile Number" required>
<input type="checkbox" class="check-box"><span>I agree to the terms & conditions</span>
<input type="submit" class="submit-btn" value="Sign-Up">
</form>
EDIT according to your comment
replace button with input type="submit"
and place # before the php variable so you wont get undefined error notice(# is used to avoid error notice)
<div class="header">
<h2>Register here</h2>
</div>
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" value="<?php echo #$username; ?>">
</div>
<div class="input-group">
<label>Email</label>
<input type="email" name="email" value="<?php echo #$email; ?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password_1">
</div>
<div class="input-group">
<label>Confirm Password</label>
<input type="password" name="password_2">
</div>
<div class="input-group">
<label>Mobile number</label>
<input type="number" name="mobile" value="<?php echo #$mobile; ?>">
</div>
<div class="input-group">
<input type="submit" class="btn" name="reg_user" value="Sign-Up">
</div>
</form>
create register.php file
and in register.php
<?php
$con = mysqli_connect("localhost", "username", "password", "dbname") or trigger_error("Unable to connect to the database");
if(isset($_POST['reg_user'])){
$name = $_POST['username']; //here "username" is what you defined in the "name" field of input form
//define other variables
//write your own sql query , here is an example
$query = "INSERT INTO table(name) VALUES(?)";
$stmt = mysqli_stmt_init($con);
if(!mysqli_stmt_prepare($stmt,$query)){
echo "Error";
}else{
mysqli_stmt_bind_param($stmt,"s",$name);
mysqli_stmt_execute($stmt);
}
}
?>

Not inserting into database. PHP and mysql

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...";

HTML - form acting weird. Not redirecting to URL and using GET instead of POST

I'm a CS student and I have a DB project due in less than 24hrs! This is really annoying because I just need to forms to access my DB. Anyway, I have this form that works perfectly, while the second form does not work. Instead of posting and directing to the correct URL the second form re-loads the current page with the variables in the URL. Anybody have any ideas?
<form role="form" method="post" action="../controller/AddPerson.php">
<div class="box-body">
<div class="form-group">
<label for="newReservationFirstName"> First name</label>
<input type="text" class="form-control" name="newReservationFirstName" placeholder="Enter first name">
<label for="newReservationLastName"> Last name</label>
<input type="text" class="form-control" name="newReservationLastName" placeholder="Enter last name">
<label for="newReservationPhoneNumber"> Phone Number</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-phone"></i>
</div>
<input type="text" class="form-control" name="newReservationPhoneNum" data-inputmask='"mask": "(999) 999-9999"' data-mask/>
</div><!-- /.input group -->
<label for="newReservationStreetAddress"> Street Address</label>
<input type="text" class="form-control" name="newReservationStreetAddress" placeholder="Enter street address">
<label for="newReservationCity"> City</label>
<input type="text" class="form-control" name="newReservationCity" placeholder="Enter city">
<label for="newReservationState"> State</label>
<select class="form-control" name="newReservationState">
<?php
$result = getTableOrderBy('States','stateName');
while($row = mysql_fetch_array($result)) {
echo "<option value=".$row[stateAbbr].">".$row[stateName]."</option>";
} ?>
</select>
<label for="newReservationZip"> Zip Code</label>
<input type="text" class="form-control" name="newReservationZip" placeholder="Enter zipcode">
</div>
<button type="submit" class="btn btn-success btn-lg">Add New Customer</button>
</div>
</form>
This is the form that doesn't work correctly, both pages exist on the server:
<form role="form" method="post" action="../controller/AddEmployee.php">
<div class="box-body">
<div class="form-group">
<label for="newEmployeeFirstName"> First name</label>
<input type="text" class="form-control" name="newEmployeeFirstName" placeholder="Enter first name">
<label for="newEmployeeLastName"> Last name</label>
<input type="text" class="form-control" name="newEmployeeLastName" placeholder="Enter last name">
<label for="newEmployeePhoneNumber"> Phone Number</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-phone"></i>
</div>
<input type="text" class="form-control" name="newEmployeePhoneNum" data-inputmask='"mask": "(999) 999-9999"' data-mask/>
</div><!-- /.input group -->
<label for="newEmployeeStreetAddress"> Street Address</label>
<input type="text" class="form-control" name="newEmployeeStreetAddress" placeholder="Enter street address">
<label for="newEmployeeCity"> City</label>
<input type="text" class="form-control" name="newEmployeeCity" placeholder="Enter city">
<label for="newEmployeeState"> State</label>
<select class="form-control" name="newEmployeeState">
<?php
$result = getTableOrderBy('States','stateName');
while($row = mysql_fetch_array($result)) {
echo "<option value=".$row[stateAbbr].">".$row[stateName]."</option>";
} ?>
</select>
<label for="newEmployeeZip"> Zip Code</label>
<input type="text" class="form-control" name="newEmployeeZip" placeholder="Enter zipcode">
<p></p>
<p></p>
<label for="newEmployeeFirstName"> Account Username</label>
<input type="text" class="form-control" name="newEmployeeUsername" placeholder="Enter username">
<label for="newEmployeeLastName"> Account Password</label>
<input type="text" class="form-control" name="newEmployeePassword" placeholder="Enter password">
<label for="newEmployeePhoneNumber"> Social Security Number</label>
<input type="text" class="form-control" name="newEmployeeSocial" placeholder="Enter SSN">
<div class="form-group" name="newEmployeePrivileges">
<br>
Privileges :
<select name="newEmployeePrivileges">
<option value="admin">Admin</option>
<option value="admin">Non-Admin</option>
</select>
</div>
<button type="submit" class="btn btn-success btn-lg">Add New Employee</button>
</div>
</div>
</form>
----------------------------------EDIT ----------------------------------------------
I tried making a another really simple form on some extra space and it still didn't work. I have no idea what could be cause it to do this.
<form method="post" action="post" action="../controller/AddEmployee.php">
<button type="submit" class="btn btn-success btn-lg">Add New Employee</button>
</form>
It could be that the button tag you are using to submit the form is causing it behave strangely. Try swapping out the button tag for an input. So:
<form method="post" enctype="multipart/form-data" action="../controller/AddEmployee.php">
<input type="submit" class="btn btn-success btn-lg" name="submit" >Add New Employee</input>
</form>
Also, I noticed you've included two 'action' attributes in your example form :-)

$_POST is empty after form submit

I am using Twitter Bootstrap to build a web application. I have a form where user should enter his name and last name and click submit. The problem is, that the $_POST comes back empty.
I am using WAMP on Windows 8 machine.
<?php
if(isset($_POST["send"])) {
print_r($_POST)
} else {
?>
<form class="form-horizontal" action="" method="post">
<div class="control-group"> <!-- Firstname -->
<label class="control-label" for="inputEmail">First name</label>
<div class="controls">
<input type="text" id="inputName" placeholder="First name">
</div>
</div>
<div class="control-group"> <!-- Lastname -->
<label class="control-label" for="inputEmail">last name</label>
<div class="controls">
<input type="text" id="inputLastname" placeholder="Last name">
</div>
</div>
<div class="form-actions">
<button type="submit" name="send" class="btn btn-primary">Submit data</button>
Cancel
</div>
</form>
<?php
}
?>
Add names to the input types like
<input type="text" id="inputLastname" name="inputLastname" placeholder="Last name">
Now check the $_POST variable.
Your inputs don't have name attributes. Set those:
<input type="text" id="inputName" placeholder="First name" name="first_name" />
<input type="text" id="inputLastname" placeholder="Last name" name="last_name" >
Also, look at the way you detect a form submission:
if(isset($_POST['send']))
You check if the submit button is present in the post data. This is a bad idea because in the case of Internet Explorer, the submit button won't be present in the post data if the user presses the enter key to submit the form.
A better method is:
if($_SERVER['REQUEST_METHOD'] == 'POST')
Or
if(isset($_POST['first_name'], $_POST['last_name']))
More info - Why isset($_POST['submit']) is bad.
The problem is that you're checking if $_POST['send'] is set, but it won't be. The "send" field in your form is the button, but the button doesn't have a value, so it won't be included in $_POST.
Hope that helps.
<?php
if(isset($_POST["send"])) {
echo $_POST["inputName"];
echo $_POST["inputLastname"];
}
else {
?>
<form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST">
<div class="control-group"> <!-- Firstname -->
<label class="control-label" for="inputEmail">First name</label>
<div class="controls">
<input type="text" id="inputName" placeholder="First name" name="inputName">
</div>
</div>
<div class="control-group"> <!-- Lastname -->
<label class="control-label" for="inputEmail">last name</label>
<div class="controls">
<input type="text" id="inputLastname" name="inputLastname" placeholder="Last name">
</div>
</div>
<div class="form-actions">
<input type="submit" name="send" class="btn btn-primary">Submit data</button>
Cancel
</div>
</form>
<?php
}
?>
I have notice that using 'name' attribute doesn't work well with Bootstrap Validator for (if(isset($_POST["send"]))).So in case if you like to add validation on your form then I recommend you to follow this way!
Solution
<button type="submit" class="btn btn-primary">Submit data</button>
php
if($_SERVER['REQUEST_METHOD'] == 'POST')
Add the location of the file in action or use:
<form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">

Categories