$submit = $_POST['submit'];
$fullname = strip_tags($_POST['fullname']);
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['confirmpassword']);
$date = date ("Y-m-d");
i get **"Notice: Undefined index"**
but when i do this
if(isset($_POST['submit']))
{
$submit = $_POST['submit'];
$fullname = strip_tags($_POST['fullname']);
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['confirmpassword']);
$date = date ("Y-m-d");
}
the undefined index gets fixed but Notice: Undefined variable: submit is my new error why is that???
It's notice because you want to use variable which is not set you can fix it by checking it with isset() function
$submit = null;
if(isset($_POST['submit'])
$submit = $_POST['submit'];
or with short syntax
$submit = isset($_POST['submit']) ? $_POST['submit'] : null;
same applies to other variables passed via $_POST
Try with
$submit = isset ( $_POST ['submit'] ) ? $_POST ['submit'] : null ;
Change:
if(isset($_POST['submit']))
In:
if(isset($_POST) && isset($_POST['submit']))
This is due to no value for 'submit' being passed by POST.
Check if $_POST['submit'] is empty prior to assigning its value to a variable.
Do You have in your form
<input type = 'submit' name = 'submit' />
if you have placed only
<input type = 'submit' />
It will not work
Changed it to start with this
<?php
if(isset($_POST['submit']) != "")
{
$submit = $_POST['submit'];
......
}
?>
Related
I have this piece of code:
session_start();
$_SESSION["username"] = 'bob';
echo $_SESSION["username"];
if ($_SERVER['REQUEST_METHOD']=='POST') {
$username = $_POST['username'];
$password = $_POST['password'];
I want to replace where it says 'bob' with $username instead (which is initialised in line 6).
Is this possible?
Use assigning by reference, requiring only that the $username variable is declared prior to assigning it to $_SESSION['username']:
$username = 'bob';
$_SESSION["username"] =& $username; //mind the &
$username = 'foo';
echo $_SESSION["username"]; // 'foo'
That said, I recommend against an approach like this. Consider using $_SESSION["username"] = $_POST['username'] instead.
No, you can't do that.
But maybe you can change your code to:
session_start();
$_SESSION["username"] = 'bob';
if ($_SERVER['REQUEST_METHOD']=='POST') {
$username = $_POST['username'];
$password = $_POST['password'];
// code
}
echo $_SESSION["username"];
Why is this line printing 1(true) and not the username variable?
$username = isset($_POST['username']);
$username = $connection->real_escape_string($username);
print_r($username);
and how can I use isset to make a mysqli query?
You are assigning 1(true) to $username. isset answer to: 'is that variable set?'.
try with :
if (isset($_POST['username'])){
$username = $_POST['username'];
$username = $connection->real_escape_string($username);
}
if (isset($_POST['username'])) {
$username = $_POST['username'];
}
Normally we do this to check if post variable is set, and if it set, we assign the value to another variable which we will pass to the query, in this case.
This is the first time I have been dumbfounded on what to search for to find my answer. I generally don't ever create a post because there are umpteen thousand posts on the internet with my answer; certainly this is no exception. My problem with finding the answer is, I'm not quite sure what to even search for.
The below code works. That's not the problem. My problem is, what if I wanted to run this one thousand times. Surely I do not need to write this entire expression so many times to get the desired affect do I?
I have a feeling it has to do with an array, but I'm still at a point where I understand arrays one day and the next they are greek.
But anyway, long post for a simple question. Hopefully someone can help me out with this.
This is PHP.
$firstname = $validate->stripExcess($firstname);
$lastname = $validate->stripExcess($lastname);
$email = $validate->stripExcess($email);
$password1 = $validate->stripExcess($password1);
$password2 = $validate->stripExcess($password2);
This is the entire page:
<?php
session_start();
require("../classes/uservalidation.php");
$firstname = $lastname = $email = $password1 = $password2 = "";
if($_SERVER['REQUEST_METHOD'] == "POST") {
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$email = $_POST['email'];
$password1 = $_POST['password'];
$password2 = $_POST['verify'];
}
//create validation object
$validate = new userValidation;
//execite stripExcess method $vaidate
$firstname = $validate->stripExcess($firstname);
$lastname = $validate->stripExcess($lastname);
$email = $validate->stripExcess($email);
$password1 = $validate->stripExcess($password1);
$password2 = $validate->stripExcess($password2);
//
$returnValidate = $validate->register($firstname, $lastname, $email, $password1, $password2);
//check if the variable is an array -- (case when returnValidate has an error)
if (is_array($returnValidate)) {
$url = $returnValidate[0];
$errorMessage = $returnValidate[1];
//echo $url;
//exit();
}else{
$url = $returnValidate;
//echo $url;
//exit();
}
//Set the form values to SESSION vairbale to pass around
$_SESSION['fname'] = $firstname;
$_SESSION['lname'] = $lastname;
$_SESSION['email'] = $email;
$_SESSION['password1'] = $password1;
$_SESSION['password2'] = $password2;
//redirect to the correct page based on validate results
header("Location: " . $url, true, 303);
?>
And the class:
<?php
session_start();
require("../classes/uservalidation.php");
$firstname = $lastname = $email = $password1 = $password2 = "";
if($_SERVER['REQUEST_METHOD'] == "POST") {
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$email = $_POST['email'];
$password1 = $_POST['password'];
$password2 = $_POST['verify'];
}
//create validation object
$validate = new userValidation;
//execite stripExcess method $vaidate
$firstname = $validate->stripExcess($firstname);
$lastname = $validate->stripExcess($lastname);
$email = $validate->stripExcess($email);
$password1 = $validate->stripExcess($password1);
$password2 = $validate->stripExcess($password2);
//
$returnValidate = $validate->register($firstname, $lastname, $email, $password1, $password2);
//check if the variable is an array -- (case when returnValidate has an error)
if (is_array($returnValidate)) {
$url = $returnValidate[0];
$errorMessage = $returnValidate[1];
//echo $url;
//exit();
}else{
$url = $returnValidate;
//echo $url;
//exit();
}
//Set the form values to SESSION vairbale to pass around
$_SESSION['fname'] = $firstname;
$_SESSION['lname'] = $lastname;
$_SESSION['email'] = $email;
$_SESSION['password1'] = $password1;
$_SESSION['password2'] = $password2;
//redirect to the correct page based on validate results
header("Location: " . $url, true, 303);
?>
I don't know what are you want, but maybe:
$values = array("firstname", "lastname", "email", "password1", "password2");
foreach($values AS $value) {
$$value = $validate->stripExcess($$value);
}
Yes. If you have quite a few variables you can use an array of variables. Basically, the array is a very common structure in PHP. When you get values from $_GET and $_POST you also work with arrays. Nested Arrays and Arrays of objects, Nested arrays of objects all of these are widely used in php.
Try to var_dump $_GET and $_POST (when you send some values to the server) and analyze how the are formed when they have values.
I'm quite new to HTTP/PHP coding and I'm having a little trouble with isset in PHP.
This is my current code below, it's supposed to check if the username and password is Admin and Password, if so, it will echo them some info.
However, it doesn't work. There is no errors, it just accepts all usernames and passwords.
$username = isset($_POST['password']);
$password = isset($_POST['username']);
$date = date('d-m-y');
$time = date('h:m:s');
$day = date('l');
if($username == 'Admin' and $password == 'Password')
{ //echo bla bla bla..
isset just checks if a variable is set. Your usercase, on the otherhand, needs to check the actual values:
if(isset($_POST['username']) and
$_POST['username'] == 'Admin' and
isset($_POST['password']) and
$_POST['password'] == 'Password') {
// echo...
In order to use isset() being passed in your variable(s) as you have it now, you need to use a ternary operator.
I.e.:
$username = isset($_POST['password']) ? $_POST['password'] : "default";
Consult Example #3 and others on
http://php.net/manual/en/language.operators.comparison.php
You can read the use isset in php isset , where it appears that isset has a value of true or false . so the value of the variable $username be true or false , so also in the variable $password. So if you will check value of the POST action you can use
if(isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
$date = date('d-m-y');
$time = date('h:m:s');
$day = date('l');
if($username == "Admin" && $password == "Password")
{ //echo bla bla bla..
}
}
try this,
if(isset($_POST['password']) && isset($_POST['username'])){
$username = $_POST['password'];
$password = $_POST['username'];
$date = date('d-m-y');
$time = date('h:m:s');
$day = date('l');
// code here
if($username == 'Admin' and $password == 'Password'){
// Okay
}else{
// not Okay
}
} else {
// error
}
I trying to create a simple user registration, that saves data into a database.
But I have an error I cant defeat.
Its says
Notice: Undefined index: pass_conf in C:\wamp\www\book\reg.php on
line 27
But my pass_config seems to be right.
Any ideas whats the problem ? this is the php code
/// Updating my code, this one works
<?php
$host = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "pagination";
$connection = mysql_connect($host,$dbuser,$dbpass);
$db = mysql_select_db($dbname,$connection);
/*
$name = $_POST["username"];
$pass = $_POST["password"];
$pass_conf = $_POST["pass_conf"];
$email = $_POST["email"];
$ip = $_POST["ip"]; */
$name = isset($_POST['username']) ? $_POST['username'] : 0;
$pass = isset($_POST['password']) ? $_POST['password'] : 0;
$pass_conf = isset($_POST["pass_conf"]) ? $_POST["pass_conf"] : 0; // 0 equals your default off value.
$email = isset($_POST['email']) ? $_POST['email'] : 0;
$ip = isset($_POST['ip']) ? $_POST['ip'] : 0;
if($name == false || $pass == false || $pass_conf == false || $email == false){
echo "Please fill in all the required fields.";
};
if($pass != $pass_conf){
echo "Passwords do not match.";
}else {
$connection = mysql_connect($host,$dbuser,$dbpass);
$db = mysql_select_db($dbname,$connection);
$sql = "INSERT INTO user (username,password,email,ip) VALUES ($name, $pass, $email, $ip)";
$result = mysql_query($sql);
echo "Thank you for your registration to Our Site";
};
?>
I Will add my form.php also since I got a request for it.
<?php
$IP = $_SERVER['REMOTE_ADDR'];
?>
<form name=reg action=reg.php method=post>
Username : <input type=text name=username><br>
Password : <input type=password name=password><br>
Confirm :<input type=password name=pass_conf><br>
Email : <input type=text name=email><br>
<input type=hidden name=ip value='<?php echo $IP ?>'>
<input type=submit value='Register'>
</form>
Thanks
instead of $x = $_POST['x'] use $x = isset($_POST['x']) ? $_POST['x'], null.
Or use:
function post($key, $default = null) {
return isset($_POST[$key]) ? $_POST[$key] : $default;
}
First off, good job on using E_ALL while creating your app! Your "pass_conf" value is probably a checkbox that isn't selected when submitting your form and so no value is ever sent. A simple:
$pass_conf = isset($_POST["pass_conf"]) ? $_POST["pass_conf"] : 0; // 0 equals your default off value.
That message means that the value you are trying to get is not present in the array. So either the value is not filled in or is not passed thru correctly (check the fields name, it has to be exactly the same).
Have you tried:
if (array_key_exists("pass_conf", $_POST))
$pass_conf = mysql_real_escape_string($_POST["pass_conf"]);
else
$pass_conf = null;
$sql =
"INSERT INTO events (username, password, email, ip) " .
"VALUES ('{$name}','{$pass}','{$email}','{$ip}')";
See this question that I asked for my full example and scroll down to how it was answered by user dldnh; it's similar to yours, so maybe it will help you: [link] Undefined index notice