i have build following form it works 100%. only one problem. when i open this page a blank data insert to database every time with out press any submit button.
I do not want to use php on seprate file like action="php/doctor.php" I do want to use php on same page of html form.
<?php
$servername = "localhost";
$username = "root";
$password = "";
?>
<?php
if (isset($_POST["dname"]))
{
$dname = $_POST['dname'];
} else {
$dname = null;
}
if (isset($_POST["mobile"]))
{
$mobile = $_POST['mobile'];
} else {
$mobile = null;
}
if (isset($_POST["dusername"]))
{
$dusername = $_POST['dusername'];
} else {
$dusername = null;
}
if (isset($_POST["dpassword"]))
{
$dpassword = $_POST['dpassword'];
} else {
$dpassword = null;
}
try {
$conn = new PDO("mysql:host=$servername;dbname=team", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO doctor (dname, mobile, dusername, dpassword)
VALUES ('$dname', '$mobile', '$dusername', '$dpassword')";
// use exec() because no results are returned
$conn->exec($sql);
echo '<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Success!</strong> Indicates a successful or positive action.
</div>';
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="bootstrap/jquery-1.12.3.min.js"></script>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="style/team.css">
<link rel="stylesheet" href="style/teams.css">
<script src="js/javas.js"></script>
</head>
<body >
<form class="form-horizontal" action="doctor.php" method="post" role="form">
<div class="form-group">
<label class="control-label col-sm-2" >Doctor Name:</label>
<div class="col-sm-8">
<input type="text" class="form-control" name="dname" placeholder="Enter email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" >Mobile:</label>
<div class="col-sm-8">
<input type="text" class="form-control" name="mobile" placeholder="Enter mobile">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">User name:</label>
<div class="col-sm-8">
<input type="text" class="form-control" name="dusername" placeholder="Enter username">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Password:</label>
<div class="col-sm-8">
<input type="password" class="form-control" name="dpassword" placeholder="Enter password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
You can use somethings like this
if(isset($_POST['submit'])) {
// Enter the Code you want to execute after the form has been submitted
} else {
// Display the Form and the Submit Button
}
Here's a complete example:
<?php
if(isset($_POST['SubmitButton'])) { //check if form was submitted
$input = $_POST['inputText']; //get input text
echo "Success! You entered: " . $input;
}
?>
<html>
<body>
<form action="" method="post">
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
Firstly, name your submit button, it will be needed.
<button name="submit" type="submit" class="btn btn-default">Submit</button>
Then apply the logic I stated in comments:
if button is set... if all POSTs are not empty/query.
Note comments in the code below, I want you to do this and you will "learn" from it which is the ultimate goal here.
Sidenote: You can substitute &&
(AND) for ||
(OR).
Consult http://php.net/manual/en/language.operators.logical.php on logic operators.
if (isset($_POST["submit"])){
if (!empty($_POST["dname"]) &&
!empty($_POST["mobile"]) &&
!empty($_POST["dusername"]) &&
!empty($_POST["dpassword"])
)
{
$dname = $_POST["dname"];
// Do the same for the other POST arrays
// Place your query in here
}
}
You can also a ternary operator inside the if (isset($_POST["submit"]))
conditional statement and assigning NULL
as a default if left empty.
Reference:
Example pulled from it:
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
and apply that logic to your code.
Also as stated in comments; use a prepared statement and hash your passwords.
References: