How to show popup on same page after data submission - php

I have this html form
<form class="form" method="post" action="data.php">
<div class="form-group">
<input class="form-control input-lg" type="text" placeholder="Full Name" name="name" id="name" required="">
</div>
<div class="form-group">
<input class="form-control input-lg" type="tel" placeholder="Phone Number" name="phone" id="phone" required="">
</div>
<div class="form-group">
<input class="form-control input-lg" type="email" placeholder="Email ID" name="email" id="email" required="">
</div>
<div class="form-group">
<!--<input class="form-control input-lg option" type="date" name="date" value="Date" id="date" required="">-->
<input type="text" class="form-control input-lg option" name="date" Placeholder="Date" id="datepicker">
</div>
<div class="form-group">
<select class="form-control input-lg option" name="time" id="time" style="padding-left: 10px;color:darkgray;">
<option>Time Slot</option>
<option>10:00-11:00</option>
<option>11:00-12:00</option>
<option>12:00-13:00</option>
<option>13:00-14:00</option>
</select>
</div>
<input class="btn btn-success btn-lg" style="margin-left: 30%; margin-top:10px; padding: 5px 16px;" type="submit" value="BOOK ">
Here is code of data.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "xxxx";
$dbname = "xxxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO data (name,phone,email,date,time)
VALUES ('$_POST[name]', '$_POST[phone]', '$_POST[email]', '$_POST[date]', '$_POST[time]')";
if (mysqli_query($conn, $sql)) {
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
This code shows a pop up on same window with ugly format and after click on ok it shows blank page with ..../data.php url. I want to redirect on same page.
And can i change popup style. I want to show popup in mid of the page.
Please help to solve these issues.. I am newbie

You cannot change the location of the alert-popup. You need to open a popup via window.open.
Use this below updated code to redirect back to your required page.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "xxxx";
$dbname = "xxxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO data (name,phone,email,date,time)
VALUES ('$_POST[name]', '$_POST[phone]', '$_POST[email]', '$_POST[date]', '$_POST[time]')";
if (mysqli_query($conn, $sql))
{
header('Location:index.php?success=1');
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Replace yourpage.php in header function with your actual name of the page.
Rename your index.html to index.php and make these changes to the index file.
<?php
$get = (isset($_GET['success'])) ? $_GET['success'] : '';
if((!empty($get)) && ($get == 1))
{
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
}
?>
<form class="form" method="post" action="data.php">
<div class="form-group">
<input class="form-control input-lg" type="text" placeholder="Full Name" name="name" id="name" required="">
</div>
<div class="form-group">
<input class="form-control input-lg" type="tel" placeholder="Phone Number" name="phone" id="phone" required="">
</div>
<div class="form-group">
<input class="form-control input-lg" type="email" placeholder="Email ID" name="email" id="email" required="">
</div>
<div class="form-group">
<!--<input class="form-control input-lg option" type="date" name="date" value="Date" id="date" required="">-->
<input type="text" class="form-control input-lg option" name="date" Placeholder="Date" id="datepicker">
</div>
<div class="form-group">
<select class="form-control input-lg option" name="time" id="time" style="padding-left: 10px;color:darkgray;">
<option>Time Slot</option>
<option>10:00-11:00</option>
<option>11:00-12:00</option>
<option>12:00-13:00</option>
<option>13:00-14:00</option>
</select>
</div>
<input class="btn btn-success btn-lg" style="margin-left: 30%; margin-top:10px; padding: 5px 16px;" type="submit" value="BOOK ">
I have also modified the data.php file again, so be sure to use the updated code.

Related

Php form not submitting, only get blank page using PDO prepared statement

-php, PDO, prepared statement, html, bootstrap, mysql database-
whenever i hit submit button i get blank php page i.e:profile.php.i see no error or records inserted successfully. please can someone help me debug this code-----------------------------------------------------------------------.
<form action="profile.php" method="GET"> <!--enctype="multipart/form-data"-->
<h1 >Profile </h1>
<div class="form-row">
<div class="form-group col-md-4">
<label>Name :</label>
<input type="text" class="form-control" required="required" name="fname" />
</div>
<div class="form-group col-md-4">
<label>Age :</label>
<input type="number" class="form-control" name="age" />
</div>
<div class="form-group col-md-4">
<label>Gender :</label>
<select class="form-control" name="gender">
<option value="">Select</option>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
<div class="form-group col-md-6">
<label>Email :</label>
<input type="email" class="form-control" name="email" />
</div>
<div class="form-group col-md-6">
<label>Phone No :</label>
<input type="tel" class="form-control" required="" pattern="[0-9]{10}" name="phone" />
</div>
<div class="form-group col-md-6">
<label>Address :</label>
<textarea type="text" class="form-control" name="address"> </textarea>
</div>
<div class="form-group col-md-6">
<label>Comments :</label>
<textarea type="text" class="form-control" name="comments"> </textarea>
</div>
<div class="form-group col-md-6">
<label>Favorite Color :</label>
<br>
<input type="checkbox" value="red" name="color[]" />Red
<input type="checkbox" value="green" name="color[]" />Green
<input type="checkbox" value="blue" name="color[]" />Blue
<input type="checkbox" value="yellow" name="color[]" />Yellow
</div>
<!-- <div class="form-group col-md-6">
<label>Photo :</label>
<input type="file" name="photo" />
</div>-->
<div class="form-group col-md-6">
<input type="submit" name="submit"/>
</div>
</div>
</form>
this database connection code in dbconn.php and php code in profile.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pritesh";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
//$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
<?php
header('Access-Control-Allow-Origin: *');
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(isset($_POST["submit"])){
include("dbconn.php");
$fname=$_REQUEST["fname"];
$age=$_REQUEST["age"];
$email=$_REQUEST["email"];
$phone=$_REQUEST["phone"];
$address=$_REQUEST["address"];
$gender=$_REQUEST["gender"];
$color=$_REQUEST["color"];
$comments=$_REQUEST["comments"];
$sql = "insert into profile (name, age, email, phone, address, gender, color, comments) values (?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param($fname, $age ,$email, $phone, $address, $gender, implode(",",$color), $comments);
$stmt->execute();
$stmt->close();
}
?>
One possible problem is that you are calling accessing $conn without first checking whether it has been correctly created. This is a bad programming practice and can lead to problems. You should add an if clause to check what $conn is before you assume it's an object you can use.
Second similar assumption you make is call
implode(",",$color)
$color being a variable received via $_REQUEST, yet you assume it is an array. Making assumptions such as this, especially from data which comes from a form submission is also a bad habit.
I would start debugging this issue by adding a check to see what $conn is before using it.

Why is sending data to local server using wamp unsuccessful?

<form role="form" action="" method="post">
<div class="form-group">
<label class="sr-only" for="form-username">First Name:</label>
<input type="text" name="firstname" placeholder="First Name" class="form-username form-control" id="form-username" required="">
</div>
<div class="form-group">
<label class="sr-only" for="form-username">Middle Name:</label>
<input type="text" name="middlename" placeholder="Middle Name" class="form-username form-control" id="form-username">
</div>
<div class="form-group">
<label class="sr-only" for="form-username">Last Name:</label>
<input type="text" name="lastname" placeholder="Last Name" class="form-username form-control" id="form-username" required="">
</div>
<div class="form-group">
<label class="sr-only" for="form-username">Address:</label>
<input type="text" name="address" placeholder="Address" class="form-username form-control" id="form-username" required="">
</div>
<div class="form-group">
<label class="sr-only" for="form-username">Phone No:</label>
<input type="text" name="phoneno" placeholder="Phone No" class="form-username form-control" id="form-username" required="">
</div>
<div class="form-group">
<label class="sr-only" for="form-username">Email Id:</label>
<input type="email" name="email" placeholder="Email Id" class="form-username form-control" id="form-username" required="">
</div>
<div class="form-group">
<label class="sr-only" for="form-username">Password:</label>
<input type="password" name="password" placeholder="Password" class="form-username form-control" id="form-username" required="">
</div>
<button type="submit" name="submit" class="btn">Sign in!</button>
</form>
<?php include_once('config.php');
if(isset($_POST['submit'])) {
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$address =$_POST['address'];
$phoneno = $_POST['phoneno'];
$email = $_POST['email'];
$password = $_POST['password'];
//query
$query ="INSERT INTO login(`id`, `first_name`, `middle_name`, `last_name`, `address`, `phone_no`, `email`, `password`) VALUES ('',$firstname','$middlename','$lastname','$address','$phoneno','$email','$password')";
$result = #mysqli_query($query,$link);
if (!$result) {
echo "Data is not Inserted"; //this statement show me all time when submitting the data
}else { echo "Data is Inserted Successful";}}?>
Can u help me what mistake i m doing.
There are several things you can do:
Remove # in front of myslqi_query to show the error message.
If you still see no error message, then make sure that errors will get reported. See display-errors and error_reporting().
Do echo $query right after it is defined. If the query looks right, then copy it, and try running it against your database, using a MySQL client, such as HeidiSQL, or mysql command line.
Correct your english. Do not use "SMS english". It will encourage people to help you.

I want to save the input select option in the database table using php

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.

PHP MySQL Insert into table [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I've followed a tutorial tot he exact code and i can't seem to get this to work
When I submit the form nothing gets put into my table
I can't seem to find the problem, ive changed a bunch of stuff but i cant seem to find the answer.
If anyone can have a look at my code and help me resolve the issue i would be very thankfull
Code
<form class="form" method="post" action="index.php">
<h2>
Post new account...
</h2>
<div class="form-group">
<label for="title">Title</label>
<input required type="text" class="form-control" name="title" placeholder="Enter a title for the account...">
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<label for="server">Server</label>
<input required type="text" class="form-control" name="server" placeholder="Enter Server...">
</div>
<div class="col-md-6">
<label for="price">Price</label>
<input required type="number" class="form-control" name="price" placeholder="Enter Price...">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<label for="Energy">Energy</label>
<input required type="number" class="form-control" name="energy" placeholder="Enter Energy...">
</div>
<div class="col-md-6">
<label for="Stamina">Stamina</label>
<input required type="number" class="form-control" name="stamina" placeholder="Enter Stamina...">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<label for="Jewels">Jewels</label>
<input required type="number" class="form-control" name="jewels" placeholder="Enter Jewels...">
</div>
<div class="col-md-6">
<label for="honor">Stamina</label>
<input required type="number" class="form-control" name="honor" placeholder="Enter Honor...">
</div>
</div>
</div>
<div class="form-group">
<label for="image">Image URL</label>
<input required type="text" class="form-control" name="image" placeholder="Enter the image link...">
</div>
<div class="form-group">
<label for="link">Link URL</label>
<input required type="text" class="form-control" name="link" placeholder="Enter the payment link...">
</div>
<div class="form-group">
<input class="btn btn-primary btn-md" type="submit" name="submit">
</div>
</form>
<?php
if (isset($_POST['submit'])) {
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("We're having problems at the moment, please come back later!");
}
$query = "INSERT INTO accounts (title,price,energy,stamina,jewels,honor,damage,server,image,link) VALUES ('$_POST[title]','$_POST[price]','$_POST[energy]','$_POST[stamina]','$_POST[jewels]','$_POST[honor]','$_POST[damage]','$_POST[server]','$_POST[image]','$_POST[link]')";
mysql_query($query,$conn);
mysql_close($conn);
};
?>
Check your connection:
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
Then check if it is inserting into the database or not. If there's an error, it will let you know as well.
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
Also, don't mix mysql_ with mysqli_. Start using mysqli_ instead of mysql_. mysql_ is deprecated.

PHP code isn't submitting into mysql database

I'm trying to create a login for a website. So far whenever I post to my database doesn't show the submitted information, the only things which are posted is a hashed password.
<form method="post" action="submit.php">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password">
</div>
<div class="form-group">
<label for="pwd2">Re-Password</label>
<input type="password" class="form-control" id="pwd2">
</div>
<div class="form-group">
<input type="submit" class="form-control" id="submit">
</div>
</form>
To submit into this php block
<?php
$servername = "localhost";
$username = "root";
$password = "Password";
$dbname = "DBNAME";
$email = NULL;
$user = NULL;
$pass1 = NULL;
if (isset($_POST['email'])){
$email = $_POST['email'];
}
if (isset($_POST['username'])){
$user = $_POST['username'];
}
if (isset($_POST['password'])){
$pass1 = $_POST['password'];
}
$hash = password_hash($pass1, PASSWORD_BCRYPT);
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Users (email, username, password )
VALUES ('$email', '$user', '$hash')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Your form fields lack name attributes. Without them no values are sent your your script. This is easily testable by doing var_export($_POST).
<form method="post" action="submit.php">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" name="email" id="email">
</div>
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" name="username" id="username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" name="password" id="password">
</div>
<div class="form-group">
<label for="pwd2">Re-Password</label>
<input type="password" class="form-control" name="pwd2" id="pwd2">
</div>
<div class="form-group">
<input type="submit" class="form-control" id="submit">
</div>
</form>
FYI, you are wide open to SQL injections

Categories