I'm trying to make a registration page using PHP but the values are not being updated in the table and no errors are shown. I'm using a Mac for this. I have set up a XAMPP server, Have a folder named Lab in htdocs folder. Inside the Lab folder I have a file named register.php -
<?php
$db=mysqli_connect("localhost","root","","User Registration");
if(isset($_POST['submit'])){
session_start();
$name=mysqli_real_escape_string($db,$_POST['name']);
$email=mysqli_real_escape_string($db,$_POST['email']);
$password=mysqli_real_escape_string($db,$_POST['password']);
$password=md5($password);
$sql="INSERT INTO User(Name,Email,Password) VALUES('$name','$email','$password')";
mysqli_query($db,$sql);
$_SESSION['message']="You are now logged in";
}
?>
<html>
<head>
<meta charset="utf-8">
<base>
<link href="register.css" rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet'>
<link rel="shortcut icon" type="image/png" href="blogger.ico"/>
<title>Blog</title>
</head>
<body background="background.png">
<div class="topnav">
<a class="active" href="index.html">Home</a>
About
Log In
Register
</div>
<div id="login">
<form action="">
<h3>Register</h3>
<label for="name"><b>Name</b></label>
<br>
<input type="name" placeholder="Name" name="name" id = "name" required>
<br>
<br>
<label for="email"><b>Email</b></label>
<br>
<input type="email" placeholder="Email" name="email" id = "email" required>
<br>
<br>
<label for="email"><b>Password</b></label>
<br>
<input type="password" placeholder="Password" name="password" id="password" required>
<br>
<br>
<label for="password"><b>Re-enter Password</b></label>
<br>
<input type="password" placeholder="Password" name="password" required>
<br>
<p>
<input type="submit" value="Submit" id="submit">
</form>
</div>
</body>
This is my database structure -
Databse
Some modifications necessary:
1) You have not set any method for <form>, so default method is GET and you are trying to access variables with $_POST. So, please make <form method="post">
Explanation:
Case GET method:
<form method="get">
This is default method.
If no method is specified, this is the method.
But, not preferred (especially in registration/login) because all form submitted data is shown on url.
The variables submitted in the form will be accessible through:
$var = $_GET['variableName'];
Case POST method:
<form method="post">
The variables submitted in the form will be accessible through:
$var = $_POST['variableName'];
post method is secure one and variables submitted in this method are not shown on url.
This preferable method.
Now, you can choose exactly which method do you prefer.-
2) Also, PHP variables are case-sensitive. You are using
if(isset($_POST['submit'])){
And form element's name is not there. Make it name="submit"
Related
I am trying to save a Users data from an input field so that it can be displayed later in their profile of a webpage, for example the user inputs data of a cinema(name, address) and can see it later under Saved Restaurants and call up the previously saved information. Can the PHP and HTML code be written together in one .PHP file?
So far I have this:
<html lang>
<head>
<link rel="stylesheet" href="css/addOrEditCinemaPage.css">
</head>
<?php include "php/head.php" ?>
<?php include "php/navigation.php" ?>
<body>
<div class="myForm">
<form>
<h2>Add or Edit a Cinema</h2>
<label for="name"><b>Name of Cinema</b></label>
<input type="text" placeholder="Enter name">
<label for="str"><b>Street</b></label>
<input type="text" placeholder="Enter street">
<label for="nr"><b>Nr.</b></label>
<input type="number" placeholder="Enter Nr.">
<label for="plz"><b>Post Code</b></label>
<input type="number" placeholder="Enter Post Code"><br><br>
<label for="ct"><b>City</b></label>
<input type="text" placeholder="Enter City">
<label for="sta"><b>State</b></label>
<input type="text" placeholder="Enter State">
<label for="descr"><b>Description</b></label><br>
<textarea placeholder="Enter Discription"></textarea>
<div class="imagebutton">
Add Image
</div>
<button type="submit">Save</button>
</form>
</div>
<?php include "php/footer.php" ?>
</body>
</html>```
Can the PHP to save and display to input infomration also be written here?
Yes, you can by setting the 'action' attribute of the form to the same file, and by setting the 'method' attribute to POST.
Instead of using
<form>
use
<form action="<?PHP echo $_SERVER['php_self'];?>" method="POST">
Then, set the 'name' attribute of each input.
For example, Instead of using
<input type="text" placeholder="Enter name">
use
<input type="text" name="name" placeholder="Enter name">
You'll also have to set the 'name' attribute of the submit button to 'submit':
<button type="submit" name="submit">Save</button>
Once you've done that, the PHP code to access the form data would be:
if (isset($_POST['submit'])) {
echo $_POST['name'];
}
send your data with html to another php page
you must use get or post for sending form data to php page
like below
I'm struggling to create a two-paged form using $_SESSION. What I want to achieve is the first page (page1.php) requires the user to enter his/her email address. And the second page (page2.php) requires the user to enter his/her password.
When the user submits page1.php, it takes you to page2.php, where the email address submitted will be printed. But unfortunately, the email address is not printed, as intended.
Please, note I've tried to adopt related resolved threads, but I'm still missing something.
<?php
session_start();
?>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<!doctype html>
<html><head>
<meta charset="utf-8">
<title>2 Step Login - Page 1</title>
<link href="page1.css" rel="stylesheet">
</head>
<body>
<div id="formwrap">
<div id="form_inner">
<div id="logo">
<img src="" alt="logo">
</div>
<div id="email">
</div>
<div id="pwd">
Sign in
</div>
<div id="form">
<form action="page2.php" method="post" enctype="multipart/form-data" name="form">
<?php
//On page 1
$_SESSION['username'] = $var_value;
?>
<input id="username" name="username" type="text" placeholder="Email" autofocus required>
<div id="forgot">No Yet A Member, Register Here</a></div>
<input type="hidden" name="username" value="var_value">
<input id="send" name="submit" type="submit" value="Next">
</form>
</div>
</div>
</div>
</body>
</html>
<?php
session_start();
?>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>2 Step Login - Page 2</title>
<link href="page2.css" rel="stylesheet">
</head>
<body>
<div id="formwrap">
<div id="form_inner">
<div id="logo">
<img src="" alt="logo">
</div>
<div id="email">
<?php
//On age 2
$var_value = $_SESSION['username'];
echo $_SESSION['username'];
?>
</div>
<div id="pwd">
Enter password
</div>
<div id="form">
<form action="login.php" method="post" enctype="multipart/form-data" name="form">
<input id="password" name="password" type="password" placeholder="Password" autofocus>
<div id="chkbx">
<div id="inptch">
<input id="keep_signed_in" name="keep_signed_in" type="checkbox" value="">
</div>
Keep me signed in</div>
<div id="forgot">I Forgot My Password</div>
<div id="different_account">Not A Member, Register Here</div>
<input type="hidden" name="username" value="var_value">
<input type="hidden" name="password" value="var_value">
<input id="send" name="submit" type="submit" value="Sign in">
</form>
</div>
</div>
</div>
</body>
</html>
From your code, it looks like you've skipped the step from using the POST value from the first form before getting to the second.
After a form is submitted, in PHP a $_POST variable contains the values of what was in the form from the keys of the name in the form.
For example <input name="myinput"> in a form set to POST will result in $_POST['myinput'] containing the value from the submit you submit.
The simple way to achieve what you want would be to just post the value from page1.php to page2.php, and then it looks like you handle the login on a login.php. For example:
page1.php
<?php
if (isset($_GET['invalidEmail'])) {
echo "Error: Invalid email address entered.";
}
?>
<form action="page2.php" method="post">
<input type="email" type="email" />
<input type="submit" value="Continue" />
</form>
page2.php
<?php
$email = htmlentities($_POST['email']);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// the email address is not valid, redirect them back to page1.php
header('Location: page1.php?invalidEmail=true');
}
?>
<form action="login.php" method="post">
<input type="hidden" value="<?=$email?>" />
<input type="password" type="password" />
<input type="submit" value="Continue" />
</form>
login.php
<?php
session_start(); // before any HTML is echoed
$email = $_POST['email'];
$password = $_POST['password'];
// ..do whatever you need todo to validate the email and password
// now you can use something like $_SESSION['loggedin'] = $email which will persist across any pages
// you can check `if (isset($_SESSION['loggedin']))` to check if they're signed in
// if the validation fails, redirect them back to page1.php with another error
?>
This way you're not using $_SESSION between pages and a user must go through from entering their email on page 1, to then adding their password on page 2 and finally validating on the login.php file.
The above will get the basics right for you and hopefully allow you to get where you need to go. There are a few extra things you will want to consider (though without having a firm understanding of the above, the rest won't work..)
Validation between pages - from page1 (collecting email) to page2, I've added a line to collect and validate an email address format. You might want to do some extra validation to see if it exists in your records before asking for a password.
Input sanitization - you'll notice I've used htmlentities around the $_POST['email'] - this is to prevent users from injecting extra code on page2
From page2.php, if you visit it directly you're always redirected to page1.php because it requires an email address to be posted to it. You may want to consider what should happen here.
The flow here also means you can't skip between pages and must go through page1 => page2 => login. This is almost to be expected, but setting $_SESSION['email'] = $_POST['email'] would allow you to persist the entered data between pages.
I am trying to post data from html form (html code is inside index.php file) into add_student.php file which is supposed to just print $_POST array using print_r function. But there is always empty array on result.
index.php
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/styles.css">
<link rel="stylesheet" href="css/font-awesome.css">
</head>
<body>
<div class="contact col-md-12" id="get_in_touch">
<div class="col-md-10 col-md-offset-1">
<div class="contact_title">
<h2>
Add a student
</h2>
</div>
<div class="contact_container">
<div class="contact_form col-md-8">
<form action="add_student.php" method="post">
<input name="student_name" type="text" placeholder="FULL NAME" class="input1"/>
<input name="student_email" type="text" placeholder="E-MAIL" class="input1"/>
<input name="password" type="text" placeholder="PASSWORD" class="input1"/>
<input name="major" type="text" placeholder="MAJOR" class="input1"/>
<input name="group" type="text" placeholder="GROUP NUMBER" class="input1"/>
<input name="submit" type="submit" value="ADD STUDENT"/>
</form>
</div>
</div>
</div>
</div>
</body>
add_student.php:
<?php
print_r($_POST);
?>
Why I always get empty array?
It seems that problem is in PHPStorm localhost port. When you just enter in browser the address of index.php it works perfectly. However when I run code from IDE it returns empty array.
Perhaps it will work if you use an include as the following:
include_once add_student.php;
And then use index.php as the form action. IF you later want to proceed to another page, add a header <url> at the bottom of the included script.
Make sure you check if the submit button has been pressed in the add_student.php script isset($_POST['submit'])
Data might get lost in the travel from to page
I am having some difficulty feeding variables from my html form into my php. Would someone mind helping me, I have been at this for hours.
HTML CODE:
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Welcome to Chorelistings - Log in Here</title>
<!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
</head>
<body>
<form action="test.php" class="login">
<h1>Chore Listings </h1>
<input type = "email" id="email" name = "email" class = "login-input" placeholder = "Username (Email)">
<input type="password" name="password " id ="password" class="login-input" placeholder="Password">
<input type="submit" value="Login" class="login-submit">
<p class="create-account">Create Account</p>
<p class="login-help">Forgot password?</p>
</form>
</body>
</html>
My VERY SIMPLE php script:
<?php
$email=$_POST['email'];
echo "hi";
?>
Firstly, try adding method="post" to the <form> tag:
<form action="test.php" class="login" method="post">
Secondly, inside your test.php file, try debugging what was actually sent:
<?php
print_r($_POST);
// or
var_dump($_POST);
// or for more info
print_r($_REQUEST);
?>
Thirdly, try to remove whitespaces when defining your input fields in html:
<input type="email" id="email" name="email" class="login-input" placeholder="Username (Email)">
And finally, add <submit> before closing </form> to your form to actually post the data.
Currently, I see you have links (<a href>) but that won't submit the form.
So your final html should look (simplified) like that:
<form method="post" action="test.php">
<input type="text" name="email">
<submit>
</form>
You have no method on your form, so it's defaulting to GET, which doesn't send $_POST values.
Add method="post" to your <form> tag.
A few things here. Your form does not specify a request method. Add a method attribute to your form tag like so:
<form method="post" action="test.php" class="login">
Next, just a side-note, there's no need for spaces when specifying your attributes, eg:
<input type = "email" id="email" name = "email" class = "login-input" placeholder = "Username (Email)" />
You'll be good to go with:
<input type="email" id="email" name="email" class="login-input" placeholder="Username (Email)" />
A few (less common) things to think about -- make sure your .htaccess is not rewriting any post requests in an unwanted way. If you happen to have any file uploads within your form(s), make sure you specify the enctype attribute on your form eg:
enctype="multipart/form-data"
Best of luck!
I've made a new log in / register template which uses CSS3 and HTML, yet I had a working form, but very basic, before this. So I decided to make another 'form' which should look like this:
http://www.script-tutorials.com/css 3-modal-popups/
Now, I use this form to handle my registration:
<!-- Popup Form #2 -->
<div class="popup">
<h2>Sign Up</h2>
<p>
Please enter your details here.
</p>
<div>
<form action="register.php" action="post">
<label for="username">Username</label>
<input type="text" id="username" placeholder="Max 32 characters" name="username" />
</div>
<div>
<label for="password">Password</label>
<input type="password" id="password" placeholder="Password" name="password" />
</div>
<div>
<label for="lastname">Lastname</label>
<input type="text" id="lastname" placeholder="Lastname" name="lastname" />
</div>
<div>
<label for="email">E-mail</label>
<input type="email" id="email" placeholder="example#hotmail.com" name="email" />
</div>
<input type="submit" value="Join the Community" /> or
</form>
Yet when I test this form, it will say the data is stored (My register.php file comes with a if-statement), yet it displays nothing. (No username, (password still gets hashed / stored, though I'm not sure if it's really handling it), last name and e-mail.
I've checked and double checked the 'name' attributes' value in the register.php file. And those are correct.
Pretty sure you are accessing your form values using $_POST. The problem is that in your form tag you are setting action twice, one of which is correct i.e. action="register.php" and the other action="post" is incorrect.
Try:
<form action="register.php" method="post">
and remember to enclose variables in quotes, for example
$foo = $_POST['foo'];
$bar = $_POST['bar'];
$sql_query = "INSERT INTO my_table (foo, bar) VALUES ('$foo', '$bar');";