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 HTML form where a user fills out a few details to register for a competition. I want them to be able to upload a photo. The PHP script successfully enters everything into the database except Photo Name and Photo, any ideas why?
HTML:
<form method="post" action="addboat.php">
<p>Add your boat to the 2018 species hunt</p>
<p>Boat Name :- <input type="text" name="boatname" size="42"></p>
<p>Boat Make/Model :- <input type="text" name="boatmake" size="42"></p>
<p>Skipper :- <input type="text" name="skipper"></p>
<p>Photo:- <input type="file" name="file"></p>
<input type="hidden" name="huntyear" value="2018">
<p align="center"><input type="submit" value="Submit" name="B1"></p>
<p align="center"> </p>
</form>
PHP:
$huntyear = $_POST['huntyear'];
$boatname = $_POST['boatname'] ;
$boatmake = $_POST['boatmake'];
$skipper = $_POST['skipper'];
// Image add
$imagename=$_FILES["file"]["name"];
//Get the content of the image and then add slashes to it
$imagetmp=addslashes (file_get_contents($_FILES['file']['tmp_name']));
$query_rsCatch = "INSERT INTO SpeciesHuntBoats (Year, BoatName, BoatMake, Skipper, PhotoName, Photo) VALUES
('$huntyear','$boatname','$boatmake','$skipper','$imagename','$imagetmp')";
$rsCatch = mysql_query($query_rsCatch, $webdb) or die(mysql_error());
You should use multipart form-data in your form
<form action="action_page" method="post" enctype="multipart/form-data">
When you make a POST request, you need to encode the data that forms the body of the request HTML forms provides multipart/form-data is significantly more complicated but it allows entire files to be included in the data. enctype='multipart/form-data’ is used when you want to upload a file (images, text files etc.) to the server.
Related
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 9 months ago.
The problem
The $_FILES['file'] array is set, yet it's empty whenever I try to use it.
What I tried
Googling
Setting file upload to On in php.ini (both Xampp and project file root)
Uploading one file at a time (just a wild try at fixing the problem
Debugging the entire code for a month trying to solve this problem
What I know for a fact
The path to the onSubmit is correct
The name of the input in the form and the name after $_FILES['file'] is exactly the same
The form has all it's required attributes
The input has type="file" and multiple in it
My code for the form(HTML) and the file engine(PHP)
<html>
<form method="POST" action="../php/post.php" enctype="multipart/form-data">
<h3>Title</h3>
<input type="hidden" name="case" value=1>
<input type="title" name="pname">
<h3>Message</h3>
<input type="message" name="pmsg">
<h3>Images</h3>
<input type="file" name="pimg[]" multiple>
<input class="submit" type="submit" value="Upload">
</form>
</html>
PHP
<?php
if (!empty($_FILES['file']['pimg'])){
$noFiles = 1;
echo "Files found...\n";
} else {
$noFiles = 0;
echo "Files not found...\n";
echo (!empty($_FILES['file']['pimg']));
echo $_FILES['file']['pimg'][0];
}
?>
Output
The If determines the array is empty, the last echo causes an error
You're not far off.
$_FILES['file']['pimg'] should be $_FILES['pimg']
You should also check to see if there is something selected. $_FILES['pimg']['size'] checks the filesize. If 0, nothing is selected
This way works.
<?php
if ($_FILES['pimg']['size'] != 0){
$noFiles = 1;
echo "Files found...\n";
echo $_FILES['pimg']['name'][0];
} else {
$noFiles = 0;
echo "Files not found...\n";
/* This will always be empty placed here as no files have been found */
//echo (!empty($_FILES['file']['pimg']));
//echo $_FILES['file']['pimg']['name'][0];
}
?>
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.
i am building a multi step login page like that of gmail,
i want the email address the the user typed in to be stored in a global variable so i can echo it in the next step of the form i have been trying but can't figure it out, have seen many error, when i change the code i get a different one the new one i have now is (Notice: Undefined index: email in C:\xampp\htdocs\xxxxx\index.php on line 3)
this is the form
<form action="" method="post" class="form-login">
<div class="step-login step-one">
<input type="text" class="email"/>
<input type="button" class="btn next-step" value="next">
</div>
</form>
this is the php code that i am trying to use and store the input, i think this is where the problem is
<?php
$emails = $_POST['email'];
if(isset($_POST['next'])){
$GLOBALS['email'] = $_GET['email'];
}
?>
this is the code where i am trying to echo the varible
<div class="data-user-find">
<p class="user-email"><?php echo $GLOBALS['email']; ?></p>
</div>
Please guys help me
The "value" of your button is "next", but your button isn't called "next", it should be <button name="next" value="next" type="submit">
Then your error would go away. Also, you define an $email variable outside of your if-clause, please put it inside your if clause, so your code would be this:
<?php if(isset($_POST["next"]) {
$email = $_POST["email"]; (DONT FORGET TO GIVE YOUR MAIL INPUT THE 'name="mail"' TAG)
$GLOBALS["email"] = $email;
} ?>
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 don't wanna use an action page. I wanna post on the same page.
These are my codes. But there is an error called "Undefined index: baslik on line 5 and aciklama on line 6
<?php
include("baglan.php");
$site_basligi = mysqli_real_escape_string($conn, $_POST['baslik']);
$site_aciklamasi = mysqli_real_escape_string($conn, $_POST['aciklama']);
$ayarsql = "UPDATE ayar SET baslik='$site_basligi', aciklama='$site_aciklamasi' WHERE durum='1'";
if($conn->query($ayarsql)){
echo "Güncelleme başarılı";
}
?>
<form action="" method="post">
Site Başlığı: <input type="text" name="baslik" ><br><br>
Açıklama: <input type="text" name="aciklama"><br><br>
<input type="submit">
</form>
<?php
include("baglan.php");
// check for post request here
if(isset($_POST['baslik']) && $_POST['baslik'] != "") {
$site_basligi = mysqli_real_escape_string($conn, $_POST['baslik']);
$site_aciklamasi = mysqli_real_escape_string($conn, $_POST['aciklama']);
$ayarsql = "UPDATE ayar SET baslik='$site_basligi', aciklama='$site_aciklamasi' WHERE durum='1'";
if($conn->query($ayarsql)){
echo "Güncelleme başarılı";
}
}
?>
<form action="" method="post">
Site Başlığı: <input type="text" name="baslik" ><br><br>
Açıklama: <input type="text" name="aciklama"><br><br>
<input type="submit">
</form>
You are not checking if the POST data actually exists, so when you first load the page there is no $_POST
do something like
if (isset($_POST)){
//your php code here
}
I can see the form IS submitting to the same page.
You just should wrap the PHP code in if condition
<?php
include("baglan.php");
if(!empty($_POST['baslik'])) { // This is the new if condition
$site_basligi = mysqli_real_escape_string($conn, $_POST['baslik'] );
$site_aciklamasi = mysqli_real_escape_string($conn, $_POST['aciklama']);
$ayarsql = "UPDATE ayar SET baslik='$site_basligi', aciklama='$site_aciklamasi' WHERE durum='1'";
if($conn->query($ayarsql)) {
echo "Güncelleme başarılı";
}
}
?>
<form action="" method="post">
Site Başlığı: <input type="text" name="baslik" ><br><br>
Açıklama: <input type="text" name="aciklama"><br><br>
<input type="submit">
</form>
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 6 years ago.
I have this issue that my form obviously doesn't send data with POST method but it sends it with GET method.
Here is my HTML code of the form
<form action="action.php" method="POST">
<input type="text" name="text">
<input type="submit" value="send">
</form>
and here is the php code in the action page
if($_SERVER['REQUEST_METHOD'] == 'POST'){
echo $_POST['text'];
var_dump($_POST);
}
if(isset($_POST['text'])){
echo "ok";
}else{
echo "no";
}
when I submit the form I get this error for output
Notice: Undefined index: text in F:\test\action.php on line 9
array(0) { } no
but when I send data with GET method it works correctly without any problem.
I think the problem is for phpstorm because it runs correctly in the xampp server. and the considerable thing is when I run it in mozila or IE it says page not found but xampp is okay.
Try using isset with your input like so:
You have to add name="something" for the isset to pick up that you have clicked it.
<?php
if (isset($_POST['sub']))
{
echo $_POST['text'];
}
?>
<form action="" method="post">
<input type="text" name="text">
<input type="submit" name="sub" value="Submit">
</form
I can only assume that the output you are seeing is before you submit the form. When you submit it, you should check for POST and not for post:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
^^^^ here
echo $_POST['text'];
}
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.
I have the code:
echo('
<div class="row">
<div class="col-sm-2" >
<form method="POST" action="plan.php">
<p>Selectati data raportului:<br>
<select name="SelectDR" onchange="form.submit()">
<option value="">Select...</option>
');
It is a form from which I select a value.
$option = isset($_POST['SelectDR']) ? $_POST['SelectDR'] : false;
if ($option){
$queryTXT = "SELECT * FROM ".$tabel." WHERE DataRaport='".$option."'";
...
The records of a SQLtable are filtered by the value selected from the form.
Now, what I want is to display in the url plan.php?DataRaport& variable php
I've tried to put in the form url (<form method="POST" action="plan.php?DataRaport'.$option.'">), but that doesn't show the variable.
You need GET not POST
<form method="GET">
First of all. If you want to echo this HTML every time, just use it as HTML:
<div class="row">
<div class="col-sm-2" >
<form method="POST" action="plan.php">
<p>Selectati data raportului:<br>
<select name="SelectDR" onchange="form.submit()">
<option value="">Select...</option>
</select>
</form>
<?php //your php stuff
To get the value from the URL, you need to change one thing:
<form method="POST" action="plan.php?DataReport=<?php echo $option?>">
Then you would get this variable as:
$dataReport = $_GET['DataReport'];
This would return the option value as POST and the DataReport as GET.
You can change the form method to GET if you wish, but you would then need to use $_GET to pick the selected option.