Cookies function not saving the Data - php

This code is intended to just take an ID and Pass without any kind of authentication and remember the data if the checkbox were to be checked. I can not figure it why is it not saving the data to cookies.
<?php
if(isset($_POST["chk"],$_POST["id"],$_POST["pass"])) {
$id=$_POST["id"];
$pwd=$_POST["pass"];
if (isset($_POST["chkbx"])){
setcookie("id","$id",time()+3600);
setcookie("pwd","$pwd",time()+3600);
$id=$_COOKIE["id"];
$pwd=$_COOKIE["pwd"];
}
print "Your ID " . $id;
print "Your PASS ". $pwd;
}
?>
<html>
<head>
<title>
Remember Me
</title>
</head>
<body>
Please Enter ID and PASS
<form method="post" >
Enter ID
<input type="text" name="id" />
Enter PASS
<input type="text" name="pass" />
<input type="submit" value="submit" /><br>
<input type="checkbox" name="chkbx" />Remember Me
<input type="hidden" name="chk" value="true" />
</form>
</body>
</html>

You code is correct but it needs to clean it up
in this part I add a condition to check is there is anything first in the $_COOKIE
before print it
if(isset($_COOKIE['id']) && isset($_COOKIE['pwd'])){
print "Your ID: " . $_COOKIE['id'] . '<br>';
print "Your PASS: ". $_COOKIE['pwd'] . '<br>';
}
your code will be like this
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$id = $_POST["id"];
$pwd = $_POST["pass"];
if (isset($_POST["chkbx"])){
setcookie("id", $id ,time()+3600);
setcookie("pwd", $pwd, time()+3600);
}
}
if(isset($_COOKIE['id']) && isset($_COOKIE['pwd'])){
print "Your ID: " . $_COOKIE['id'] . '<br>';
print "Your PASS: ". $_COOKIE['pwd'] . '<br>';
}
?>
<html>
<head>
<title>
Remember Me
</title>
</head>
<body>
Please Enter ID and PASS
<form method="post" >
Enter ID
<input type="text" name="id" value="<?= isset($_COOKIE['id'])? $_COOKIE['id']: '' ?>" />
Enter PASS
<input type="text" name="pass" value="<?= isset($_COOKIE['pwd'])? $_COOKIE['pwd']: '' ?>" />
<input type="submit" value="submit" /><br>
<input type="checkbox" name="chkbx" />Remember Me
<input type="hidden" name="chk" value="true" />
</form>
</body>
</html>

Related

How to handle multiple php forms using one php page

I'm a student and I know nothing about PHP. but I have to do one of my assignment using PHP.
Here is the problem which I faced.
On my index page, there are 3 links that direct to 3 different forms. when the user chooses one form, then fill and submit it result.php file shows the output using values that the user enters in the form.
all the 3 forms should germinate its result using the same result.php file.
I cannot figure out how to generate the result page by identifying which form the user selects.
Here is my code,
form1.php
<!DOCTYPE html>
<html>
<head>
<title>PHP form handling</title>
</head>
<body>
<form name="form1" action="result.php" method="post">
<label for="pullDownMenu">Title</label>
<select name="pullDownMenu" id="pullDownMenu" size="1">
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Mrs">Mrs</option>
<option value="Rev">Rev</option>
</select>
<p>Name: <input type="text" name="firstname" value="" /></p>
<p>Reg No: <input type="text" name="lastname" value="" /></p>
<p>Email Addr: <input type="text" name="Email" value="" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
form2.php
<!DOCTYPE html>
<html>
<head>
<title>form 2</title>
</head>
<body>
<form name="form2" action="result.php" method="post">
<p>Registrationa no: <input type="text" name="RegNO" value="" /></p>
<p>NIC number <input type="text" name="NIC" value=""></p>
<p>Telephone number: <input type="text" name="Telephone" value="" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
I tried the result.php file, but it didn't work. here is the result.php
<!DOCTYPE html>
<html>
<head>
<title>PHP demo</title>
</head>
<body>
<?php
if(!empty($_POST['form1'])){
$title=$_POST['pullDownMenu'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$Email = $_POST['Email'];
echo"<h1>student information</h1>";
echo'title is : ' . $title . '</br>';
echo 'first name is : '. $firstname . '</br>';
echo 'lastname is : '.$lastname;
}
if (!empty($_POST['form2'])) {
$regNo = $_POST['RegNO'];
$NIC = $_POST['NIC'];
$tel = $_POST['Telephone'];
echo "<p>Following details are saved to database</p>";
echo 'reg No\t:\t' . $regNo. '</br>';
echo 'NIC\t:\t' . $NIC. '</br>';
echo 'Tel No\t:\t' . $tel. '</br>';
}
?>
</body>
</html>
Consider using isset() to check for a specific variable. It can be better then checking with empty().
<!DOCTYPE html>
<html>
<head>
<title>PHP demo</title>
</head>
<body>
<?php
if(isset($_POST['form1'])){
echo "<h1>student information</h1>\r\n";
echo "title is : $_POST['pullDownMenu']<br />\r\n";
echo "first name is : $_POST['firstname']<br />\r\n";
echo "lastname is : $_POST['lastname']\r\n";
}
if (isset($_POST['form2'])) {
echo "<p>Following details are saved to database</p>\r\n";
echo "reg No\t:\t$_POST['RegNO']<br />\r\n";
echo "NIC\t:\t$_POST['NIC']<br />\r\n";
echo "Tel No\t:\t$_POST['Telephone']<br />\r\n";
}
?>
</body>
</html>
If you have more forms, consider using switch() instead of if().
You could set different values for button submit for each form. Or you could use a input tag hidden to set type of form. Example:
Form1 : <input type="hidden" name="type" value="form1">
Form2 : <input type="hidden" name="type" value="form2">
And in php form
if($_POST["type"]=="form1")
{
//code here
}else if($_POST["type"]=="form2"){
//code here
}

getting id from post request

i'm trying to make CMS with PHP
i need to get the if from post request
<form action="updatevideo.php" method="post" role="form">
Title: <input name="title" type="text" required> <br />
description:<input name="desc" type="text" required> <br />
url: <input name="ytl" type="text" required> <br />
<input type="hidden" name="id" />
<input type="submit" name="addVideo" value="Add New Video" />
</form>
how can i make this input's value = id
<input type="hidden" name="id" />
on control page
public function Update()
{
/*
1-get data into variables
2-validation
3-Database
*/
if(isset($_GET['id']) && (int)$_GET['id']>0)
{
$id = (int)$_GET['id'];
$user = $this->videoModel->Get_By_Id($id);
print_r($user);
echo
'
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<form action="updatevideo.php" method="post" role="form">
Title: <input name="title" type="text" required> <br />
description:<input name="desc" type="text" required> <br />
url: <input name="ytl" type="text" required> <br />
<input type="hidden" name="id" />
<input type="submit" name="addVideo" value="Add New Video" />
</form>
</body>
</html>
';
}
else
{
if(isset($_POST['addVideo']))
{
$id = $_POST['id'];
echo "5ara" ;
$title = $_POST['title'];
$desc = $_POST['desc'];
$url = $_POST['ytl'];
//Validation
$vid = $this->getVid($url); //video id -_-
$data = array(
'title' => $title,
'desc' => $desc,
'vid' => $vid
);
if($this->videoModel->Update($id,$data))
{
System::Get('tpl')->assign('message','User Updated');
System::Get('tpl')->draw('success');
}
else
{
System::Get('tpl')->assign('message','Error Updating User');
System::Get('tpl')->draw('error');
}
}
else
{
System::Get('tpl')->assign('message','NO USER CHOSEN');
System::Get('tpl')->draw('error');
}
}
}
Quite simple really. As you checked it exists and moved it into a variable you can just echo the $id into the value attribute of that input tag.
<input type="hidden" name="id" value="' . $id . '" />
Using your code:
echo '
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<form action="updatevideo.php" method="post" role="form">
Title: <input name="title" type="text" required> <br />
description:<input name="desc" type="text" required> <br />
url: <input name="ytl" type="text" required> <br />
<input type="hidden" name="id" value="' . $id . '" />
<input type="submit" name="addVideo" value="Add New Video" />
</form>
</body>
</html>';
Define the VALUE attribute :
<input type="hidden" name="id" value="ID_VALUE"/>
OR if you have id in varible than use below code :
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
OR
As you are using the HTML in PHP than:
<input type="hidden" name="id" value="'.$id.'"/>
ID_Value should be your id like 1,2,3..etc.
And Get the id on Action page :
$id = $_POST['id'];
Apart from this you have mention POST in form method and on your
action page you are trying to get the values using GET method,
which is wrong.
Edited due to question edit.
<input type="hidden" name="id" value="'.$id.'" />
You echo a big block of text, this way you can concatenate the $id by stopping block output, including $id and resuming block output.

Validating radio button using php

Im currently using CodeIgniter for my webpage and I am trying to make the radio button to be selected if the locked_status is locked and vice versa. I have data that is passed to the view and I want to use the data to check if the locked_status is either locked or not. The user is able to change the locked_status and when the form is submitted, the data is updated accordingly when the form is submitted. I'm trying to use javascript but I do not know how to retrieve the data and check it. Or is there any other better way to do this?
my controller loading view and passing the data to view.
function userInformation($userName)
{
$this->load->model("agentDB_model");
$data['results'] = $this->agentDB_model->getSelectedAgentDetails($userName);
$this->load->view("viewAgentInfo_view", $data);
}
view
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
<div id="content">
<h1>Home Page</h1>
<p>Selected Agent Information</p>
</div>
<?php
foreach($results as $row)
{
$userName = $row->userName;
$userPassword = $row->userPassword;
$agencyName = $row->agencyName;
$agencyCodeNo = $row->agencyCodeNo;
$invalidLoginCount = $row->invalidLoginCount;
$locked_status = $row->locked_status;
$logged_in = $row->logged_in;
}
/*
if($row->locked_status == 0)
{
$locked_status = "Avalaible";
}
else if($row->locked_status == 1)
{
$locked_status = "Locked";
}
*/
?>
<?php echo form_open('site/updateValues') ?>
User Name: <input type="text" name="tbx_userName" value="<?php echo "$userName"?>" readonly/></br>
User Password: <input type="password" name="tbx_userPassword" value="<?php echo "$userPassword"?>"/></br>
Agency Name: <input type="text" name="tbx_agencyName" value="<?php echo "$agencyName"?>"/></br>
Agency Code Number: <input type="text" name="tbx_agencyCodeNo" value="<?php echo "$agencyCodeNo"?>"/></br>
Invalid Login Count: <input type="text" readonly name="tbx_invalidLoginCount" value="<?php echo "$invalidLoginCount"?>"/></br>
Locked Status: <input type="radio" name="locked_status" id="lock">Lock
<input type="radio" name="locked_status" id="unlock">Unlock
<br/>
<!--<input name="tbx_locked_status" value="<?php echo "$locked_status"?>" readonly/></br> -->
Logged In: <input type="text" readonly name="tbx_logged_in" value="<?php echo "$logged_in"?>" /></br>
<input type="submit" value="Submit"/>
</form>
<div id="footer">
<p>Copyright (c) 2012 basicsite.com</p>
</div>
</body>
</html>
Try this one
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
<div id="content">
<h1>Home Page</h1>
<p>Selected Agent Information</p>
</div>
<?php
foreach($results as $row)
{
$userName = $row->userName;
$userPassword = $row->userPassword;
$agencyName = $row->agencyName;
$agencyCodeNo = $row->agencyCodeNo;
$invalidLoginCount = $row->invalidLoginCount;
$locked_status = $row->locked_status;
$logged_in = $row->logged_in;
if($row->locked_status == 0)
{
$locked_status = "<input type="radio" name="locked_status" checked value=0 id="lock">Locked <input type="radio" name="locked_status" id="unlock" value=1> Unlocked";
}
else if($row->locked_status == 1)
{
$locked_status = " <input type="radio" name="locked_status" value=0 id="lock"> Locked <input type="radio" name="locked_status" checked id="unlock" value=1> Unloacked";
}
}
?>
<?php echo form_open('site/updateValues') ?>
User Name: <input type="text" name="tbx_userName" value="<?php echo "$userName"?>" readonly/></br>
User Password: <input type="password" name="tbx_userPassword" value="<?php echo "$userPassword"?>"/></br>
Agency Name: <input type="text" name="tbx_agencyName" value="<?php echo "$agencyName"?>"/></br>
Agency Code Number: <input type="text" name="tbx_agencyCodeNo" value="<?php echo "$agencyCodeNo"?>"/></br>
Invalid Login Count: <input type="text" readonly name="tbx_invalidLoginCount" value="<?php echo "$invalidLoginCount"?>"/></br>
Locked Status: <?php echo $locked_status; ?>
<br/>
Logged In: <input type="text" readonly name="tbx_logged_in" value="<?php echo "$logged_in"?>" /></br>
<input type="submit" value="Submit"/>
</form>
<div id="footer">
<p>Copyright (c) 2012 basicsite.com</p>
</div>
</body>
</html>

got Notice message when submit form in html

<html>
<body>
<form action="" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
<?php
if( $_GET["name"] || $_GET["age"] )
{
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
}
?>
Question:
when I open above script in browser, It shows:Notice: Undefined index: name in D:\wamp\www\oop\test3.php on line 13,
I know if is because the form is not submit yet, so $_GET["name"] does not exist, but how to fix this problem?
Just set the name of the submit button, and use isset() to check if the form was submitted.
<html>
<body>
<form action="" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
</body>
</html>
<?php
if( isset($_GET['submit']) )
{
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
}
?>
The example below will check if both fields are filled.
If one or the other is not filled, an error message will appear.
If both are filled, then the user will see:
Welcome Bob // assuming the person's name is "Bob" in the field.
You are 30 years old. // assuming the person entered "30" in the field.
Otherwise, it will echo:
Fill in all fields.
Here is the example below:
<html>
<body>
<form action="" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
</body>
</html>
<?php
if($_GET['name'] && $_GET['age'])
{
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
}
if(!$_GET['name'] || !$_GET['age'])
{
echo "Fill in all fields";
}
?>
I've made use of both && and || logical operators for validation.
Consult the PHP manual for more information: http://php.net/manual/en/language.operators.logical.php
You can test if your particular _GET variable exists. For example:
<?php
if ( isset ( $_GET["name"] ) ) {
echo "Welcome ". $_GET['name']. "<br />";
}
?>
Try:
<form action="" method="GET">
Name: <input type="text" name="name" id="name" />
Age: <input type="text" name="age" id="age" />
<input type="submit" />
</form>

PHP Sticky form

When I input numeric value in Number 1 and Number 2, and press "Add". It does not display the total added value. Please see my coding below. and advice me, what to is the problem, and what can be done.
<html>
<head>
<title>Simple Calculator</title>
<?php
if(isset($_POST['submitted'])){
if(is_numeric($_POST['number1']) && is_numeric($_POST['number2'])){
$add = ($_POST['number1'] + $_POST['number2']);
echo "Add: ".$_POST['number1']."+".$_POST['number2']."=";
}
}
?>
<script type="text/javascript">
</script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="post">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if(isset($_POST['number1'])) echo $_POST['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if(isset($_POST['number2'])) echo $_POST['number2'];?>"/></p>
<input type="button" name="add" value="Add" />
<input type="button" name="minus" value="Minus" />
<input type="button" name="multiply" value="Multiply" />
<input type="button" name="divide" value="Divide" />
<input type="reset" name="rest" value="Reset" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
You are echoing the result data into the <head>, so it will not be displayed.
You forgot to echo $add.
Your <input>s are of type button and not submit, so the form will not be submitted to the server.
Because you are echoing the previously entered values into the form, <input type="reset"> will probably not do what you want/expect it to do. I think it would be better to implement this as another submit.
Because this form affects only what the next page displays and does not make a permanent change to the server, you should use the GET method and not POST.
Try this:
<html>
<head>
<title>Simple Calculator</title>
<script type="text/javascript"></script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="get">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if (isset($_GET['number1']) && !isset($_GET['reset'])) echo $_GET['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if (isset($_GET['number2']) && !isset($_GET['reset'])) echo $_GET['number2'];?>"/></p>
<input type="submit" name="add" value="Add" />
<input type="submit" name="minus" value="Minus" />
<input type="submit" name="multiply" value="Multiply" />
<input type="submit" name="divide" value="Divide" />
<input type="submit" name="reset" value="Reset" />
<input type="hidden" name="submitted" value="1" />
</form>
<?php
if (isset($_GET['submitted']) && !isset($_GET['reset'])) {
echo "<div>";
if (is_numeric($_GET['number1']) && is_numeric($_GET['number2'])) {
if (isset($_GET['add'])) {
$result = $_GET['number1'] + $_GET['number2'];
echo "Add: ".$_GET['number1']." + ".$_GET['number2']." = ".$result;
} else if (isset($_GET['minus'])) {
$result = $_GET['number1'] - $_GET['number2'];
echo "Minus: ".$_GET['number1']." - ".$_GET['number2']." = ".$result;
} else if (isset($_GET['multiply'])) {
$result = $_GET['number1'] * $_GET['number2'];
echo "Multiply: ".$_GET['number1']." * ".$_GET['number2']." = ".$result;
} else if (isset($_GET['divide'])) {
$result = $_GET['number1'] / $_GET['number2'];
echo "Divide: ".$_GET['number1']." / ".$_GET['number2']." = ".$result;
}
} else {
echo "Invalid input";
}
echo "</div>";
}
?>
</body>
</html>
The solution of DaveRandom works fine if you change this
action="simple_calculator.php"
by
action="<?php echo $_SERVER['PHP_SELF'] ?>"

Categories