how to make html work in php - php

I am trying to get this PHP code working with my html. The PHP alone work but when im trying to use the html form and use PHP code it does not do anything at all. I've been trying to figure this out but without any success. Can somebody please explain what I am doing wrong?
<?php
include_once("config.php");
if(isset($_POST['submit'])){
//Perform the verification
$uname = $_POST['uname'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$pass = $_POST['pass'];
$pass2 = $_POST['pass2'];
if($email == $email2){
if($pass == $pass2){
//All good. Carry on.
$query = $sql->prepare("INSERT INTO login (uname, email, pass) VALUES (?, ?, ?)");
$query->bind_param('sss', $uname, $email, $pass );
$query -> execute();
header("Location: index.html");
}else{
echo "Passwords do not match.<br />";
exit();
}
}else{
echo "Emails do not match.<br /><br />";
exit();
}
$sql->close();
$query->close();
}
?>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div class="container">
<section>
<div id="container_demo" >
<a class="hiddenanchor" id="tologin"></a>
<div id="wrapper">
<div id="login" class="animate form">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h1> Sign up </h1>
<p>
<label for="usernamesignup" class="uname" data-icon="u">Username</label>
<input id="usernamesignup" name="usernamesignup" required="required" type="text"/>
</p>
<p>
<label for="emailsignup" class="email" data-icon="e"> Your email</label>
<input id="emailsignup" name="emailsignup" required="required" type="email"/>
</p>
<p>
<label for="emailsignup" class="email2" data-icon="e">Confirm your email</label>
<input id="emailsignup" name="emailsignup" required="required" type="email"/>
</p>
<p>
<label for="passwordsignup" class="pass" data-icon="p">Your password </label>
<input id="passwordsignup" name="passwordsignup" required="required" type="password"/>
</p>
<p>
<label for="passwordsignup_confirm" class="pass2" data-icon="p">Please confirm your password </label>
<input id="passwordsignup_confirm" name="passwordsignup_confirm" required="required" type="password"/>
</p>
<p class="signin button">
<input type="submit" value="Sign up"/>
</p>
<p class="change_link">
Already a member ?
Go and log in
</p>
</form>
</div>
</div>
</div>
</section>
</div>
</body>
</html>

Your problem is here, you're checking if there is a post variable named submit:
if(isset($_POST['submit'])){
But your form doesn't have any input named submit. I suggest changing to:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
Checking the REQUEST_METHOD is better because it doesn't rely on the submit button being present in the post data.

You did not give your submit button a name.
<input type="submit" value="Sign up"/>
Without a name, the value of the input won't be sent to the server. Basically submitting the form will find that if(isset( and skip the entire code.

You should add the name="submit" attribute to your submit button. Otherwise $_POST['submit'] will never be set, and nothing will happen.

EXTRA TIP : Your HTML textbox name is mismatch with the $_POST variable, I'll give a short example : <input type="text" name="usernamesignup" /> should be $_POST["usernamesignup"], not $_POST["uname"].

Related

Showing error message on correct page in PHP

I have 4 forms in my PHP Project. Index.php will store the user's name and id number then they may click next and it will take them to Form2.php. Form2.php will store some random answers of theirs, Form3.php will do the same as Form2 and Form4.php will store a few details then the user can click submit and the record should save in my DB. The issue I am having is that my ID number field is a unique field, and I want an error to show on Index.php when the user clicks Next if the ID input is the same as one in the DB. Currently, it is showing after the submit button is clicked in the last Form. Is there any way to do this?
Index.php
<body>
<center>
<div class="div2">
<h1>Welcome</h1>
<form action="form2.php" method="post">
<p>
<label for="firstName">Named:</label>
<input size="30" class="rounded-input" type="text" name="name" id="name" autocomplete="off" required>
</p>
<p>
<label for="lastName">S ID:</label>
<input size="30" class="rounded-input" type="text" name="Sid" id="Sid" autocomplete="off" required>
</p>
<input class="btn" type="submit" value="Next" style="float: right;">
</form>
</div>
</center>
</body>
Form2.php:
<?php
session_start();
$_SESSION['name'] = $_POST['name'];
$_SESSION['Sid'] = $_POST['Sid'];
?>
<div class="div2">
<h1>How disappointed would you be if this product ceased to exist?</h1>
<form action="form3.php" method="post">
<input type="radio" style="height:20px; width:20px;" required
name="product_exist_satisfaction"
<?php if (isset($product_exist_satisfaction) && $product_exist_satisfaction == "Very disappointed") echo "checked"; ?>
value="Very disappointed">
<label style="font-size: 20px;"> Very disappointed</label><br />
<input type="radio" style="height:20px; width:20px;" required
name="product_exist_satisfaction"
<?php if (isset($product_exist_satisfaction) && $product_exist_satisfaction == "Mildly disappointed") echo "checked"; ?>
value="Mildly disappointed">
<label style="font-size: 20px;"> Mildly disappointed</label><br />
<input type="radio" style="height:20px; width:20px;" required
name="product_exist_satisfaction"
<?php if (isset($product_exist_satisfaction) && $product_exist_satisfaction == "Not at all") echo "checked"; ?>
value="Not at all">
<label style="font-size: 20px;"> Not at all</label><br />
<input type="button" onclick="history.back()"
value="Previous" style="float: left;">
<input type="submit" value="Next" style="float: right;">
</form>
</div>
Insert.php
<?php
session_start();
?>
<body>
<div class="div2">
<?php
$conn = mysqli_connect("localhost", "root", "", "survey");
if ($conn === false) {
die("ERROR: Could not connect. "
. mysqli_connect_error());
}
$stmt = $conn->prepare('insert into `cus_survey`
( `fullname`, `Sid`, `product_exist_satisfaction`,
`system_battery_runout`, `rank_appliances` )
values (?, ?, ?, ?, ?)');
$stmt->bind_param('sssss', $_SESSION['fullname'], $_SESSION['Sid'],
$_SESSION['product_exist_satisfaction'],
$_SESSION['system_battery_runout'],
$_POST['rank_sequence']);
$stmt->execute();
$msg = ($stmt->affected_rows == 1)
? 'Your survey was captured successfully. Thank You!'!'
: 'Sorry, your S ID is used already, Please use another and resubmit.' . "<h3><a href='/index.php'>Click here to edit your S ID</a></h3>" . mysqli_connect_error();
$stmt->close();
$conn->close();
printf('<h3>%s</h3>', $msg);
?>
</div>
</body>
I have not checked the code, but the basic steps are:
-In your index.php, you could post the index to self, by changing to action="<?php echo $_SERVER['PHP_SELF']; ?>"
-You would then run a select query for the Sid
-If Sid is not already in sql table, then you would redirect to form2.php
-else you have Sid already, then set the error message and then display of the index form with the error message.
<?php
// Start/resume sessions
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
//set your error message to empty string
$error_message = "";
// ensure that you actually have values to check
if ((isset($_POST['name'])) &&(isset($_POST['Sid']))) {
// use select statement to verify that the Sid is not already in table
$sql_check = 'SELECT Sid FROM cus_survey...
//...
//... put your check on the result of select statement
if (Sid not already there) {
// redirect to form 2
header("Location: form2.php");
}else{
// If Sid already used found, populate the error message, which will get displayed in your body
$error_message = "Sorry, your S ID is used already, Please use another and submit.";
}
}
?>
<body>
<center>
<div class="div2">
<?php if ($error_message != ""){
?>
<h1>Oops: <?php echo "{$error_message}"; ?></h1>
<?php
}else{
?>
<h1>Welcome</h1>
<?php
};
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<label for="firstName">Named:</label>
<input size="30" class="rounded-input" type="text" name="name" id="name" autocomplete="off" required>
</p>
<p>
<label for="lastName">S ID:</label>
<input size="30" class="rounded-input" type="text" name="Sid" id="Sid" autocomplete="off" required>
</p>
<input class="btn" type="submit" value="Next" style="float: right;">
</form>
</div>
</center>
</body>

Insert value of multiple checkbox in sqlite database using php pdo

Hi I have multiple check boxes name make and female I want that if a person select make and don't select female it input nothing or false in database now if I don't select example: female it return nothing in female row but it also get a error
Undefined index: Female in /storage/sdcard1/www/3/signup/index.php on line 11
but I don't want this error and I want the if female not select it insert false and don't show error I don't want the selected check box value to be true because it is wrote as on in database
Code index.php
<?php
if(isset($_POST['submit'])){
$conn = new PDO("sqlite: sign.db");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$Email = $_POST['Email'];
$First = $_POST['First'];
$Last = $_POST['Last'];
$Password = $_POST['Password'];
$Male = $_POST['Male'];
$Female =$_POST['Female'];
$Dateofb = $_POST['Dateofb'];
require_once 'imp.php';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css" >
<script src="index.js" ></script>
<title>Survey</title>
</head>
<body>
<form action="" method="post" >
<div class="container" >
<div class="form" >
<input type="email" class="first" id="Email" name="Email" placeholder="Email" required="required">
<input type="text" class="second" id="First" name="First" placeholder="First name" required="required">
<input type="text" class="last" id="Last" name="Last" placeholder="Last name" required="required">
<input type="password" class="pass" name="Password" id="Password" placeholder="Password" required="required">
<div class="day" >
<p class="bd" >Birthday Date:</p>
<input type="date" class="date" id="Dateofb" name="Dateofb" >
</div>
<div>
<div class="malee" >
<input type="checkbox" class="male" id="Male" name="Male">
<p class="mal" >Male</p>
</div>
<div class="femalee" >
<input type="checkbox" class="female" id="Female" name="Female" >
<p class="fem" >Female</p>
</div>
</div>
<div >
<input class="submit" id="submit" type="submit" name="submit" >
</div>
<div class="acc" >
already have account <a href="#" >Login</a>
</div></div>
</div>
</form>
</body>
</html>
Code imp.php
<?php
$sql = "INSERT INTO signup (`Email`, `First`, `Last`, `Password`, `Male`, `Female`, `Dateofb`) VALUES ('$Email', '$First', '$Last', '$Password', '$Male', '$Female', '$Dateofb');";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
$conn->connection = null;
?>
The problem is that only checked boxes are submitted.
You need to check if a box's name is set in $_POST and IF NOT:
manually assign a value you wish to use instead
I.e. use this short hand if/else
$Female = (isset ($_POST['Female'])) ? 1 : 0;

How to move from current PHP page to another PHP page if condition true?

I am working on a web project where I want to move from one PHP page to another Php page if condition true...
In below login PHP page, I am getting username and password using $_POST[]. if both username and password got matched in (if statement) of current PHP login page then, I want to jump to another PHP page(choice.php) specified in header function below after if.
<html>
<body>
<head>
</head>
<form method="post" action="login.php">
<div id="div1">
<h1>welcome to bizdiary</h1>
<div id="div2">
<label >Username</label>
<input id="name" type="text" name="username" value=""
placeholder="username" />
<label >Password</label><input type="text" name="password" value=""
placeholder="password"/>
<input type='submit' name="login" value="login" >
</form>
<?php
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
if($username=='root' && $password=='tiger'){
header( "Location:http://localhost/bizdiary/choice.php" ); die;
}
}
?>
This code should work:
The HTML in top of the file.
Remove the action in your form.
<?php
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
$host = $_SERVER['HTTP_HOST'];
// Put in here the conditional that the request need to accomplish to redirect.
if($username=='root' && $password=='tiger'){
header("Location: http://{$host}/choice.php");
}
}
?>
<html>
<body>
<head>
</head>
<body>
<form method="post">
<div id="div1">
<h1>welcome to bizdiary</h1>
<div id="div2">
<label >Username</label>
<input id="name" type="text" name="username" value="" placeholder="username" />
<label >Password</label>
<input type="text" name="password" value="" placeholder="password"/>
<input type='submit' name="login" value="login" >
</form>
</body>
</html>
You should mysql control. Example
if ($_POST){
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);
if (empty($username) or empty($password)){
echo 'Don't leave blank.';
}else {
$user = mysql_query('SELECT * FROM user WHERE username = "'.$username.'" AND password = "'.$password.'"');
if (mysql_num_rows($user)){
header('Location: asd.php');
}else {
echo 'Didn't find user.';
}
}
}

php session dropped after form submit

Seems, that I can't add password protection to the script: it should allow to login with the pass and to submit data from the form to mysql. Login looks fine, but if I try to press submit, it returns me to login page. Seems, that session is dropped or overwritten, but is not clear, how:
//login area
<?php
$password = "test";
session_start();
$_SESSION['txtPassword']= $_POST['txtPassword'] ;
if ( $_SESSION['txtPassword']!=$password ) {
?>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><label for="txtPassword">Password:</label>
<br /><input type="text" title="Enter your password" name="txtPassword" /></p>
<p><input type="submit" name="Submit" value="Login" /></p>
</form>
<?
}
elseif ( $_SESSION['txtPassword']=$password ) {
echo $_SESSION['txtPassword'] ; // tried to print password, result is correct: test
//my db connection, just in case:
include "config.php";
$connect = mysqli_connect(HOST, USER, PASSWORD, NAME);
// data which should be inserted to db
if
(#$_POST['posted']=='1' $_POST['posted'])) {
$sSQL = "UPDATE users SET user_login='".mysqli_real_escape_string($connect, $_POST['usern'])."',user_pass='".mysqli_real_escape_string($connect, dohashpw($_POST['passw']))."' WHERE ID=1";
mysqli_query($connect, $sSQL) or print(mysql_error());
print ' <div class="container"> <p class="pstype">Password updated! </p>';
...
//input form
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><input type="hidden" name="posted" value="1" />
<div class="col-xs-3">
<label for="ex2">New Username: </label>
<input type="text" class="form-control input-lg" name="usern" >
</div>
<div class="col-xs-3">
<label for="ex2">New Password: </label>
<input type="password" class="form-control input-lg" name="passw" >
</div>
<div class="col-xs-3">
<input type="submit" value="Submit" onclick="<? mysqli_query ($connect, $sSQL);?>; ">
</div>
</form>
I am able to login this page, but when I fill the form and click Submit, I get login area again. If echo $_SESSION show a correct result, I think that it was established, but data are lost after for submit. Could you please help to find my error?
You are assigning and not comparing here :
elseif ( $_SESSION['txtPassword']=$password ) {
this is better
elseif ( $_SESSION['txtPassword']==$password ) {
but thats a bad idea anyway, passwords should not be stored in session variables like this, and you have to hash them once the user submit them and only manipulate and store the hashed passwords in your code and database
<?php
$password = "test";
session_start();
$_SESSION['txtPassword']= $_POST['txtPassword'] ;
if($_SESSION['txtPassword']!=$password ){
?>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ? >">
<p><label for="txtPassword">Password:</label></p>
</br>
<p><input type="text" title="Enter your password" name="txtPassword"/> </p>
<p><input type="submit" name="Submit" value="Login"/></p>
</form>
<?php
}
else{
echo $_SESSION['txtPassword'];
}
?>
i am not understanding why the elseif stands for? you are already checking inside the if condition which both are not equal?.

if(isset($_POST['submit'])) function responds to button but not to input type=submit

Well, as the title already says, I'm not getting it. Probebly doing something very stupid.
This is the form that I'm using. Its not that special. For testing purpose it now has a <button action=submit...> and a <input type=submit...> line.
<div id="cd-login">
<form method="POST" action="index.php" class="cd-form">
<p class="fieldset">
<label class="image-replace cd-email" for="signin-email">E-mail</label>
<input name="login" class="full-width has-padding has-border" type="text" placeholder="Username or E-Mail">
<span class="cd-error-message">Error message here!</span>
</p>
<p class="fieldset">
<label class="image-replace cd-password" for="signin-password">Password</label>
<input name="password" class="full-width has-padding has-border" type="password" placeholder="Password">
<span class="cd-error-message">Error message here!</span>
</p>
<p class="fieldset">
<input type="checkbox" name="remember_me" id="remember-me"/>
<label for="remember_me" onclick="document.getElementById('remember_me').click();">Remember Me</label>
</p>
<p class="fieldset">
<input class="full-width" type="submit" name="submit" value="Login" />
<br />
<button name="submit">Log In</button>
</p>
</form>
It then goes to the PHP code which is alligned above the HTML code:
<?php
require "config.php";
if(isset($_POST['submit'])){
$identification = $_POST['login'];
$password = $_POST['password'];
if($identification == "" || $password == ""){
$msg = array("Error", "Username / Password Wrong !");
}else{
$login = \Fr\LS::login($identification, $password, isset($_POST['remember_me']));
if($login === false){
$msg = array("Error", "Username / Password Wrong !");
}else if(is_array($login) && $login['status'] == "blocked"){
$msg = array("Error", "Too many login attempts. You can attempt login after ". $login['minutes'] ." minutes (". $login['seconds'] ." seconds)");
}
}
}
?>
When I smack my finger on the button it works, but I want to get it to work with an input line.
EDIT: Doesn't work in IE, Mozilla or Chrome. The <input type=submit ...> is clickeble. But smashing the enter-key doesn't work either.
replace
<button name="submit">Log In</button>
by :
<input name="submit" type="submit" value="Log In"/>
and eliminate the other input over :)
When I gave each button/input a unique name, and then checked for that name in POST in your index.php page, I was able to detect which button was pressed.
In principle, it's the name of the item on the form that helps us find it in the POST variables. When troubleshooting forms like this, you can always var dump the POST array and see what the computer is receiving on the processing page.
As an example, a slightly modified copy of your code is presented below. I ran your form in the following html.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div id="cd-login">
<form method="POST" action="index.php" class="cd-form">
<p class="fieldset">
<label class="image-replace cd-email" for="signin-email">E-mail</label>
<input name="login" class="full-width has-padding has-border" type="text" placeholder="Username or E-Mail">
<span class="cd-error-message">Error message here!</span>
</p>
<p class="fieldset">
<label class="image-replace cd-password" for="signin-password">Password</label>
<input name="password" class="full-width has-padding has-border" type="password" placeholder="Password">
<span class="cd-error-message">Error message here!</span>
</p>
<p class="fieldset">
<input type="checkbox" name="remember_me" id="remember-me"/>
<label for="remember_me" onclick="document.getElementById('remember_me').click();">Remember Me</label>
</p>
<p class="fieldset">
<input class="full-width" type="submit" name="submit1" value="Login" />
<br />
<button name="submit2">Log In</button>
</p>
</form>
</body>
</html>
Then, I modified the processing page to catch that name in POST with the isset and provide some printing that will show the change is working. I commented out that config.php because I did not have it.
<?php
//require "config.php";
if(isset($_POST['submit1'])){
print ("<p>Hello from Submit1</p>");
var_dump($_POST);
$identification = $_POST['login'];
$password = $_POST['password'];
if($identification == "" || $password == ""){
$msg = array("Error", "Username / Password Wrong !");
}else{
$login = \Fr\LS::login($identification, $password, isset($_POST['remember_me']));
if($login === false){
$msg = array("Error", "Username / Password Wrong !");
}else if(is_array($login) && $login['status'] == "blocked"){
$msg = array("Error", "Too many login attempts. You can attempt login after ". $login['minutes'] ." minutes (". $login['seconds'] ." seconds)");
}
}
} else {
print ("<p>Submit1 was not recognized.</p>");
var_dump($_POST);
}
?>
When I ran that code, I could show the difference in the two buttons.

Categories