Button in bootstrap does not function - php

Submit button does not functioning when I click. All of the code seem to have no errors, but it just does not inserted to my database. I am currently using bootstrap. I don't know what is the error I am having.
index.html
<div class="container">
<div class="col-md-5">
<div class="form-area">
<form role="form">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Schedule Form</h3>
<form name="form2" method="post" action="scheduleform.php">
<div class="form-group">
<input type="text" class="form-control" id="tajuk" name="tajuk" placeholder="Tajuk" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="tarikh" name="tarikh" placeholder="Tarikh" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number" required>
</div>
<div class="form-group">
<textarea class="form-control" type="textarea" id="maklumat" name="maklumat" placeholder="Maklumat" maxlength="140" rows="7"></textarea>
<span class="help-block"><p id="characterLeft" class="help-block ">You have reached the limit</p></span>
</div>
<button><input type="submit" name="submit" class="btn btn-primary pull-right">Submit Form</button>
</form></form>
</div>
</div>
</div>
<!-- /#page-content-wrapper -->
scheduleform.php
<?php
$server = "localhost";
$user = "root";
$pass = "";
$dbname = "kajangdb";
//Creating connection for mysqli
$conn = new mysqli($server, $user, $pass, $dbname);
//Checking connection
if($conn->connect_error){
die("Connection failed:" . $conn->connect_error);
}
$tajuk = mysqli_real_escape_string($conn, $_POST['tajuk']);
$tarikh = mysqli_real_escape_string($conn, $_POST['tarikh']);
$mobile = mysqli_real_escape_string($conn, $_POST['mobile']);
$maklumat = mysqli_real_escape_string($conn, $_POST['maklumat']);
$sql = "INSERT INTO schedule (tajuk, tarikh, mobile, maklumat) VALUES ('$tajuk', '$tarikh', '$mobile', '$maklumat')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>

Issues i found with your code.
1. You don't need to wrap form element with another form element
2.
$sql = "INSERT INTO schedule (tajuk, tarikh, mobile, maklumat) VALUES ('$tajuk', '$tarikh', '$mobile', '$maklumat')";
Here you added extra space after "schedule" and "VALUES" which may created problem. change your code as below.
$sql = "INSERT INTO schedule(tajuk, tarikh, mobile, maklumat) VALUES('$tajuk', '$tarikh', '$mobile', '$maklumat')";

1) Nested forms are definitely error-prone. Use more forms in a page, if
you wish, but never nest them. Here you are using form2 inside
another one. Don't do that, delete the outer one.
2) Your submit button syntax is wrong. Use input OR button.
Instead of:
<button><input type="submit" name="submit" class="btn btn-primary pull-right">Submit Form</button>
use:
<input type="submit" name="submit" value="Submit Form" class="btn btn-primary pull-right" />
or:
<button type="submit" name="submit" class="btn btn-primary pull-right">Submit Form</button>
3) Provide validation on $_POST values. Like:
if (
!isset($_POST['tajuk']) ||
!isset($_POST['tarikh']) ||
!isset($_POST['mobile']) ||
!isset($_POST['maklumat'])
) {
echo 'Error: not all values are valid. Please provide valid values.';
}
$conn = new mysqli($server, $user, $pass, $dbname);
//...
I recommend you to always prepare your sql statements for execution, in order to avoid the SQL injection risks. Use prepare() before querying. See: mysqli::prepare.
Also use exception handling. See please The mysqli_sql_exception class
EDIT 1:
It is not allowed to use paragraphs (<p>) inside spans (<span>). Use other container types, like <div> instead of <span>. So, replace
<span class="help-block">
<p id="characterLeft" class="help-block ">
You have reached the limit
</p>
</span>
with
<div class="help-block">
<p id="characterLeft" class="help-block ">
You have reached the limit
</p>
</div>

Related

Why is the header not working in this code?

The php code to get to the header after executing the queries
<?php
if (isset($_POST['Submit1'])) {
$con = mysqli_connect("localhost:3306", "root", "", "travels");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$fname1 = $_POST['fname'];
$lname1 = $_POST['lname'];
$pnum1 = $_POST['pnum'];
$email1 = $_POST['email'];
$fcode = $_POST['fcode'];
$sql = "insert into customer_info(fname,lname,pnumber,email) values ('$fname1','$lname1','$pnum1','$email1')";
$sql1 = "insert into booking_info(fname,lname,pnumber,email,f_code) values ('$fname1','$lname1','$pnum1','$email1','$fcode')";
$sql2 = "update flight_info set seats_available=seats_available-1 where flight_code='$fcode'";
mysqli_query($con, $sql);
mysqli_query($con, $sql1);
mysqli_query($con, $sql2);
header("Location: Booking_confirm.php");
}
?>
HTML CODE
<div class="container mt-5">
<div class="row">
<div class="col-md-6">
<form action="Booking.php" method="POST">
<div class="form-group">
<label for="fname">First Name: </label>
<input type="text" name="fname" class="form-control" id="fname"/>
</div>
<div class="form-group">
<label for="lname">Last Name: </label>
<input type="text" name="lname" class="form-control" id="lname"/>
</div>
<div class="form-group">
<label for="pnum">Phone Number: </label>
<input type="text" name="pnum" class="form-control" id="pnum"/>
</div>
<div class="form-group">
<label for="email">Email-Address: </label>
<input type="text" name="email" class="form-control" id="email"/>
</div>
<div class="form-group">
<label for="flight">Flight No: </label>
<input type="text" name="fcode" class="form-control" id="fcode"/>
</div>
<form action="Booking_confirm.php" method="POST" target="_blank">
<button type="Submit" name="Submit1" class="btn btn-primary">Book</button>
</form>
</form>
</div>
This is how my code looks. for some reason my header is not working and i am not able to find out why.
Help would be appreciated.
I have tried every possible change to get the header to work but of no use
You have two nested forms, your first form action="Booking.php" contains all fields while the second, nested form action="Booking_confirm.php", only contains the submit button an NO fields.
Replace
<form action="Booking_confirm.php" method="POST" target="_blank">
<button type="Submit" name="Submit1" class="btn btn-primary">Book</button>
</form>
with
<button type="Submit" name="Submit1" class="btn btn-primary">Book</button>
When clicking submit there is no $_POST['fname'], $_POST['lname'] etc.
Edit: you also might need to replace
<form action="Booking.php" method="POST">
with
<form action="Booking_confirm.php" method="POST" target="_blank">
depending on where your "header"/code is placed
Make sure there is not white space or html tags or any output, before the header() function...
upload the entire code, structure so we can help you find the solution

My simple html php form not inserting into database

For some reason, this form doesn't insert into my database.
Html
<form action="../php/register.php" method="post">
<div id ="personal-form">
<h4><b>Personal Details:</b></h4>
<hr>
<div class="form-group">
<label class="sr-only" for="first-name">First name</label>
First Name
<input type="text" name="firstname" placeholder=""
class="form-control" id="firstname">
<button type="submit" class="btn btn-next" id="submit">
Submit
</button>
</center>
</div>
</div>
</form>
php/register.php
<?php
include('connect.php');
if(isset($_POST["submit"])) {
$firstname = $_POST["firstname"];
$stmt = $conn->prepare("INSERT INTO storeowners (firstname) VALUES
(:firstname)");
$stmt->bindParam(':firstname', $firstname);
$stmt->execute();
header("location: next.php");
}
?>
This is connect.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=blaza", $username,
$password);
//set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "success";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
When I click on submit button, it shows the php/register.php page with the message success which is thesame message in the connect.php code if db connection was successful.
I dont know where the problem is cause it doesnt store the firstname to the database and no error was given.
if(isset($_POST["submit"]))
You have no form controls with name=submit so this condition will never be true.
You connect to the database unconditionally, but you never use the connection to do anything.
Add name="submit" to your button.
<form action="../php/register.php" method="post">
<div id ="personal-form">
<h4><b>Personal Details:</b></h4>
<hr>
<div class="form-group">
<label class="sr-only" for="first-name">First name</label>
First Name
<input type="text" name="firstname" placeholder="" class="form-control" id="firstname">
<button name="submit" type="submit" class="btn btn-next" id="submit">Submit</button></center>
</div>
</div>
</form>
Instead of $_POST["submit"] add this
if(isset($_POST["firstname"]))

isset($_POST['submit'] not set after submitting my form

I'm new to php and I'm trying to register a user to a database using a form.
This is the code for the form (using the bootstrap framework):
<form class="form-horizontal" role="form" method="POST" action="connectivity-reserve.php">
<div class="form-group">
<label class="col-sm-2 control-label">Customer's name</label>
<div class="col-sm-6">
<div class="input-group">
<input type="text" class="form-control" id="Customername" name="Customername" placeholder="Customer name">
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Password</label>
<div class="col-sm-6">
<div class="input-group">
<input type="password" class="form-control" id="pwd" name="pwd" placeholder="Password">
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" name ="submit" class="btn btn-primary">Reserve</button>
<button type="button" class="btn btn-default btn-sm"
data-dismiss="modal">Cancel</button>
</div>
</div>
<div class="form-group">
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>Warning:</strong>: Please <a href="tel:+85212345678" class="alert-link">
call</a> us to reserve for more than six guests.
</div>
</div>
</form>
Here is the file (mysqli_connect.php) for connecting to the database. This is working:
<?php
// This provides the information for accessing the database.
// It also creates a connection to MySQL,
// It selects the database, and sets the encoding.
// Set the database access information as constants:
DEFINE ('DB_USER', 'xxxx');
DEFINE ('DB_PASSWORD', 'xxxx');
DEFINE ('DB_HOST', 'xxxx');
DEFINE ('DB_NAME', 'xxxx');
// Make the connection:
$dbcon = #mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );
// Set the encoding...
mysqli_set_charset($dbcon, 'utf8');
The file connectivity-reserve.php is what I'm using to add the user into the database.
Here is the code:
<?php
require ('mysqli_connect.php');
echo "yay";
if(isset($_POST['submit']))
{
$Customername = $_POST['Customername'];
$pwd = $_POST['pwd'];
$q = "INSERT INTO users (user_id, cname, psword, ) VALUES (' ', '$Customername', '$pwd')";
$result = #mysqli_query ($dbcon, $q);
if($result)
{
echo "YOUR REGISTRATION IS COMPLETED...";
}
else
{
echo "nah";
}
}
?>
I'm able to connect to the database since the code echo "yay" is displayed on the browser. The issue is with the if(isset($_POST['submit'])) condition. I thought it should work since my button's type and name are 'submit' on the form:
<button type="submit" name ="submit" class="btn btn-primary">Reserve</button>
Help will be greatly appreciated.
Edit: Thanks guys! It was the , after psword. Also I know the code isn't secure. I'm just starting out with php so that isn't really a priority for me right now.
Replace
<button type="submit" name ="submit" class="btn btn-primary">Reserve</button>
By
<input type="submit" name ="submit" class="btn btn-primary" value="Reserve">
Error in query
$q = "INSERT INTO users (user_id, cname, psword, ) VALUES (' ', '$Customername', '$pwd')";
comma after psword
$q = "INSERT INTO users (user_id, cname, psword ) VALUES (' ', '$Customername', '$pwd')";
Remove the comma from psword. Tested your code in my local db and is working just fine.
INSERT INTO users (cname, psword, ) VALUES ('{$Customername}', '{$pwd}')
You Should remove user_id because its already auto i
There is an error in your query:
Below statement:
$q = "INSERT INTO users (user_id, cname, psword, ) VALUES (' ', '$Customername', '$pwd')";
should be:
$q = "INSERT INTO users (user_id, cname, psword) VALUES (' ', '$Customername', '$pwd')";
There was an additional comma which would fail the query and result in error.
Secondly, instead of checking if(isset($_POST['submit'])), you should check for Customername and pwd
if(isset($_POST['Customername']) && isset($_POST['pwd'])) {

how to insert dynamically added form fields in database using php

I want to add dynamic form fields in the database using PHP. I have used angular to add dynamic form fields. The thing is when I am trying to insert this data into the database only last form field is inserting in the database. SO, I used array and loop to increment and update this form field into the database. but somehow query is not working properly and data is also not inserting into the database. can anybody tell me what is wrong here? I am stuck. Please help. Thanx in advance
Php Code:
<?php
if(isset($_POST['submit_row']))
{
$link = mysqli_connect("localhost", "root", "", "midata");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$camp_name = mysqli_real_escape_string($link, $_REQUEST['camp_name']);
$start_date = mysqli_real_escape_string($link, $_REQUEST['start_date']);
$end_date = mysqli_real_escape_string($link, $_REQUEST['end_date']);
$store = mysqli_real_escape_string($link, $_REQUEST['$store']);
$elements= $mysqli->real_escape_string($_POST['elements']);
$quantity = $mysqli->real_escape_string($_POST['quantity']);
$description = mysqli_real_escape_string($link, $_REQUEST['description']);
for($i=0;$i<count($elements);$i++)
{
if( $elements[$i]!="" && $quantity[$i]!="")
{
$sql = "INSERT INTO create_campaign(camp_name, start_date, end_date,store,elements,quantity, description )
VALUES('$camp_name',' $start_date', '$end_date','$store','$elements[$i]', '$quantity[$i]', '$description')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_query($sql, $link);
}
}
}
?>
HTML Code:
<div class="row col-md-12" ng-app="angularjs-starter" ng-controller="MainCtrl">
<fieldset data-ng-repeat="choice in choices" name="records">
<label for="inputPassword3" class="col-md-1 control-label">Elements</label>
<div class="form-group col-md-3 ">
<input type="text" placeholder="Campaign Name" ng-model="choice.name" class="form-control c-square c-theme input-lg" name="elements[]">
</div>
<label for="inputPassword3" class="col-md-1 control-label">Quantity</label>
<div class="form-group col-md-3" >
<select class="form-control c-square c-border-2px c-theme" name="quantity[]>
<option value="1">100</option>
<option value="2">200</option>
<option value="3">300</option>
<option value="4">400</option>
</select>
</div>
<button type="button" class="btn c-theme-btn c-btn-uppercase btn-lg c-btn-bold c-btn-square" ng-click="addNewChoice()" >add</button>
<button ng-show="$last" ng-click="removeChoice()" class="btn c-theme-btn c-btn-uppercase btn-lg c-btn-bold c-btn-square" >Remove</button>
</fieldset>
</div>
</div>
</div>
<div class="form-group">
<input type="text" placeholder="Description" class="form-control c-square c-theme input-lg" name="description">
</div>
<input class="btn c-theme-btn c-btn-uppercase btn-lg c-btn-bold c-btn-square" value="Submit" type="submit" name="submit_row">
</form>
</div>
You should declare a variable like x=1 and use it inside your array of input like name="input_name[+ x +]"
and increment the variable after each input field i.e x++
You can access those inputs using inputName[1], input_name[2] in your php controller

PHP MySQLi not updating

I have been trying to make a form where I can update two fields one field is going be admin_welcomebox and admin_author and I'm trying update it by the id so here go my code
<div class="col-lg-6">
<div class="panel panel-color panel-inverse">
<div class="panel-heading">
<h3 class="panel-title">Welcome Box Update</h3>
</div>
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "trres";
$password = "sss";
$dbname = "txxxs";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE admin_news SET welcomebox = '{$admin_news}' SET author = {$admin_author} id='{$id}'";
if ($conn->query($sql) === TRUE) {
echo "<h4 class='bg-success'>You have updated admin welcome box.</h4>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}
$conn->close();
}
?>
<div class="panel-body">
<form method="post" action="">
<div class="form-group">
<label for="welcomebox">Welcome Box</label>
<textarea type="text" name="welcomebox" id="welcomebox" placeholder="Enter Your Message" class="form-control"></textarea>
</div>
<div class="form-group">
<label for="author">Author Name</label>
<input type="text" name="author" id="author" placeholder="Author Name" class="form-control" / >
</div>
<div class="form-group text-right m-b-0">
<button class="btn btn-primary waves-effect waves-light" type="submit" name="submit" id="submit">
Update Info
</button>
</div>
</form>
</div>
</div>
</div>
When I try update it just refresh the page nothing else.
Your query has invalid syntax. This is wrong:
UPDATE admin_news SET welcomebox = '{$admin_news}' SET author = {$admin_author} id='{$id}'
The right MySQL syntax for UPDATE is
UPDATE admin_news SET welcomebox = 'value', author = 'value' WHERE id='id'
More in the MySQL manual
Moreover, where do you define $admin_news, $admin_author and $id? I do not see any variable definition in your code.
You didn't define $admin_news ,$admin_author and $id as well. define first.
Try this code :-
<div class="col-lg-6">
<div class="panel panel-color panel-inverse">
<div class="panel-heading">
<h3 class="panel-title">Welcome Box Update</h3>
</div>
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "trres";
$password = "sss";
$dbname = "txxxs";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id=$_POST['id'];
$admin_news=$_POST['welcomebox'];
$admin_author=$_POST['author'];
$sql = "UPDATE admin_news SET welcomebox = '$admin_news', author = $admin_author where id=$id";
if ($conn->query($sql) === TRUE) {
echo "<h4 class='bg-success'>You have updated admin welcome box.</h4>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}
$conn->close();
}
?>
<div class="panel-body">
<form method="post" action="">
<input type="hidden" name="id" value="<?php echo $id; ?>" /> <!-- put here your id -->
<div class="form-group">
<label for="welcomebox">Welcome Box</label>
<textarea type="text" name="welcomebox" id="welcomebox" placeholder="Enter Your Message" class="form-control"></textarea>
</div>
<div class="form-group">
<label for="author">Author Name</label>
<input type="text" name="author" id="author" placeholder="Author Name" class="form-control" / >
</div>
<div class="form-group text-right m-b-0">
<button class="btn btn-primary waves-effect waves-light" type="submit" name="submit" id="submit">
Update Info
</button>
</div>
</form>
</div>
</div>
</div>

Categories