Undefined index error PHP - php

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

Related

PHP recover data from Post

I have inside loop some fields for send inside form with other fields :
<?php
for($f=1;$f<=3;$f++)
{
?>
<input type="text" name="friend['email_<?php echo $f;?>']" value="<?php echo $_POST['friend']['email_'.$f.''];?>">
<?php
}
?>
When i send from form i need get the value of each field if no empty , i think the problem it´s in no recover the value send from the form , i don´t know if i writte right
Ony it´s that problem , thank´s , the best regards
I think the problem here is that you have single quotes around email_<?php echo $f; ?> - in POST data, these are not needed. So, you would have:
<form action="myPage.php" method="POST">
<?php for ($f=1;$f<=3;$f++) { ?>
<input type="text" name="friend[email_<?php echo $f; ?>]" value="<?php echo ... ?>">
<?php } ?>
<input type="submit">
</form>
PHP will parse POST data into associative arrays, so you will get back a 'friend' object in $_POST. You can access this like so:
<?php
$friends = isset($_POST['friend']) ? $_POST['friend'] : array();
print_r($friends);
?>
I'm not sure if i'm getting it right. You must send your form data in order to manipulate it. For example if you want to print all the form data:
Client Side:
<form action="boo.php" method="POST">
<input type="text" name="name">
<input type="text" name="age">
<input type="submit">
</form>
Server Side:
<?php
$name = "";
$age = "";
if( isset($_POST['name']) && isset($_POST['age']) )
{
$name = htmlspecialchars($_POST['name']);
$age = htmlspecialchars($_POST['age']);
}
echo 'Your name is ' . $name . ' and your age is ' . $age;
?>
If you are trying to retain the values even after invalid input you can just echo the input in value attribute of the input field.

How to keep radio button value inside php variable without error

Let me explain more in details what I am trying to do.
Okay so, I am doing a huge form to send in a particular e-mail. My problem right now is that only my radio buttons in my form show an error when the form is submit and return false. Here's an example of what I am talking about :
<?php
if (isset($_POST['submit'])) {
$radio = $_POST['number'];
$name = $_POST['name'];
}
?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<input type="radio" name="number" value="allo">
<input type="radio" name="number" value="yo">
<input type="text" name="name">
<input type="submit" name="submit">
</form>
So as you see, if the customer press on "submit" and didn't filled values inside of radio and input field, $name will show no error but $radio will show an error.
My Error :
Notice: Undefined index: number in D:\xampp\htdocs\test\preg.php on line 5
What you are getting is the server warning and not an actual error.
To avoid it you have flags that you can set to ignore warning, but this isn't a good practice.
I would suggest you to check if the form are set and handle them if not.
Code:
<?php
if (isset($_POST['submit'])) {
$radio = (isset($_POST['number']) ? $_POST['number'] : "");
$name = (isset($_POST['name']) ? $_POST['name'] : "");
}
?>
Edit: Of course, lets not forget, the better practice will be handle them against malicious code.
You get that error because $_POST['number'] is not passed to the page.
You should set at least one of the radio buttons, and if you want to be sure of not getting an error even if the users modifies the page, you can check with an isset as others as wrote in the other answers.
If you were wondering why you didn't get any error from the name, that's because it contains the empty string "".
Check this code and view the source of the page to see what print_r writes in a formatted way
<?php
if (isset($_POST['submit'])) {
$radio = $_POST['number'];
$name = $_POST['name'];
}
print_r($_POST)
?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<input type="radio" name="number" value="allo" checked="true">
<input type="radio" name="number" value="yo">
<input type="text" name="name">
<input type="submit" name="submit">
</form>
You can write
if (isset($_POST['name'])) $name = $_POST['name'];
but even better is
isset($_POST['name']) ? $name = $_POST['name'] : $name = '';
if (isset($_POST['number']))
{
$radio = $_POST['number'];
}

PHP: get the value of TEXTBOX then pass it to a VARIABLE

Good Day!.
I need a kinda little help for PHP. I’m really really newbie for PHP and I already search it to google and can’t find any solution.
My Problem is:
I want to get the value of textbox1 then transfer it to another page where the value of textbox1 will be appeared in the textbox2.
Below is my codes for PHP:
<html>
<body>
<form name='form' method='post' action="testing2.php">
Name: <input type="text" name="name" id="name" ><br/>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
I also add the code below and the error is “Notice: Undefined index: name”
<?php
$name = $_GET['name'];
echo $name;
?>
or
<?php
$name = $_POST['name'];
echo $name;
?>
Your method is post, so use $_POST
Also, try wrapping it around an isset function:
if (isset($_POST['name'])){
echo $_POST['name'];
}
This will also handle the undefined error
This would be your textbox2:
<input type="text" name="textbox2" id="name" <?php echo (isset($_POST['name']) ? 'value=' .htmlspecialchars($_POST['name']). ' : ''); ?>/>
If you want to use the GET method, change post to get in your form and $_POST to $_GET in the php code of textbox2

mysql UPDATE variable

Hi guys i have code like this
include('db.php');
if (isset($_POST['save']) && $_POST['save'] == '') {
if(isset($_POST['misamarti'])){ $misamarti = $_POST['misamarti']; }
if(isset($_POST['teleponi'])) { $teleponi = $_POST['teleponi']; }
if(isset($_POST['posta'])) { $posta = $_POST['posta']; }
if(isset($_POST['paqsi'])) { $paqsi = $_POST['paqsi'];}
$query = "UPDATE contact SET `misamarti` = ".$misamarti.", `teleponi` = ".$teleponi.", `posta` = ".$posta.", `paqsi` = ".$paqsi." WHERE `contact`.`id` =1;";
$result = mysql_query($query);
}
<form method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="about" value="template1" />
<input type="text" value="" name"misamarti" />
<input type="text" value="" name"teleponi" />
<input type="text" value="" name"posta" />
<input type="text" value="" name"paqsi" />
<input type="submit" value="" name="save" />
and result is
Notice: Undefined variable: misamarti in C:\xampp\htdocs\Template\admin_panel\contact.php on line 18
Notice: Undefined variable: teleponi in C:\xampp\htdocs\Template\admin_panel\contact.php on line 18
Notice: Undefined variable: posta in C:\xampp\htdocs\Template\admin_panel\contact.php on line 18
Notice: Undefined variable: paqsi in C:\xampp\htdocs\Template\admin_panel\contact.php on line 18
if anyone know why is this error pls comment.
You're using the variables even if they weren't submitted with the form, and update the fields in the db with those undefined varaibles, so you'll be destroying any data that was in the DB previously.
If nothing else, you need to at least set a default value for the fields, e.g:
$misamarti = isset($_POST['misamarti']) ? $_POST['misamarti'] : '';
^^--default empty string
And then realize that you are WIDE OPEN to an sql injection attack. You are BEGGING to get your server pwn3d.
As you can see in your HTML code, you have the names bad written, they have to be written this way: name="misamarti". You forgot the equal (=) between name and the name. If you see the rest of attributes, like text and value, you see that theres an equal, that's the correct way of setting attributes.

Open page with Form pre-filled

I have an entry Form. When the page is loaded, it must check:
if ($_SESSION[WorkMode] == 'UPDATE')
Then fill the Form with values from the database, else open a blank Form.
If I fetch the results in a different PHP file and call this .php file on load, how to fill the Form.
Set the variables that hold the values for your form, then include the "template" of the form you're having.
File 1:
<?php
$res = mysql_query("..");
if($res) {
$row = mysql_fetch_assoc($res);
$name = $row['name'];
$birthday = $row['birthday'];
...
include('form.tpl');
}
File 2 (form.tpl)
<form action="">
<input type="text" name="username" value="<?php isset($name)?$name:""; ?>" />
.. and so on
</form>
Alternatetively you can use a full blown template engine like Smarty to do the job for you.
Best wishes,
Fabian
$formvalue = NULL;
if ($_SESSION[WorkMode] == 'UPDATE')
$formvalue = $some_database_value;
echo '<input type="text" name="myname" value="'.$formvalue .'" />';
I found out that the following did it for me, adding the variable after a column.
<label>Firstname : </label><input type="text" name="Firstname" value= "<?php echo (isset($_POST['firstname'])) ? htmlspecialchars($_POST['firstname']) : $firstname;?>"

Categories