Can't get the $_POST Variable - php

It should be a multiple upload form for pictures
I get the HTML Code for a Upload-Form:
<form action="upload.php" method="post" id="uploadform" name="uploadform" enctype="multipart/form-data">
<label id="filelabel" for="fileselect">Choose the Pictures</label>
<input type="file" id="fileselect" class="fileuplaod" name="uploads[]" multiple />
<span class="text">Exist Album</span><br />
<select id="existAlbum" name="existAlbum" size="1">
<option value="noAlbum">SELECT ALBUM</option>
</select>
<span class="text">OR</span>
<span class="text">New Album</span><br />
<input id="newAlbum" name="newAlbum" type="text" maxlength="20" placeholder="ALBUM NAME"/>
<input type="submit">
</form>
The form link to the uploaded.php. But there i get:
Notice: Undefined index: existAlbum in E:\xampp\htdocs\fotokurs\upload\upload.php on line 11
Notice: Undefined index: newAlbum in E:\xampp\htdocs\fotokurs\upload\upload.php on line 12
Here's the upload.php:
<?PHP
$allowedExtensions = array('png', 'jpg', 'jpeg');
$maxSize = 20971520;
$i = 0;
$first = 0;
$exist_album = $_POST['existAlbum'];
$new_album = $_POST['newAlbum'];
Where is my fault? I can't find it...
EDIT
Add following to my code:
if( isset( $_POST['existAlbum'] ) or isset( $_POST['newAlbum'] ) ){
$exist_album = $_POST['existAlbum'];
$new_album = $_POST['newAlbum'];
}else{
echo 'no album <br />';
}
print_r($_POST);
new output:
no album
Array ( )
Notice: Undefined variable: new_album in E:\xampp\htdocs\fotokurs\upload\upload.php on line 20
Notice: Undefined variable: exist_album in E:\xampp\htdocs\fotokurs\upload\upload.php on line 21
Notice: Undefined variable: new_album in E:\xampp\htdocs\fotokurs\upload\upload.php on line 22
Notice: Undefined variable: exist_album in E:\xampp\htdocs\fotokurs\upload\upload.php on line 23

One of your issues is that existAlbum has no actual values associated with it.
You have <option>Select Album</option> which has no value associated with the option element. If there is no value associated, the select element is not posted to the server. You should change it to be:
<option value="">Select Album</option>
EDIT
Since the user only has to supply one or the other, you should use the following to set your variables:
$existsAlbum = (isset($_POST['existAlbum']) && !empty($_POST['existAlbum'])) ? $_POST['existAlbum'] : 'defaultValue';
$newAlbum = (isset($_POST['newAlbum']) && !empty($_POST['newAlbum'])) ? $_POST['newAlbum'] : 'defaultValue';
One important thing to note is that Internet Explorer does not support the placeholder attribute.
EDIT 2
Here is my quick test page that worked test.php:
<form action="upload.php" method="post" id="uploadform" name="uploadform" enctype="multipart/form-data">
<label id="filelabel" for="fileselect">Choose the Pictures</label>
<input type="file" id="fileselect" class="fileuplaod" name="uploads[]" multiple />
<span class="text">Exist Album</span><br />
<select id="existAlbum" name="existAlbum" size="1">
<option value="noAlbum">SELECT ALBUM</option>
</select>
<span class="text">OR</span>
<span class="text">New Album</span><br />
<input id="newAlbum" name="newAlbum" type="text" maxlength="20" placeholder="ALBUM NAME"/>
<input type="submit" value="Submit">
</form>
upload.php
<pre>
<?php print_r($_POST); ?>
<?php print_r($_FILES); ?>
</pre>
results
Array
(
[existAlbum] => noAlbum
[newAlbum] =>
)
Array
(
[uploads] => Array
(
//Contents here
)
)

Try if the value existAlbum get set, because it won't return any value if you there is nothing picked. You could give the existAlbum picker a default='1' or something:
if isset($_POST['existAlbum']){
echo 'yes';
}
else{
echo 'no';
}
I think that there is something wrong with the rule enctype="multipart/form-data". Try to just remove this, it should be set automatically by your browser.

You have no value for the option select album, even if you don't intend that option to be used give it a value such as 0 so that it will always be set in the POST variables.
<option value="0">SELECT ALBUM</option>
<option value="some album">Some Album</option>
...

If select is not picked you will not get it at all (you expect it to be empty, which is not true). You have to check first
$exist_album = isset($_POST['existAlbum']) ? $_POST['existAlbum'] : '<DEFAULT VALUE>';
and same for checkbox.
The newAlbum thing should work as text inputs are always there. See
print_r($_POST);
to see what's really in there, and in my case it is - on "empty" submit I get:
Array
(
[existAlbum] => SELECT ALBUM
[newAlbum] =>
)
BTW: you should use <?php rather than <?PHP.

print $_POST Array using print_r($_POST); Make sure your form action is correct
<form action="upload.php" method="post" id="uploadform" name="uploadform" enctype="multipart/form-data">

Related

Warning: Undefined array key even when I'm not using array

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);
}
?>

undefined index name1 and Undefined index name2

these are the two files in which error is occurred:
html_form.html
<html>
<body>
<center>
<h1>Admission Form</h1>
<br>
<form action="php_register.php" name="registration">
Firstname: <input type="text" name="name1" placeholder="Enter Firstname"/>
<br>
Lastname: <input type="text" name="name2" placeholder="Enter Lastname"/><br>
<input type="submit" value="submit">
</form>
</center>
</body>
</html>
php_register.php
<?php
$fname=$_POST['name1'];
$lname=$_POST['name2'];
$conn=mysqli_connect("localhost","root","","test");
if (isset($fname) && isset($lname))
{
mysqli_query($conn, "insert into test_table(firstname,lastname)
values ('$fname','$lname')");
}
else
echo "<br> Errror....Values are not set in variables...!!!";
?>
( ! ) Notice: Undefined index: name1 in C:\wamp2\www\PHP_project\php_register.php on line 2
Call Stack
Time Memory Function Location
1 0.0022 131712 {main}( ) ...\php_register.php:0
( ! ) Notice: Undefined index: name2 in C:\wamp2\www\PHP_project\php_register.php on line 3
Call Stack
Time Memory Function Location
1 0.0022 131712 {main}( ) ...\php_register.php:0
Errror....Values are not set in variables...!!!
You need to add method="post" to your <form> tag.
Also you could a check if those values are set before on your php_register.php page to ensure the script will run successfully.
e.g.
if (empty($_POST['name1']) || empty($POST['name2']) {
// some kind of error setting and redirecting back maybe
}
By default, form method is GET but you are trying to get values By $_POST.
You didn't define form POST method then you can get values by $_GET or $_REQUEST as:
$_GET['name1'];
$_GET['name2'];
or
$_REQUEST['name1'];
$_REQUEST['name2'];
If you want to use $_POST then you should define form method="post"
If you have no idea about form method, It's a better way to use $_REQUEST. By $_REQUEST you can get both type values.
Example:
$fname=isset($_POST['name1'])?$_POST['name1']:'';
$lname=isset($_POST['name2'])?$_POST['name2']:'';
$conn=mysqli_connect("localhost","root","","test");
if (!empty($fname) && !empty($lname))
{
mysqli_query($conn, "insert into test_table(firstname,lastname)
values ('$fname','$lname')");
}
else
echo "<br> Errror....Values are not set in variables...!!!";
u don't declare POST or GET in the form ,but u use $_POST['name1'] to get the value, thats where the error occured.

PHP - Undefined index - $_POST hidden input

I'm a beginner at coding, and I have to make a small game in php for school.
I have to use a hidden input, like this:
<form action="process.php" method="post">
<input type="hidden" name="rand" value="<?php rand(1,10); ?>" />
</form>
The value stands for a random number between 1 and 10 (I hope the value is correct). Now, in process.php I want to retrieve the random number by using post, so what I tried to do is the following:
<?php $random = $_POST['rand'];
echo $random; ?>
In my browser (Firefox), I'm getting the following error:
Notice: Undefined index: rand in
G:\xampp\htdocs\process.php on line 2
Does anyone know how I can echo the hidden value without using complex techniques?
Thanks in advance,
Maxime
You haven't echoed your rand function within the hidden input tag.
Also, there should be a submit button. Only then you can access the POST parameters.
<input type="hidden" name="rand" value="<?php echo rand(1,10); ?>" />
<input type="submit" name="submit" value="submit">
Try something like this:
if (isset($_POST['submit'])) {
echo "<pre>";
print_r($_POST); // See your POST array
$random = $_POST['rand'];
echo $random;
}
Hope this helps.
Peace! xD

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.

PHP: Undefined index in $_POST

I am using forms select. I just want to check what user selects by echo-ing the result on the same page so I kept the action="". But its showing error undefined index slct. Can any one please help me
<form action="" method="post">
<select name="slct">
<option value="yes" selected="selected"> yes </option>
<option value="no"> no </option>
</select>
<input type="button" value="Submit" />
</form>
<?php
$tofd = $_POST["slct"];
echo $tofd;
?>
Why its showing the error
Notice: Undefined index: slct in C:\wamp\www\Univ Assignment\Untitled-4.php on line 21
You should use button type submit NOT button
<input type="submit" value="submit" />
And then test IT like
echo (isset($_POST['slct']))? $_POST['slct'] : 'Variable undefined..';
Use PHP isset to check if its exist first
Example :
$tofd = isset($_POST["slct"]) ? $_POST["slct"] : null ;
Example 2 Using a function
function __POST($var)
{
return isset($_POST[$var]) ? $_POST[$var] : null ;
}
$tofd = __POST("slct");
If they are on the same page, initaially, $_POST would be empty because the user has not posted anything. So you have to handle that.
if(isset($_POST["slct"]))
$tofd = $_POST["slct"];
<?php
if (isset($_POST["slct"])){
$tofd = $_POST["slct"];
echo $tofd; }
?>

Categories