got Notice message when submit form in html - php

<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>

Related

Cookies function not saving the Data

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>

scope of $_POST[] , trying to access $_POST[] variable, getting the undefined index error

I have these two forms that submits to different blocks of the same script. i am unable to access variable of one block in other, though both variables are in same script.
html form :(1.php)
<html>
<form method="POST" action="2.php" enctype="multipart/form-data">
</br>
Choose a user name:</font>
<input type="text" name="username">
<input type="submit" name="submit" value="Save and Proceed">
</form>
</html>
2.php:
<?php
$name=$_POST['username'];
if ((isset($_POST['username'])) && ($_POST['submit'] == 'Save and Proceed'))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
enter age:</font>
<input type="text" name="age">
<input type="submit" name="submit" value="done">
</form>
</html>
<?php
}
}
if((isset($_POST['age'])) && ($_POST['submit'] == 'done'))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "hi" .$name;
echo "your age is ". $age;
echo"you are eligible";
}
}
?>
How do I access $_POST['username'] in the code following the html form in the same script? Thank you in advance.
On the second form you can use a hidden input, i.e.:
<input type="hidden" name="username" value="$name">
Example:
<?php
$name=$_POST['username'];
if (!empty($_POST['username']) && $_POST['submit'] == 'Save and Proceed'))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
echo <<< LOL
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="hidden" name="username" value="$name">
<input type="submit" name="submit" value="done">
</form>
</html>
LOL;
}
}
if((isset($_POST['age'])) && ($_POST['submit'] == 'done'))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "hi" .$name;
echo "your age is ". $age;
echo"you are eligible";
}
}
?>
If I understand well you want to pass username twice.
Than you can use hidden input, which is not visible (only transports data):
<input type="hidden" name="username" value="<?php echo $_POST['username']; ?>" />

PHP Form Get and Post Output with If statement to output results on same page

I'm learning PHP and working on forms right now. I have an assignment to create a form, one GET and one POST on the same page and have the results posted on the same page below each form.
I created an if/else statement but maybe I'm going about it incorrectly.
<div>
<h1>POST Method Form</h1>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
First Name:<br>
<input type="text" name="firstname"><br>
Last Name:<br>
<input type="text" name="lastname"><br>
E-mail: <input type="text" name="email"><br>
Database Utilized: <br>
<input type="checkbox" name="dba" value="SQL Server">SQL Server<br>
<input type="checkbox" name="dba" value="Oracle">Oracle<br>
<input type="checkbox" name="dba" value="Access">Microsoft Access<br>
<br>
<input type="submit" name="postsubmit" value="Submit">
</form>
<br>
<h1>Database Consulting POST Form Results</h1>
<p>
<?php
if (isset($_POST['postsubmit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['laststname'];
$email = $_POST['email'];
$dba = $_POST['dba'];
echo $firstname; <br>
echo $lastname; <br>
echo $email; <br>
echo $dba; <br>
}
else {
echo "Please enter correct values in form and hit submit";
}
?>
</p>
<br>
<h1>GET Method Form</h1>
<form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
First name:<br>
<input type="text" name="firstname_get"><br>
Last name:<br>
<input type="text" name="lastname_get"><br>
E-mail: <input type="text" name="email_get"><br>
Database Utilized<br>
<input type="checkbox" name="dba_get" value="SQL Server">SQL Server<br>
<input type="checkbox" name="dba_get" value="Oracle">Oracle<br>
<input type="checkbox" name="dba_get" value="Access">Microsoft Access<br>
<br>
<input type="submit" name="getsubmit" value="Submit">
</form>
<br>
<br>
<h1>Database Consulting GET Form Results</h1>
<p>
<?php
if (isset($_GET['getsubmit'])) {
$firstname_get = $_GET['firstname'];
$lastname_get = $_GET['laststname'];
$email_get = $_GET['email'];
$dba_get = $_GET['dba'];
echo $firstname_get; <br>
echo $lastname_get; <br>
echo $email_get; <br>
echo $dba_get; <br>
}
else {
echo "Please enter correct values in form and hit submit";
}
?>
</p>
</div>
You misspelled $_GET['lastname'] as $_GET['laststname'] , change your action to action="" and include your <br> in your echo statement like echo $firstname .'<br>'; instead of echo $firstname; <br>
<h1>POST Method Form</h1>
<form method="POST" action="" >
First Name:<br>
<input type="text" name="firstname"><br>
Last Name:<br>
<input type="text" name="lastname"><br>
E-mail: <input type="text" name="email"><br>
Database Utilized: <br>
<input type="checkbox" name="dba[]" value="SQL Server">SQL Server<br>
<input type="checkbox" name="dba[]" value="Oracle">Oracle<br>
<input type="checkbox" name="dba[]" value="Access">Microsoft Access<br>
<br>
<input type="submit" name="postsubmit" value="Submit">
</form>
<br>
<h1>Database Consulting POST Form Results</h1>
<p>
<?php
if (isset($_POST['postsubmit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$dba = $_POST['dba'];
echo $firstname .'<br>';
echo $lastname .'<br>';
echo $email .'<br>';
foreach ($dba as $database) {
echo $database .'<br>';
}
}
else {
echo "Please enter correct values in form and hit submit";
}
?>
</p>
first of all, I would suggest having a hidden field called action like:
<input type="hidden" name="action" value="submituserinfo" />
then put your php logic in the top not the bottom, process before the page shows! Your PHP_SELF deal is good. But your logic is off. The correct logic should be:
if(isset($_REQUEST['action']) && $_REQUEST['action']=='submituserinfo'){
//analyze the data
if($errors){
//set a var to show errors below
}else{
//enter the data, whatever
}
}
That should get you started in the right direction. If you put the coding at the head of the document, you can better handle output or even use header() to redirect based on success or error.

How do I multiply values in PHP?

I am trying to find a basic input where user enters one number and the second number and then multiplies it.
I got it to work without the isset function, but now I am trying to echo out the error line when the page first starts up. If you see the input it is named, name and name2 so I call them in PHP.
My original code did not use isset and it worked but I got error before any input. This is my PHP code:
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name'])) && (isset($_POST['name2'])){
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>
You have closed your IF parentheses too soon. The line should be like this:
if (isset($_POST['name']) && isset($_POST['name2'])) {
This is working code you have some extra parenthesis. If you are multiplying integer values from user always use intval function so that you always have integer value. If user enters string or characters it intval will change to zero
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name']) && isset($_POST['name2'])){
$num = intval($_POST['name']);
$num2 = intval($_POST['name2']);
echo $num*$num2;
}
else{
echo '';
}
?>
Try this I think it is helpful to you:
<form method="POST">
<input type="text" name="value1" placeholder="Enter 1st Value" required>
<input type="text" name="multiply" value="*" readonly>
<input type="text" name="value2" placeholder="Enter 2nd Value" required>
<input type="submit" name="submit" value="Calculate">
</form>
<?php
if(isset($_POST['submit'])){
$value1 = $_POST['value1'];
$multiply = $_POST['multiply'];
$value2 = $_POST['value2'];
if($multiply == "*"){
echo $value1*$value2;
}
}
?>
The main problem is paranthesis are not closed properly it is
if(condition1)&& (condition2){
}
it should be
if((condition1)&&(condition2)){
}
you can use single condition for this also as shown in below code
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send" name="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
//if (isset($_POST['name'])) && (isset($_POST['name2'])){ problem is here your paranthesis are not closed properly
if (isset($_POST['send'])){ //use this as this will ensure that your send button is clicked for submitting form
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>

passing value from one text box to another in php

I have two text boxes and one button. I want when i will type any value in 1st textbox and click on a button the value which i'll type in 1st text box should be the value of 2nd text box. And i was using this code. Can some one help me on this?
<?php
if(isset($_POST['sum']))
{
$v1=$_POST['abc'];
if($v1=="vivek")
{
echo "welcome Mr.".$v1;
}
else
{
echo "Unauthorised User Mr. ".$v1;
}
}
?>
<html>
<body>
<form method="post" action="">
<p>Name:&nbsp&nbsp&nbsp&nbsp&nbsp
<input type="text" name="abc" value=""/>&nbsp&nbsp
</br></br>
Passed Value: <input type="text" name="xyz" value="<?php echo $v1;?>"/>
</p>
<p>
<input type="submit" name="sum" value=" Submit "/>
</p>
</form>
</body>
</html>
try this one.
<form method="post" action="">
<p>Name:&nbsp&nbsp&nbsp&nbsp&nbsp
<input type="text" name="abc" value=""/>&nbsp&nbsp
</br></br>
Passed Value: <input type="text" name="xyz" value="<?php if(isset($_POST['abc'])) { echo $_POST['abc']; } ?>"/>
</p>
<p>
<input type="submit" name="sum" value=" Submit "/>
</p>
</form>
Passed Value:
<input type="text" name="xyz" value="<?php
if(isset($_POST['abc']))
echo htmlentities($_POST['abc'],ENT_QUOTES);
?>"/>
This should do it :)
Try this code.
<script type="text/javascript">
function sub() {
document.getElementById('xyz').value = document.getElementById('abc').value;
}
</script>
<form>
<label>Amonut</label>
<input type="text" name="abc" onkeyup="sub();" id="abc" />
<br /><br />
<label>Discount</label>
<input type="text" name="xyz" id="xyz" />
<input type="submit" name="sum" value=" Submit "/>
</form>
<script type="text/javascript">
function sub() {
document.getElementById('xyz').value = document.getElementById('abc').value;
}
</script>
<form onsubmit="sub();">
<label>Amonut</label>
<input type="text" name="abc" id="abc" />
<br /><br />
<label>Discount</label>
<input type="text" name="xyz" id="xyz" />
<input type="submit" name="sum" value=" Submit "/>
</form>
I think you want this.
Try this code
$v1='';
if(isset($_POST['sum']))
{
if($v1=="vivek")
{
echo "welcome Mr.".$v1;
$v1=$_POST['abc'];
}
else
{
echo "Unauthorised User Mr. ".$v1;
}
}

Categories