This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I have a 4 input boxes, 3 are hidden fields. The first input box take a figure, while the second displays 20% of the figure and the 3rd displays the answer gotten from subtracting 20% of the initial figure from the figure. here is a typical example of what i mean.
textbox A = 100
20% of 100 = 20.
textbox B = 20.
100 - 20 = 80.
textbox C = 80
i have been able to do all this and passed the variables to the input box, but when i load the page it populates the textbox with undefined variable and when i click on the button it execute the script and the initial text that says undefined variable is inserted into my db. how do i post the answer into my db instead of post what loads with the page.
here is my code.
When it loads up:
<?php
if (isset($_POST['amount'])&& ($_POST['iniamount']) &&
($_POST['outamount'])) {
$amount = $_POST['amount'];
$iamount = 0.2;
$inimain = $iamount * $amount;
$outamount = $amount - $inimain;
}
$date = date("Y-m-d H:i:s");
?>
here is the form
<div class="form">
<form method="POST" enctype="multipart/form-data" name="donation" class="" >
<div class="form-group ">
<label for="cname" class="control-label col-lg-2">Donation Amount <span class="required">*</span></label>
<div class="col-lg-10">
<input class="form-control" id="amount" name="amount" type="text" required />
<input name="email" type="hidden" class="form-control" id="email" required />
<input name="iniamount" type="text" class="form-control" id="iniamount" value="<?php echo $inimain; ?>" required />
<input name="outamount" type="text" class="form-control" id="outamount" value="<?php echo $outamount; ?>" required />
<input name="date" type="hidden" class="form-control" id="outamount" value="<?php echo $date; ?>" required />
</div>
<?php
$amount = 0.0;
$iamount = 0.0;
$inimain = 0.0;
$outamount = 0.0;
if (isset($_POST['amount'])&& ($_POST['iniamount']) && ($_POST['outamount'])) {
$amount = $_POST['amount'];
$iamount = 0.2;
$inimain = $iamount * $amount;
$outamount = $amount - $inimain;
}
$date = date("Y-m-d H:i:s");
?>
The problem is from the variables.
Initiate the variables. Actually not like this because it is for calculation.
but if you do like this and when the textboxs are empty it will cause undefine error.The system will not knw how to deal with it.
$inimain = '';
$outamount = '';
This is the correct way.
$amount = 0.0;
$iamount = 0.0;
$inimain = 0.0;
$outamount = 0.0;
<input name="iniamount" type="text" class="form-control" id="iniamount" value="<?php if(isset($_POST['iniamount'])){echo $inimain;} ?>" required />
<input name="outamount" type="text" class="form-control" id="outamount" value="<?php if(isset($_POST['$outamount'])){echo $outamount;} ?>" required />
<input name="date" type="hidden" class="form-control" id="outamount" value="<?php if(isset($_POST['$date'])){echo $date;} ?>" required />
Initialize the variable first
While The accepted answer will work, I think initializing the variable before hand will serve you better in the long run.
<?php
$inimain = '';
$outamount = '';
// repeat for any other variable you might need in your form
if (isset($_POST['amount'])&& ($_POST['iniamount']) && ($_POST['outamount'])) {
$amount = $_POST['amount'];
$iamount = 0.2;
$inimain = $iamount * $amount;
$outamount = $amount - $inimain;
}
$date = date("Y-m-d H:i:s");
?>
This will allow you to keep your HTML form simple and easy to read and will allow you more flexibility in the future, for example if you want to set a default value for certain input fields.
Related
I have the following code:
<form action="calculator.php" method="get">
<label for="age">Enter your age</label>
<input type="number" id="age" name="age">
<br>
<label for="height">Enter your height</label>
<input type="number" id="height" name="height">
<br>
<input type="submit">
</form>
<?php
$age = $_GET["age"];
$height = $_GET["height"];
if (isset($age) && isset($height)) {
echo ($age + $height);
}
?>
This gives me the warnings: Warning: Undefined array key "age" in (file location) and Undefined array key "height" in (file location).
As you can see I'm not using arrays. I googled this and tried putting the echo statement inside an if ( isset() ), but it still gives me the same warning.
Can someone tell me why and how to fix this?
$_GET is an empty array on this case that's why they are undefined keys.
You should check if $_GET has age and height.
You can do something like this:
if(isset($_GET['age'], $_GET['height'])){
// do the calculation
}
Try using extract function.
<?php
extract($_GET);
/*$age = $_GET["age"];
$height = $_GET["height"];
*/
if (isset($age) && isset($height)) {
echo ($age + $height);
}
?>
I have a form on my site, and I want to make the minimum number that's allowed to be inputted be 10 or higher.
Here's the code for that form:
<input type="number" id="amount" onchange="updateInput(this.value)" class="form-control" value="10" placeholder="1.00" autocomplete="off" required>
Can anyone help me get this done?
Thank you
Change value to min
<input type="number" id="amount" onchange="updateInput(this.value)" class="form-control" min="10" placeholder="1.00" autocomplete="off" required>
this question has the PHP tag. in first place you need to consider Jaime's answer for client side (and please add a name attribute for any form input. in this case we assume you set the name to "amount"), and at back-end PHP do this :
<?php
$error = '';
$amount = ''; // default value;
if (isset($_REQUEST['amount'])) {
$amount = intval($_REQUEST['amount']); // get the value as number
if ($amount < 10) $error = 'the amount is under 10 and not acceptable';
}
echo $error;
?>
I am trying to pass a value from a textbox to another textbox (Same window) with conditions, Lets say. If the value from the first textbox is 1 then the output on the other textbox will be 2, and if the first textbox is 2 then the output on the other will be 3 and so on so forth.
Here is what Ive done so far.
Text box 1:
<input type="text" name="basic_salary" value="<?php
if ($emp_salary->basic_salary <= 15999) {
echo $emp_salary->provident_fund = 120;
} elseif ($emp_salary->basic_salary >= 16000) {
echo $emp_salary->provident_fund = 1;
} else {
echo "Have a good night!";
}
?>" class="form-control">
and for the textbox 2:
<label class="control-label" >SSS </label>
<input type="text" name="provident_fund" value="<?php
if (!empty($emp_salary->provident_fund)) {
echo $emp_salary->provident_fund;
}
?>" class="form-control">
am using Codeigniter. Thank you!
I would do this in the controller before passing the data to the view.
// defaults
$content_data['basic_salary'] = $emp_salary->basic_salary;
$content_data['provident_fund'] = "";
// exception(s) - can be expanded
if ($emp_salary->basic_salary <= 15999) {
$content_data['provident_fund'] = 120;
}elseif ($emp_salary->basic_salary >= 16000) {
$content_data['provident_fund'] = 1;
}
$this->load->view('content', $content_data);
Then in your view do something like
<input type="text" name="basic_salary" value="<?php echo $basic_salary; ?>">
<input type="text" name="provident_fund" value="<?php echo $provident_fund; ?>">
Might need some tuning from your side.
I'm working on a formular, but for the moment I just want to insert into an array my elements (I have books and authors).
I can display my books with author (name + surname) with the foreach, but I can't add more elements.
Here is the code with the form.
<H1>Exercice 2</H1>
<form method="POST">
<label for"code" >Number :</label>
<input id="code" name="code" type="number" />
<label for"title">Title :</label>
<input id="title" name="title" type="text" />
<label for"author" >Author :</label>
<input id="author" name="author" type="text" />
<button type="input" type="submit">Ok</button>
$title = $_POST['title'];
$code = $_POST['code'];
$author = $_POST['author'];
$book = array();
$book['code'] = 123;
$book['title'] = "Legendes";
$book['author'] = array("David", "Gemmel");
foreach($book as $value){
$book['key'] = $value;
var_dump($book);
if (is_array($value)) {
foreach($value as $otherValue) {
echo($otherValue);
}
} else {
echo($value);
}
}
I did some searcch, but I don't think it works, it's using the array_push() method with the POST, but I don't know where I can manipulate my form into the array.
If you want some details, I'll be happy to do that =) I'm working on it, if i have some news, you will know =)
Have a nice day =)
1) Assignments are in reverse. Correct way:
$myVar = $myValue
2) You need to set the name attribute in your inputs in order to be sent:
<input id="code" type="number" name="code" />
Then you can access them like:
$_POST['code']
3) To add an element by key in an array, use:
$array['key'] = $value;
Your Exercise 2 have some mistakes :
First, your HTML inputs must have the name attribute to be retrieved by post:
<h1>Exercice 2</h1>
<form method="post">
<label>
<input name="code" type="number" />
</label>
<button type="submit">Ok</button>
</form>
With PHP, you can access to any input value using the name:
$code = $_POST['code'];
Now, I think you want to "add" several books using this HTML form without a storage system. The problem is you can not do this if for every a new request since all the elements you have in your array will be deleted each time you run a new post request. To keep this information you need to use some persistent storage system as a database or others.
Since you seem to want to keep the information for each book together, you need to use a multidimensional array - hence, you'll need to redo the whole thing. Here's a suggestion:
Form:
<h2>Exercice 2</h2>
<form method="post">
<label for"code">Number :</label>
<input id="code" name="code" type="number">
<label for"title">Title :</label>
<input id="title" name="title" type="text">
<label for"author-firstname">Author First Name:</label>
<input id="author-firstname" name="author-firstname" type="text">
<label for "author-lastname">Author Last Name:</label>
<input id="author-lastname" name="author-lastname" type="text">
<input type="submit" name="submit_book" value="Ok">
</form>
Fixed the name-problems, changed the heading (you never, ever use H1 for a form, H1 is strictly used for the site-wide heading/logo/name of site). Also changed the button into a simple input type="submit".
$title = $_POST['title'];
$code = $_POST['code'];
$author = $_POST['author'];
$book = []; // changed this to modern PHP version array assignment
$book[0]['code'] = 123;
$book[0]['title'] = "Legendes";
$book[0]['author-firstname'] = "David";
$book[0]['author-lastname'] = "Gemmel"; // no reason to assign a separate array for first and last name, just use two array-keys
for ($c = 0; $c <= count($book); $c++) { //changed this to a for, counting the amount of entries in the $book array
echo 'Title: '.$book[$c]['title'];
echo 'Author: '.$book[$c]['author-firstname'].' '.$book[$c]['author-lastname'];
} // the content should probably be wrapped in a container of some sort, probably a <li> (and then a <ul>-list declared before the for-loop)
Now. None of this has anything to do with putting stuff INTO the array. That would be something like this (there isn't even a point of assigning the $_POST-variables for the code you posted. But, you can do something like this:
if (isset($_POST['submit_book'])) {
$title = $_POST['title'];
$code = $_POST['code'];
$author-firstname = $_POST['author-firstname'];
$author-lastname = $_POST['author-lastname'];
// however, if all you're doing is putting this into the array, no need to assigne the $_POST to variables, you can just do this:
$temp_array = ['code'=>$_POST['code'],'title'=>$_POST['title'],'author-firstname'=>$_POST['author-firstname'],'author-lastname'=>$_POST['author-lastname']];
$book[] = $temp_array;
}
So, that would replace the assigned variables at the beginning of your code.
I'm new in PHP and I'm getting this error:
Notice: Undefined index: productid in /var/www/test/modifyform.php on
line 32
Notice: Undefined index: name in /var/www/test/modifyform.php on line
33
Notice: Undefined index: price in /var/www/test/modifyform.php on line
34
Notice: Undefined index: description in /var/www/test/modifyform.php
on line 35
I couldn't find any solution online, so maybe someone can help me.
Here is the code:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="rowID" value="<?php echo $rowID;?>">
<p>
Product ID:<br />
<input type="text" name="productid" size="8" maxlength="8" value="<?php echo $productid;?>" />
</p>
<p>
Name:<br />
<input type="text" name="name" size="25" maxlength="25" value="<?php echo $name;?>" />
</p>
<p>
Price:<br />
<input type="text" name="price" size="6" maxlength="6" value="<?php echo $price;?>" />
</p>
<p>
Description:<br />
<textarea name="description" rows="5" cols="30">
<?php echo $description;?></textarea>
</p>
<p>
<input type="submit" name="submit" value="Submit!" />
</p>
</form>
<?php
if (isset($_POST['submit'])) {
$rowID = $_POST['rowID'];
$productid = $_POST['productid']; //this is line 32 and so on...
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
}
What I do after that (or at least I'm trying) is to update a table in MySQL.
I really can't understand why $rowID is defined while the other variables aren't.
Thank you for taking your time to answer me.
Cheers!
Try:
<?php
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['price'])) {
$price = $_POST['price'];
}
if (isset($_POST['description'])) {
$description = $_POST['description'];
}
?>
Apparently the index 'productid' is missing from your html form.
Inspect your html inputs first. eg <input type="text" name="productid" value="">
But this will handle the current error PHP is raising.
$rowID = isset($_POST['rowID']) ? $_POST['rowID'] : '';
$productid = isset($_POST['productid']) ? $_POST['productid'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$price = isset($_POST['price']) ? $_POST['price'] : '';
$description = isset($_POST['description']) ? $_POST['description'] : '';
This is happening because your PHP code is getting executed before the form gets posted.
To avoid this wrap your PHP code in following if statement and it will handle the rest no need to set if statements for each variables
if(isset($_POST) && array_key_exists('name_of_your_submit_input',$_POST))
{
//process PHP Code
}
else
{
//do nothing
}
TRY
<?php
$rowID=$productid=$name=$price=$description="";
if (isset($_POST['submit'])) {
$rowID = $_POST['rowID'];
$productid = $_POST['productid']; //this is line 32 and so on...
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
}
There should be the problem, when you generate the <form>. I bet the variables $name, $price are NULL or empty string when you echo them into the value of the <input> field. Empty input fields are not sent by the browser, so $_POST will not have their keys.
Anyway, you can check that with isset().
Test variables with the following:
if(isset($_POST['key'])) ? $variable=$_POST['key'] : $variable=NULL
You better set it to NULL, because
NULL value represents a variable with no value.
Hey this is happening because u r trying to display value before assignnig it
U just fill in the values and submit form it will display correct output
Or u can write ur php code below form tags
It ll run without any errors
If you are using wamp server , then i recommend you to use xampp server .
you . i get this error in less than i minute but i resolved this by using (isset) function . and i get no error .
and after that i remove (isset) function and i don,t see any error.
by the way i am using xampp server
this error occurred sometime method attribute ( valid passing method )
Error option :
method="get" but called by $Fname = $_POST["name"];
or
method="post" but called by $Fname = $_GET["name"];
More info visit http://www.doordie.co.in/index.php
To remove this error, in your html form you should do the following in enctype:
<form enctype="multipart/form-data">
The following down is the cause of that error i.e if you start with form-data in enctype, so you should start with multipart:
<form enctype="form-data/multipart">