<html>
<body>
<h1>NewsLetter Registration Time! </h1>
<form action='$_SERVER["PHP_SELF"]' method='post'>
Enter the username to be added:
<input type="text" id="nt1" name="username"/>
Enter the corresponding email-id to be added:
<input type="text" id="nt1" name="email_id"/>
<input type="submit"/>
<?php
if(isset($_POST['submit']))
{
echo "<h1 style='color:red;'> Entering Data..... </h1>";
$database=mysqli_connect('localhost','root','','userdetails')
or die("didn't work");
mysqli_query($database,"INSERT INTO userdetails (username,password)VALUES ($_POST[username],$_POST[email_id])");
mysqli_close($database);
echo "<h3 style='color:red;'> Check the database..... </h3>";
}
?>
</body>
</html>
I am not able to access Database using $_SERVER['PHP_SELF'] but the code works if I use some other php script in action. Is this a problem with xampp?
Error:
You are missing php tag in your forms and type=submit in your submit button. Also, check the manual for inserting data in database using php
<html>
<body>
<h1>NewsLetter Registration Time! </h1>
<form action='<?php $_SERVER["PHP_SELF"];?>' method='post'>
Enter the username to be added:
<input type="text" id="nt1" name="username"/>
Enter the corresponding email-id to be added:
<input type="text" id="nt1" name="email_id"/>
<input type="submit" name="submit"/>
<?php
if(isset($_POST['submit']))
{
echo "<h1 style='color:red;'> Entering Data..... </h1>";
$database=mysqli_connect('localhost','root','','userdetails')
or die("didn't work");
mysqli_query($database,"INSERT INTO userdetails (username,password)VALUES ('$_POST[username]','$_POST[email_id]')");
mysqli_close($database);
echo "<h3 style='color:red;'> Check the database..... </h3>";
}
?>
</body>
</html>
Related
I'm not that good with PHP but i need to do a user registration form and this is what I haven done so far
<!doctype html>
<html>
<head>
<title>Register</title>
</head>
<body>
<p>Register | Login</p>
<h3>Formulario de registro</h3>
<form action="" method="POST">
Usuario: <input type="text" name="login"><br />
Contrasena: <input type="password" name="password"><br />
Email: <input type="text" name="email"><br />
Sexo: <input type="text" name="sex"><br />
Edad: <input type="text" name="age"><br />
Telefono: <input type="text" name="phone"><br />
Pais: <input type="text" name="country"><br />
Ciudad: <input type="text" name="city"><br />
Direccion: <input type="text" name="address"><br />
<input type="submit" value="Register" name="submit" />
</form>
<?php
if(isset($_POST["submit"])){
if(!empty($_POST['login']) && !empty($_POST['password'])) {
$login=$_POST['login'];
$password=$_POST['password'];
$email=$_POST['email'];
$sex=$_POST['sex'];
$age=$_POST['age'];
$phone=$_POST['phone'];
$country=$_POST['country'];
$city=$_POST['city'];
$address=$_POST['address'];
$con=mysqli_connect('localhost','root','','registro_usuarios') or die(mysql_error());
$sql="SELECT * FROM usuario ";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result)>=0)
{
echo "I'm in";
$sql="INSERT INTO usuario (login,password,email,sex,age,phone,country,city,address)
VALUES('$login','$password','$password','$email','$sex','$age','$country','$city','$address')";
$result=mysqli_query($con,$sql);
if($result){
echo "Account Successfully Created";
} else {
echo "Failure!";
}
} else {
echo "That username already exists! Please try again with another.";
}
} else {
echo "All fields are required!";
}
}
?>
</body>
</html>
I can see the message "I'm in" in my screen but I'm also get the "Failure" message because it's not inserting anything into the the database. I have been struggling trying to find the error but nothing so far. The error should be around here
$sql="INSERT INTO usuario (login,password,email,sex,age,phone,country,city,address)
VALUES('$login','$password','$password','$email','$sex','$age','$country','$city','$address')";
$result=mysqli_query($con,$sql);
I have checked all the values from my database in case of a typo just in case you guys wonder and there is no typo.
Thanks
You wrong in sql insert.
your sql :
$sql="INSERT INTO usuario (login,password,email,sex,age,phone,country,city,address) VALUES('$login','$password','$password','$email','$sex','$age','$country','$city','$address')";
$result=mysqli_query($con,$sql);
look at the value row doesn't match with the insert fields. You insert twice $password, and no $phone.
If still error, you can add this to know what wrong with your query:
if(mysqli_query($con,$sql))
{
echo 'Success'.'</br>';
}
else
{
echo 'Fail';
echo "Error Description : ".mysqli_error($con);
}
I've got an if statement to check if a variable within the $_SESSION is active and set, and if it is then a message is returned to the user. Here's my header.php:
<?php
$conn = HIDDEN;
session_start();
$username = '';
$_SESSION['username'] = $username;
?>
<header>
<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="style/main.css">
<title>webshop</title>
</header>
<div id="LogIn">
<?php
if (isset($_SESSION['username']))
{
echo "its working";
} else {
?><form class="form1" method="post" action="" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
<?php } ?>
</div>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
}
?>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
// Use the following code to print out the variables.
echo 'Session: '.$_SESSION['username'];
echo '<br>';
echo 'POST: '.$_POST['username'];
}
?>
The first time running, or before the user logs out (to be implemented later), the site should prompt for a username to be entered and then upon refreshing the page the welcome message should be display.
As of right now the code simply returns "it's working" despite no variable in $username existing. The code:
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
// Use the following code to print out the variables.
echo 'Session: '.$_SESSION['username'];
echo '<br>';
echo 'POST: '.$_POST['username'];
}
?>
should print out the variable underneath the welcome message, or nothing at all if it's empty. As of right now, the welcome message "it's working" is displayed always but no variables are in $username. Can anyone tell me why?
Thanks in advance.
$_SESSION['username'] is SET/NULL but is EMPTY you should try !empty() instead if isset(). See below.
<?php
if (!empty($_SESSION['username']))
{
echo "its working";
} else {
?><form class="form1" method="post" action="" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
<?php } ?>
EDIT 2
As to the comment.
IF statement needed to tell if there was a submit if there was don't display the form else display the form. See below Code
<?php
$conn = ""; //HIDDEN kept throwing error whilst I was testing
session_start();
$username = '';
$_SESSION['username'] = $username;
?>
<header>
<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="style/main.css">
<title>webshop</title>
</header>
<div id="LogIn">
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
$_SESSION['username'] = $_POST['username'];
// Use the following code to print out the variables.
echo 'Session: '.$_SESSION['username'];
echo '<br>';
echo 'POST: '.$_POST['username'];
} else {
if (!empty($_SESSION['username']))
{
echo "its working";
} else {
?><form class="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit transparentButton" value="Next" type="submit" name="Submit"/> //removed css selector .
</ul>
<br/>
</fieldset>
</form>
<?php } } ?>
</div>
The isset() checks only whether the variable is set or not and it returns true since it is initialized to null string. Here you have to use !empty().
if (isset($_SESSION['username']) && !empty($_SESSION['username'])) {
}
Just a quick solution is to change
if (isset($_SESSION['username']))
to
if (strlen($_SESSION['username']) > 0)
That will work. Im guessing it because technically u did set username so it isset but if u check the length u know its not empty
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi I made this very simple registration page I have 3 files: welcome.php, registration_form.php, and register.php
This is my code for welcome.php
<html>
<body>
<h1>WELCOME!</h1>
<form action="login.php" method="POST">
<p>Username: <input type="text" name="login_username" value=""> </p>
<p>Password: <input type="password" name="login_password" value=""></p>
<p><input type="submit" value="LOGIN" name="login" size="20"></p>
Register for new account
</body>
<html>
This is my code in registration_form.php
<html>
<body>
<h1>Register here</h1>
<form action="register.php" method="POST">
<p>Username: <input type="text" name="register_username" value=""></p>
<p>Password: <input type="password" name="register_password" value=""></p>
<p>Re-type Password: <input type="password" name="register_repassword" value=""></p>
<p>E-mail Address: <input type="text" name="register_email" value=""></p>
<p>Re-type E-mail Address: <input type="text" name="register_reemail" value=""></p>
<p><input type="submit" value="Register" name="register"></p>
</form>
</body>
<html>
This is my register.php
<html>
<body>
<?php
ob_start();
//=======================database variables
$host="localhost";
$db_username="root";
$db_password="";
$db="forum_members";
$db_table="members";
//=======================connect to database
mysql_connect("$host","$db_username","$db_password") or die("Could not connect to the database!");
mysql_select_db("$db") or die("database not found!");
//form variables
$register_user=$_POST['register_username'];
$register_pass=$_POST['register_password'];
$register_repass = $_POST['register_repassword'];
$register_email=$_POST['register_email'];
$register_reemail=$_POST['register_reemail'];
//protect database from MySQL database
$register_user=stripslashes($register_user);
$register_pass=stripslashes($register_pass);
$register_repass=stripslashes($register_repass);
$register_email=stripslashes($register_email);
$register_reemail=stripslashes($register_reemail);
$register_user=mysql_real_escape_string($register_user);
$register_pass=mysql_real_escape_string($register_pass);
$register_repass=mysql_real_escape_string($register_repass);
$register_email=mysql_real_escape_string($register_email);
$register_reemail=mysql_real_escape_string($register_reemail);
//check required fields
if (empty($register_user) || empty($register_pass)) {
echo "Please fill the required fields";
die();
}
if (empty($register_repass) || empty($register_email)) {
echo "Please fill the required fields";
die();
}
if (empty($register_reemail)) {
echo "Please fill the required fields";
die();
}
//check if username has alphanumeric characters only
if (!preg_match("/^[a-zA-Z0-9_]+$/", $register_user) || !preg_match("/^[a-zA-Z0-9_]+$/", $register_pass)) {
echo "Username and Password can only contain alphanumeric characters";
die();
}
//check username and password minimum length
if (strlen($register_user) < 4) {
echo "Username must be more than 4 characters!";
die();
}
if (strlen($register_pass) < 8) {
echo "Password must be at least 8 characters!";
die();
}
if ($register_pass !== $register_repass) {
echo "Your password did not match";
die();
}
if ($register_email !== $register_reemail) {
echo "Your E-mail address did not match";
die();
}
//check duplicate username
$duplicate_user="SELECT * FROM $db_table WHERE username='$register_user'";
$execute_duplicate=mysql_query($duplicate_user);
$duplicate_result = mysql_num_rows($execute_duplicate);
if ($duplicate_result == 1) {
echo "This username is already used";
die();
}
//create MySQL Query
$query_insert="INSERT INTO $db_table(username, password, email) VALUES ('$register_user', '$register_pass', '$register_email')";
//execute MySQL query
$execute_insert=mysql_query($query_insert);
//$execute_insert=mysql_query($query_insert);
//check inserted data
$check_insert="SELECT * FROM $db_table WHERE username='$register_user'";
$execute_insert1=mysql_query($check_insert);
$verify_insert=mysql_num_rows($execute_insert1);
if ($verify_insert==1) {
echo "Registration Successful! You may now login";
}
else {
echo "Registration Failed!";
}
ob_end_flush();
?>
</body>
</html>
My question is how can I use $_SERVER['PHP_SELF'] so that I can merge registration_form.php and register.php? So that I won't be working on multiple files. My goal is to display "Registration successful! You may now login" or "Registration failed" at the same page(preferably at the top) and when a guest didn't enter any information and clicked the 'register' button' it will go back to the registration form as if nothing happens. I tried to look for an answer in google, but it doesn't work.
P.S. I know there are a lot of flaws in my code, please be good. I'm just starting in studying php.
$_SERVER['PHP_SELF'] is for calling form action on same page. so you should give it in form action.
Now what you have done on register.php, simply put on same page with condition if data posted..like if(isset)
put this code in your registration_form.php
<?php
ob_start();
//=======================database variables
$host="localhost";
$db_username="root";
$db_password="";
$db="forum_members";
$db_table="members";
//=======================connect to database
mysql_connect("$host","$db_username","$db_password") or die("Could not connect to the database!");
mysql_select_db("$db") or die("database not found!");
?>
<h1>Register here</h1>
<?php
// To confirm form is submitted
if(isset($_POST['register']))
{
//form variables
$register_user=$_POST['register_username'];
$register_pass=$_POST['register_password'];
$register_repass = $_POST['register_repassword'];
$register_email=$_POST['register_email'];
$register_reemail=$_POST['register_reemail'];
//protect database from MySQL database
$register_user=stripslashes($register_user);
$register_pass=stripslashes($register_pass);
$register_repass=stripslashes($register_repass);
$register_email=stripslashes($register_email);
$register_reemail=stripslashes($register_reemail);
$register_user=mysql_real_escape_string($register_user);
$register_pass=mysql_real_escape_string($register_pass);
$register_repass=mysql_real_escape_string($register_repass);
$register_email=mysql_real_escape_string($register_email);
$register_reemail=mysql_real_escape_string($register_reemail);
//check required fields
if (empty($register_user) || empty($register_pass)) {
echo "Please fill the required fields";
die();
}
if (empty($register_repass) || empty($register_email)) {
echo "Please fill the required fields";
die();
}
if (empty($register_reemail)) {
echo "Please fill the required fields";
die();
}
//check if username has alphanumeric characters only
if (!preg_match("/^[a-zA-Z0-9_]+$/", $register_user) || !preg_match("/^[a-zA-Z0-9_]+$/", $register_pass)) {
echo "Username and Password can only contain alphanumeric characters";
die();
}
//check username and password minimum length
if (strlen($register_user) < 4) {
echo "Username must be more than 4 characters!";
die();
}
if (strlen($register_pass) < 8) {
echo "Password must be at least 8 characters!";
die();
}
if ($register_pass !== $register_repass) {
echo "Your password did not match";
die();
}
if ($register_email !== $register_reemail) {
echo "Your E-mail address did not match";
die();
}
//check duplicate username
$duplicate_user="SELECT * FROM $db_table WHERE username='$register_user'";
$execute_duplicate=mysql_query($duplicate_user);
$duplicate_result = mysql_num_rows($execute_duplicate);
if ($duplicate_result == 1) {
echo "This username is already used";
die();
}
//create MySQL Query
$query_insert="INSERT INTO $db_table(username, password, email) VALUES ('$register_user', '$register_pass', '$register_email')";
//execute MySQL query
$execute_insert=mysql_query($query_insert);
//$execute_insert=mysql_query($query_insert);
//check inserted data
$check_insert="SELECT * FROM $db_table WHERE username='$register_user'";
$execute_insert1=mysql_query($check_insert);
$verify_insert=mysql_num_rows($execute_insert1);
if ($verify_insert==1) {
echo "Registration Successful! You may now login";
}
else {
echo "Registration Failed!";
}
ob_end_flush();
}
?>
<?php // To call form on same page ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>Username: <input type="text" name="register_username" value=""></p>
<p>Password: <input type="password" name="register_password" value=""></p>
<p>Re-type Password: <input type="password" name="register_repassword" value=""></p>
<p>E-mail Address: <input type="text" name="register_email" value=""></p>
<p>Re-type E-mail Address: <input type="text" name="register_reemail" value=""></p>
<p><input type="submit" value="Register" name="register"></p>
</form>
</body>
<html>
What is it ?
$_SERVER['PHP_SELF'] is not for calling form action on same page but you can use it this way.
$_SERVER['PHP_SELF'] contains the filename of the currently executing script, relative to the document root. this means if you are in http://domain.com/path/to/file.php then
$_SERVER['PHP_SELF'] would be /path/to/file.php.
How to use it?
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
How to merge then ?
$_SERVER['REQUEST_METHOD'] contains the current request method. i.e. GET, POST, PUT
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
// handle your form submition here
}
?>
<html>
...
<!-- show register form -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
How to avoid the submit due to a refresh of the page ?
redirect user to somewhere else.
header("Location: /path/to/somewhere");
but you don't like files, so redirect user to current url.
header("Location: {$_SERVER['PHP_SELF']}");
All in one:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
// handle your form submition here
// everything is ok.
header("Location: {$_SERVER['PHP_SELF']}");
}
?>
<html>
...
<!-- show register form -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Please help me. I written a code but it is not working well.
I want to retrieve data from database and display text fields.
My Code is:
<DOCTYPE html>
<html>
<head><title>Practice</title></head>
<body align="center">
<?php
$con=mysqli_connect("localhost","root","","address_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<form action="1.php" method="post">
Name <br><input type="text" name="name" value="<?php echo $_GET['n']; ?>"><br>
Address 1<br><input type="text" name="address_1" value=""><br>
Address 2<br><input type="text" name="address_2" value=""><br>
Address 3<br><input type="text" name="address_3" value=""><br><br><br>
<input type="submit" name="reset" value="Clear">
<input type="submit" name="submit" value="Submit">
<input type="submit" name="retrieve" value="Retrieve">
</form>
<?php
if (isset($_POST['submit']))
{
$name=$_POST['name'];
$address_1=$_POST['address_1'];
$address_2=$_POST['address_2'];
$address_3=$_POST['address_3'];
if(($name=='')||($address_1=="")||($address_2=="")||($address_3==""))
{
echo "<script>alert('Please fill all fields')</script>";
exit();
}
else
{
mysqli_query($con,"INSERT INTO address_tbl (name,address_1,address_2,address_3)
VALUES ('$name','$address_1','$address_2','$address_3')");
echo "<script>alert('Your record successfull inserted into database...')</script>";
exit();
}
}
if (isset($_POST['retrieve']))
{
$result = mysqli_query($con,"SELECT * FROM address_tbl");
while($row = mysqli_fetch_array($result))
{
$name=$row['name'];echo "<br>";echo "<br>";
$add1=$row['address_1'];echo "<br>";echo "<br>";
$add2=$row['address_2'];echo "<br>";echo "<br>";
$add3=$row['address_3'];echo "<br>";echo "<br>";
echo "<script type='text/javascript'>
window.open('1.php?n=$name','_self'); </script>";
}
}
?>
</body>
</html>
Please help me. give me any hint that I can solve my problem. Thanks
try this ,
mysqli_query($con,"INSERT INTO `1address_tbl` (`name`,`address_1`,`address_2`,`address_3`)
VALUES ('$name','$address_1','$address_2','$address_3')");
it should work fine now. it needs to include ( ` ) around the table names and column name to make sql work correctly. you left them out,
you replace this with yours.
First of all you should have your php in a seperate file called index.php with just php code then create a page called index.html.php in that page use a foreach loop to output data from the database its the most common and practical way of doing what your trying to do .
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
i want only if part to run after submit and display form should not be seen after clicking submit form button
Write your html form code inside else part.
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
} else {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
<?php } ?>
Use exit.
if(isset($_POST['submit']) {
$name = $_POST['name'];
echo "User has submitted the form and entered this name : <b> $name </b>";
exit;
}