<?php require 'db_connect.php'; ?>
<form id="form1" name="form1" method="post" enctype="multipart/form-data" action="register.php" >
Username :
<input type="text" id="username" class="username" name="username" value="">
<br>
Password :
<input type="password" id="password" class="password" name="password" value="">
<br>
Confirm Password :
<input type="password" id="con_password" class="con_password" name="con_password" value="">
<br>
Phone No. :
<input type="text" id="phoneno" class="phoneno" name="phoneno" value="">
<br>
<input type="submit" id="submit" name="submit" class="submit" value="Register">
</form>
<?php
if(isset($_REQUEST['submit']))
{
$res = "insert into register(username, password, phoneno) value('".$_POST["username"]."','".$_POST["password"]."','".$_POST["phoneno"]."')";
mysqli_query($conn, $res);
if(!empty($_POST)){
echo "Registered!";
}
else {
echo "Try Again!";
}
}
?>
I want to check all $_Post fields check for empty, null or 0 if values of form is not filled show error "try again" and filled show error "registered"? please tell me first is this function is correct or not. if not correct what the correct function.
If you want to validate fields through php, you have to check each field individually. There are some checks you can apply on a data.
if ($_POST['field'] === null) //I would suggest === because it will return false if $_POST['field'] is 0 or empty string
if (empty($_POST['field'])) //to check empty data
if (isset($_POST['field'])) //to check if data exists
If you want to validate your fields like is either string? or digits? or alpha numeric? or all lowercase? or uppercase? There are certain functions which allows you to check field by just calling a single function
Please have a look at this page: CType Functions
You can also use this option. Make all fields required you want to check null or 0, this will make sure that data in field is must before submitting form.
Try changing input field from:
<input type="text" id="username" class="username" name="username" value="">
to:
<input type="text" id="username" class="username" name="username" value="" required >
Also as Rishi said you should also edit query, change values to values
Also 1 tip, you are inserting data in database first and then validating data, according to me it is not good practice, you should first validate data then if all went good then insert it into database, else you will add bad data in database also.
Your final code will be:
<?php require 'db_connect.php'; ?>
<form id="form1" name="form1" method="post" enctype="multipart/form-data" action="register.php" >
Username :
<input type="text" id="username" class="username" name="username" required >
<br>
Password :
<input type="password" id="password" class="password" name="password" required >
<br>
Confirm Password :
<input type="password" id="con_password" class="con_password" name="con_password" required >
<br>
Phone No. :
<input type="text" id="phoneno" class="phoneno" name="phoneno" required >
<br>
<input type="submit" id="submit" name="submit" class="submit" value="Register">
</form>
<?php
if(isset($_POST['submit']))
{
$res = "insert into register(username, password, phoneno) values('".$_POST["username"]."','".$_POST["password"]."','".$_POST["phoneno"]."')";
mysqli_query($conn, $res);
}
?>
Related
Before I start, I want to say that I used the search function but none of the other questions helped me.
I found a code that fits what I need (here) but there is a is when I try to retrieve the values.
// Edit
Assuming that i'am using exactly the some code, how to retrieve the values?
The code above is to show what happen when user click in the plus button.
<form method='post' name='form'>
<input type="text" name="username[0]'>
<input type="password" name="password[0]">
// new added fiels using the jquery
<input type="text" name="username[1]'>
<input type="password" name="password[1]">
</form>
Using a form like this, how to retrieve the values one by one?
<?php if (isset($POST['submit'])) { // what to do here? }?>
I ask this question because I want to store the values on a sql database, and I need the separated values like:
User 1 - user & pass
User 2 - user & pass
To use this on a php function that receive the $user and $pass values.
<form method='post' name='form'>
<input type="text" name="username['username'][]'>
<input type="password" name="password['password'][]">
// new added fiels using the jquery
<input type="text" name="['username'][]'>
<input type="password" name="password['password'][]">
<input type = "submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])){
$postData = $_POST;
for($i = 0; $i < count($postData);$i++){
$username = $postData['username'][$i];
$password = $postData['password'][$i];
}
}
?>
You can echo out each value of the array, or use it any way you want
foreach ($_POST['username'] as $user)
echo $user;
You could use separate arrays for every user - pass you need. For example:
<form method='post' name='form'>
<input type="text" name="user[0]['user']'>
<input type="password" name="user[0]['password']">
// new added fiels using the jquery
<input type="text" name="user[1]['user']>
<input type="password" name="user[1]['password']>
</form>
You read it from php with something like this.
<?php if (isset($_POST['user'])) {
foreach($_POST['user'] as $user){
$username = $user['user'];
$password = $user['password'];
//save into database
}
}?>
And then save into database. You could use php pdo.
http://php.net/manual/en/ref.pdo-mysql.php
You can use different names:
<form method='post' name='form'>
<input type="text" name="username_1">
<input type="password" name="password_1">
// new added fiels using the jquery
<input type="text" name="username_2">
<input type="password" name="password_2">
</form>
<?php
$username_1 = $_POST['username_1'];
....
Or, you can use arrays like this:
<form method='post' name='form'>
<input type="text" name="username[]">
<input type="password" name="password[]">
// new added fiels using the jquery
<input type="text" name="username[]">
<input type="password" name="password[]">
</form>
Your var $_POST['username'] and $_POST['password'] will be arrays. You can iterate the way you want:
foreach($_POST['username'] as $username)
for($i = 0, $c = count($_POST['username']); $i < $c; $i++)
This is pretty straightforward using PHP and HTML. You can look at some examples and documentation here: http://php.net/manual/en/reserved.variables.post.php.
You use an HTML form like this:
<form name="form1" method="post">
<input type="text" name="username1" />
<input type="password" name="password1" />
<input type="text" name="username2" />
<input type="password" name="password2" />
<input type="submit" name="submit" value="Submit" />
</form>
Then you would use PHP to get the values like this:
<?php
if (isset($_POST['submit']))
{
$user1 = $_POST['username1'];
$pass1 = $_POST['password1'];
$user2 = $_POST['username2'];
$pass2 = $_POST['password2'];
}
?>
Of course, use isset() to make sure each username and password is set also, but you get the point.
I am trying to get a username and password from an HTML form using $_POST method,
but i am getting an undefined index error with given input.
the form portion of my index file is as follows
<form class="form-signin" role="form"action="" method="post">
<h2 class="form-signin-heading">title<em class='current'>title</em></h2>
<input id="username" type="username" class="form-control" placeholder="Username" required autofocus>
<input id="password" type="password" class="form-control" placeholder="Password" required>
<input name ="submit" type="submit" value="sign in">
</form>
i also have my php script (called auth.php) included in the top of my index file.
this is my auth.php script code sample
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
echo "auth is called";
if(isset($_POST['submit'])){
echo "submit is pressed";
//this if statement enters when submit is pressed
if(empty($_POST['username']) || empty($_POST['password'])) {
echo "Username or Password is empty";
//this if statement enters too, even with input given
}
else{
echo "YAY";
}
}
?>
The "username or password is empty" statement keeps getting printed, even when I enter in a username and password into the input fields. What is the problem
Missing "name" attribute in input fields, it should be
EDIT 2 : Missing type="text" in username input
<input type="text" name='username' id="username" class="form-control" placeholder="Username" required autofocus>
<input name='password' id="password" type="password" class="form-control" placeholder="Password" required>
Correct your form , 1. Name missing, 2. No datatype with the name of username
<form class="form-signin" role="form" action="" name="loginform" method="post">
<h2 class="form-signin-heading">title<em class='current'>title</em></h2>
<input id="username" name="username" type="text" class="form-control" placeholder="Username" required autofocus>
<input id="password" name="password" type="password" class="form-control" placeholder="Password" required>
<input name="submit" type="submit" value="sign in">
</form>
<input id="username" name="username" type="username" class="form-control" placeholder="Username" required autofocus>
<input id="password" type="password" name="password" class="form-control" placeholder="Password" required>
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
echo "auth is called";
if(isset($_POST['submit'])){
echo "submit is pressed";
//this if statement enters when submit is pressed
if(empty($_POST['username']) || empty($_POST['password'])) {
echo "Username or Password is empty";
//this if statement enters too, even with input given
}
else if(empty($_POST['username']){
echo "Username is empty";
}
else if(empty($_POST['password']){
echo "Password is empty";
}
else{
echo "YAY";
}
}
?>
input type = text and name attr required
<input name='username' id="username" type="text" class="form-control" placeholder="Username" required autofocus>
Check values using print_r() or var_dump if you are not getting the desired output.
This will help you to check which part of your code malfunctions. If you have done so, you would have found that there's something wrong only in your html form content, as you wont be having any field content printed and you would have easily sorted it out...
Set the username input's type to "text":
<input name='username' id="username" type="text" class="form-control" placeholder="Username" required autofocus>
<input name='password' id="password" type="password" class="form-control" placeholder="Password" required>
I have got my validation working but I wanted to only output it, when it has been submitted but isset nor empty seems to work for me for some reason? I am using php 5.5.3 I believe
My code:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="register">
<input type="text" placeholder="Username" maxlength="15" name="username"/><br>
<input type="password" maxlength="15" name="password1"/><br>
<input type="password" maxlength="15" name="password2" /><br>
<input type="text" placeholder="your#email.com" maxlength="25" name="email"/><br>
<input type="text" maxlength="20" name="county" /><br>
<input type="submit" value="Register" name "register"/>
</form>
<?php
//Form Validation
if(empty($_POST['register']))
{
echo "Option A <br>";
}
else
{
echo "Option B <br>";
}
Also, I can't remember how I would enter in the same information for that user if it validates fine?
value="<?php $_POST['stuff'] ?>
<input type="submit" value="Register" name "register"/>
^
You're missing an = there.
It should be:
<input type="submit" value="Register" name="register"/>
It's betterto use isset() instead of empty(), in this case:
if(isset($_POST['register']))
{
echo "Option A <br>";
}
else
{
echo "Option B <br>";
}
I'm not sure what Option A and B are, but with this code, you'll always see Option B when you load the page. The condition in your if block will execute to FALSE and hence the else block will executed. If you want to avoid this, you can use an elseif statement instead of an else.
I am currently learning Web-Design(HTML,PHP,Javascript) and I have created my own site a file called MySite.html.
I have a signin screen which uses POST method to send data to a file called signin.php.
For now it has a simple check which confirms whether data is inputted or not.
But when I enter the Username and Password, the script tells me that I haven't inputted anything.
Here is the code----->
<form name="signin" action="signin.php" method="POST">
<p><input type="text" id="username" placeholder="Enter Username"></p>
<p>
<input type="password" id="pass" placeholder="Password">
<input type="submit" id="sgnin" value="Sign In">
</p>
<p id="small">
<input type="checkbox" id="rem">Remember me.Forgot password?
</p>
<span><p> </p>
</form>
The php script--->
<?php
if(isset($_POST['username']) && isset($_POST['pass']) && !empty($_POST['username']) && !empty($_POST['pass'])){
echo 'Logged In';
}else die("enter something");
?>
Plz Help!
You are not giving name attribute to your form elements give it like this
<input type="text" id="username" placeholder="Enter Username" name="username">
and
<input type="password" id="pass" placeholder="Password" name="pass">
I am trying to POST to a page that has two forms with duplicate name elements. The problem is that one form gets the password value and the other form gets the login value. (I can see this by printing out curl_exec($ch);) I will include my code for the target URL and the formdata. How do I fix this?
// my target url and form data
$target = "http://www.example.com/login";
$formdata = "id=$login&password=$password&Submit=Log In";
Forms:
<form id="login" name="login" method="post" action="login">
<label for="id">LOGIN ID</label> <input type="text" value="" name="id" maxlength="50" size="30"><br>
<label for="password">Password ID</label> <input type="password" name="password" maxlength="12" size="30">
<div align="center"><button class="siteSprite signInSm" value="Log In" name="Submit" type="submit"></button></div>
</form>
<form section="login" id="loginform" name="loginform" action="http://www.example.com/login" method="post">
<input type="text" size="20" value=" Log-in" onfocus="this.value=''" name="id"></td>
<input type="password" value="Password" maxlength="15" size="12" onfocus="this.value=''" name="password">
<input type="submit" class="siteSprite signInSm" value="Sign-In">
</form>
You'll have to do something to indicate which of the two forms got submitted. You can either submit a field with the same name but different values in each one, or use the submit button:
<form ...>
<input type="hidden" name="whichform" value="1" />
<input type="submit" name="Submit" value="form 1" />
</form>
<form ...>
<input type="hidden" name="whichform" value="2" />
<input type="submit" name="Submit" value="form 2" />
</form>
and then
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (($_POST['Submit'] == 'form 1') || ($_POST['whichform'] == '1')) {
.... handle form #1 ....
}
if (($_POST['Submit'] == 'form 2') || ($_POST['whichform'] == '2')) {
.... handle form #1 ....
}
using either method works the same, just pick the one that makes most sense/is easiest and go from there.
$formdata = "id=$login&password=$password&Submit=Sign-In"; might do the trick; note the fact that the second form has a submit button with a value, and the first form has a <button> which won't send a value (or, maybe, sends a different value via script or something)
I just noticed that the submit button doesn't have a name; try passing it with NO submit parameter, i.e.:
$formdata = "id=$login&password=$password