Reputation: 679
i have the following form in html:
<form id="signguestbook" class="form-horizontal" action="upload.php" method="post" enctype="multipart/form-data">
<div class="control-group">
<label class="control-label" for="inputName">Uw naam</label>
<div class="controls"><input type="text" id="inputName" placeholder="Uw naam"></div>
</div>
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls"><input type="text" id="inputEmail" placeholder="Email"><span class="maildisclaimer">(uw e-mailadres zal niet op onze site verschijnen)</span></div>
</div>
<div class="control-group">
<label class="control-label" for="inputText">Uw ervaring:</label>
<div class="controls"><textarea rows="5" cols="50" id="inputText" placeholder="Geef hier mee hoe u uw verblijf ervaren heeft!"></textarea></div>
</div>
<div class="control-group">
<label class="control-label" for="inputPhoto">Foto:</label>
<div class="controls"><input type="file" name="photo" id="photo" /></div>
</div>
<div class="control-group">
<div class="controls"><input type="submit" class="btn" value="Versturen" /></div>
</div>
</form>
With twitter bootstrap it looks OK with that lay-out.
Then I have a upload.php with following test code:
<?php
if ($_POST["inputName"]) {
echo "Naam: " . $_POST["inputName"] . "<br />";
}
if ($_POST["inputEmail"]) {
echo "Mail: " . $_POST["inputEmail"] . "<br />";
}
if ($_POST["inputText"]) {
echo "Text: " . $_POST["inputText"] . "<br />";
}
// photo is the name of our file input field
if ($_FILES['photo']['error'] > 0) {
echo "Error: " . $_FILES['photo']['error'] . "<br />";
} else {
echo "File name: " . $_FILES['photo']['name'] . "<br />";
echo "File type: " . $_FILES['photo']['type'] . "<br />";
echo "File size: " . ($_FILES['photo']['size'] / 1024) . " Kb<br />";
echo "Temp path: " . $_FILES['photo']['tmp_name'];
}
?>
I didn't expect any errors here but I get the following:
Notice: Undefined index: inputName in /var/www/greze/upload.php on line 3
Notice: Undefined index: inputEmail in /var/www/greze/upload.php on line 6
Notice: Undefined index: inputText in /var/www/greze/upload.php on line 9
File name: 3479331713.pdf
File type: application/pdf
File size: 230.8388671875 Kb
Temp path: /tmp/phpJ4z0FV
My file gets recognized, but no $_POST variables. When I do var_dump($_POST) its an empty array. So it should be something in my form, but I've set it to post and the enctype is correct (as I recall)....
I must admit that my coding is off and it's been a while.
Thanks in advance!!
Upvotes: 3
Views: 4407
Reputation: 76646
Your input fields does not have a name
attribute. Change it to:
<input type="text" id="inputName" name="inputName" placeholder="Uw naam">
and
<input type="text" id="inputEmail" name="inputEmail" placeholder="Email">
Also, make sure you add a name attribute for your submit button for the use-case mentioned below:
<input type="submit" name="submitButton" class="btn" value="Versturen" />
I also recommend using isset()
to check if the form was actually submitted:
if(isset($_POST['submitButton'])) {
// your PHP code here
}
That way, you can make sure that the form is submitted before trying to access the form input values.
And, in your PHP code, you're using $_POST
values over and over. Why not store them as variables and re-use as needed?
With the corrections, your PHP code should look like:
if(isset($_POST['submit'])) { //if form submitted
$name = $_POST["inputName"];
$email = $_POST["inputEmail"];
$text = $_POST["inputText"];
if ($name) {
echo "Naam: " . $name . "<br />";
}
if ($email) {
echo "Mail: " . $_POST["inputEmail"] . "<br />";
}
if ($text) {
echo "Text: " . $text . "<br />";
}
// photo is the name of our file input field
if ($_FILES['photo']['error'] > 0) {
echo "Error: " . $_FILES['photo']['error'] . "<br />";
} else {
echo "File name: " . $_FILES['photo']['name'] . "<br />";
echo "File type: " . $_FILES['photo']['type'] . "<br />";
echo "File size: " . ($_FILES['photo']['size'] / 1024) . " Kb<br />";
echo "Temp path: " . $_FILES['photo']['tmp_name'];
}
}
Hope this helps!
Upvotes: 1
Reputation: 15656
These are just notices. They appear because you try to use $_POST
array with an index that doensn't exist.
Instead of
if ($_POST["inputName"]) {
use
if (!empty($_POST["inputName"])) {
Other will tell you to use isset()
, but it won't do what you want. isset()
function just checks if variable exists. But here you also expect this variable to ba boolean true, which mans they can't be empty too. Here, empty
function checks both if variable exists and is not empty.
Moreover there is no name
attribute in your input
elements. They are identidied by name
and not id
as you expect.
Upvotes: 3
Reputation: 11656
Try using the isset
method to test whether the POST global variable has been populated.
For example:
if (isset($_POST["inputName"])) {
echo "Naam: " . $_POST["inputName"] . "<br />";
}
Also, give the <input>
elements a name
value which is the same as the POST variable that you're expecting with the request.
Upvotes: 2