showing undefined index when try to submit the form [duplicate] - php

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 7 years ago.
Every time I hit submit button after selecting an image will face the problem "Notice: Undefined index: profile_picture in C:\wamp\www\Test\testImage.php on line 5", and the page print the text not selected which must be display if image is not selected.
The index is defined, but why it is behave like this, please help with your suggestions
<?php
$photo = "";
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$photo = trim(stripslashes($_POST["profile_picture"]));
if($photo != '')
{
//$photo = trim(stripslashes($_POST["profile_picture"]));
header("Location: success.php");
}
else{
echo 'not selected';
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form name="signup_form" method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<input type="file" class="profile_picture" id="profile_picture" name="profile_picture"> </br>
<input type="submit" name="submit_btn" id="submit_btn" class="submit_btn" value="sign up">
</form>
</body>
</html>

Files are not posted over $_POST.
They are posted with $_FILES array.
Reference

Related

Notice: Undefined variable: world in abc.php on line [duplicate]

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 8 months ago.
I have a html/php code as shown below in the file abc.php in which at Line#A, I am getting the following error:
Notice: Undefined variable: world in abc.php on line#A
html/php code (abc.php) :
if ('POST' == $_SERVER['REQUEST_METHOD']) {
$world = $_POST['world'];
}
<form method="post" action="abc.php?id=<?= $id; ?>">
<input type="hidden" name="world" value="<?= $world; ?>"/> //line#A
</form>
This is what I have tried but it doesn't seem to work:
if ('POST' == $_SERVER['REQUEST_METHOD']) {
$world = isset($_POST['world']);
}
The $world variable will only be defined if page served via POST method.
So, what you need is define it before the condition:
$world = "";
if ('POST' == $_SERVER['REQUEST_METHOD']) {
$world = $_POST['world'];
}
<form method="post" action="abc.php?id=<?= $id; ?>">
<input type="hidden" name="world" value="<?= $world; ?>"/> //line#A
</form>

Undefined variable: details in C:\xampp\htdocs\redo\consultation.php on line 21 in Php [duplicate]

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 2 years ago.
<?php include 'connection.php'; ?>
<?php
if (isset($_GET['consultation']))
{
echo "Consultation Details";
$no=$_GET['consultation'];
?>
<form class="" method="get">
<textarea name="details" rows="8" cols="80" placeholder="enter consultation details"></textarea><br>
<button type="submit" name="c">submit</button>
</form>
<?php
if (isset($_GET['details'])) {
$details=$_GET['details'];
}
//$details= $_GET['details'];
$insertQuery="INSERT INTO redo (consultation) VALUES ($details) WHERE no=$no;";
$insert = mysqli_query($conn,$insertQuery);
if ($insert) {
echo "recorded";
?>
Back to Total Patients
Back to Registration
<?php
}
else {
echo "record couldnt be inserted";
}
}
?>
No $_GET['details'], you sure you're using a GET method in the form? Change $_GET['details'] to $_REQUEST['details'].
To be exact, this one fails:
if (isset($_GET['details'])) {
$details=$_GET['details'];
}
I mean, the if condition is not met, so $details is not defined, as you don't define it anywhere in the code outside that if.
Other solution would be adding:
$details = '';
in front of that if.

getting data from the form created with echo in php to another php [duplicate]

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 4 years ago.
My Code Looks Like:
<?php
echo "
<form action='form.php'>
<input type='text' name='value'>
<input type='submit'>
</form>
";
?>
And form.php is
<?php
echo $_POST['value'];
?>
Error Code is:
Notice: Undefined index: value in C:\xampp\htdocs\dynamic\form.php on line 2
Is it possible to get the value of field created using echo command?I don't want to use database,jQuery etc.
You need to set the form method to post
<?php
echo "
<form action='form.php' method='post'>
<input type='text' name='value'>
<input type='submit'>
</form>
";
?>
you are missing method="post" in form tag

$_FILES array is always empty [duplicate]

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 am trying to upload some files on server with PHP and HTML.
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<body>
<form action="upload3.php" method="post" enctype="multipart/form-data">
<p><input type="file" name="upload[]"/><br></p>
<p><input type="file" name="upload[]"/><br></p>
<p><input type="file" name="upload[]"/><br></p>
<input type='submit' name ='submit' value='Upload' />
</form>
</body>
</html>
PHP:
if(!empty($_FILES['submit']['name'][0])) {
$name_arr = $_Files['upload']['name'];
$tmp_arr = $_Files['upload']['tmp_name'];
$type_arr = $_Files['upload']['type'];
$error_arr = $_Files['upload']['error'];
echo count($tmp_arr);
for($i = 0; $i < count($tmp_arr); $i++) {
if(move_uploaded_file($tmp_arr[$i], "test_uploads/" . $name_arr[$i])) {
echo "Done";
} else {
echo "Nope";
}
}
} else
echo "What's going on?";
And I always get the "What's going on?" message or "the size of" e.g. $tmp_arr "is 0".
Use a var_dump($_FILES) to find out if the files are correctly uploaded.
If it was, Then you will see the array structure for all info.
This should be avoided: $_Files, always a good practice to use correct case ($_FILES).

Undefined index: command [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
undefined index [closed]
(2 answers)
Closed 9 years ago.
These are my codes And i'm getting error
Notice: Undefined index: command in E:\xampp\htdocs\Shop_cart\products.php on line 17
Javascript:
<script language="javascript">
function addtocart(pid){
document.form1.productid.value=pid;
document.form1.command.value='add';
document.form1.submit();
}
</script>
HTML Code:
<form name="form1" action="">
<input type="hidden" name="productid" />
<input type="hidden" name="command" />
<input type="button" value="Add to Cart" onclick="addtocart(1)" />
</form>
PHP Code:
if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
$pid=$_REQUEST['productid'];
addtocart($pid,1);
header("location:shoppingcart.php");
exit();
In your PHP code add isset check to the condition:
if ( isset($_REQUEST['command']) && $_REQUEST['command'] == 'add' && $_REQUEST['productid']>0 ) {}
Change
if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
to
if(isset($_REQUEST['command']) && $_REQUEST['command']=='add' && $_REQUEST['productid']>0){
Same to other answer but I would literally proceed something like this for security purposes and better layout of my code.
//Check if request has been made
if (isset($_REQUEST['command'])){
//proceed for checking
if($_REQUEST['command'] == 'add' && $_REQUEST['productid']>0 )
{
//Your code here
}
}else{
//redirect or whatsoever
}

Categories