Why won't this print "success" when I submit the form? I'm pretty sure it should.
<?php
if (count($_POST) > 0) {
echo "success!!";
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="userfile" />
<input type="submit" value="upload" />
</form>
At a guess, the submit field has no name, so it won't be included in $_POST. Your file upload will be placed in $_FILES, see Handling file uploads.
It is also good practice to NOT ommit the action attribute.
If you want the form to submit to itself, try
<form method="post" action="?" enctype="multipart/form-data">
or
<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>" enctype="multipart/form-data">
Further reading on second method Disclaimer: Link to my own blog
<?php
if (count($_POST['submit']) > 0) {
echo "success!!";
}
?>
<form method="post" enctype="multipart/form-data" action="">
<input type="file" name="userfile" />
<input type="submit" value="upload" name="submit"/>
</form>
Related
I'm trying to make a simple file upload while learning how to do so.
code looks as following:
<form action="upload.php" method="post">
<input type="file" multiple="multiple" name="files[]"
enctype="multipart/form-data"><br><br>
<input type="submit" value="upload">
</form>
<?php
echo $_FILES["files"]['name']
?php
For some reason i get 0 results, I cant figure out why. I know that when I upload more files it'd be wise to run through them with a foreach loop, but for the sake of simplicity I'd rather have the results of 1 file first.
You need to correct your html like this
<form action="upload.php" enctype="multipart/form-data" method="post">
Then in upload.php:
print_r($_FILES);
Instead of echo try with
print_r($_FILES)
Also try to add to form tag code below
enctype="multipart/form-data"
I'm assuming both the HTML form and PHP upload code is written within this file upload.php. Please correct me if I'm wrong.
Change your form HTML to:
<form action="" enctype="multipart/form-data" method="post"> <!-- Form action can be omitted. enctype attribute is needed -->
<input type="file" multiple="multiple" name="files[]"/>
<br/><br/>
<input type="submit" value="upload" name ="submit" /> <!-- Add name here -->
</form>
Modify your PHP code:
<?php
if (isset($_POST['submit'])) {
echo "<pre>";
print_r($_FILES);
}
?>
Hope this helps.
Peace! xD
Correct html for you will be:
<form action="upload.php" enctype="multipart/form-data" method="post">
<input type="file" multiple="multiple" name="files[]"/>
<br/><br/>
<input type="submit" value="upload"/>
</form>
it must work
You may also try:
var_dump($_FILES);
print_r($_FILES);
is possible to show the 2nd form? after clicked the 1st form submit button? and is there a way to hide the second form?
example
<?php
if(isset($_POST['submit']))
{ #show form 2
}
?>
<form action="" method="post" name="form1">
<input type="submit" name="submit">
</form>
if theres a way to hide form 2
<form action="" method="post" name="form2">
<input type="submit" name="submit">
</form>
thanks in advance fellow web developers
Yes. Move the condition down.
<form action="" method="post" name="form1">
<input type="submit" name="submit">
</form>
<?php if (isset($_POST['submit'])) { ?>
<form action="" method="post" name="form2">
<input type="submit" name="submit">
</form>
<?php } ?>
But note that, you have the same name in both the form's submit button, so it is better to disable the previous form's submit:
<form action="" method="post" name="form1">
<input type="submit" name="submit"<?php if (isset($_POST['submit'])) { echo ' disabled="disabled"'; } ?>>
</form>
<?php if (isset($_POST['submit'])) { ?>
<form action="" method="post" name="form2">
<input type="submit" name="submit">
</form>
<?php } ?>
Have a look at the following code:
<?php
if (isset($_POST['email']))
{
$expertmail=trim($_POST['email']);
echo $expertmail;
$expertfile=$_FILES['upfile']['tmp_name'];
echo $expertfile;
}
?>
<form action="test.php" method="post" name="users" id="users" >
<input name="upfile" id="upfile" type="file" />
<input name="email" id="email" type="text" />
<input type="submit" name="submit_button" id="submit_button" value="ΑΠΟΣΤΟΛΗ" />
</form>
Why 'echo $expertfile' does not display anything?
Thank you
POST Method Uploads gives all the information you need to handle file uploads in PHP. For your case you need: enctype="multipart/form-data":
<form action="test.php" method="post" name="users" id="users" enctype="multipart/form-data">
As Salman A points out, you will also need to check to see if a file was uploaded.
I have a form that is written in PHP that will call the page upon itself (I don't know if I say this right).
echo('</table>
<hr>
<h1 id="loadscript_h1">Voeg een loadscript toe</h1>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" enctype="multipart/form-data">
<div><input type="file" name="loadscript" id="loadscript" value="Kies loadscript"/></div>');
echo ('<input type="submit" class="formsubmit" name="upload_loadscript" value="Upload loadscripts" />
</form>');
But when I look at my website there is something written like this:
" method="POST" enctype="multipart/form-data">
How can I write this properly?
you have to use concatenation for strings. notice the '. which tells PHP to terminate string, then concatenate with the following. See the docs: http://www.php.net/manual/en/language.operators.string.php
echo '</table>
<hr>
<h1 id="loadscript_h1">Voeg een loadscript toe</h1>
<form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="POST" enctype="multipart/form-data">
<div><input type="file" name="loadscript" id="loadscript" value="Kies loadscript"/></div><input type="submit" class="formsubmit" name="upload_loadscript" value="Upload loadscripts" />
</form>';
also, you don't need parenthesis for echo.
Using PHP Heredoc
$action = htmlspecialchars($_SERVER["PHP_SELF"]);
$form = <<<FORM
</table>
<hr>
<h1 id="loadscript_h1">Voeg een loadscript toe</h1>
<form action="{$action}" method="POST" enctype="multipart/form-data">
<div><input type="file" name="loadscript" id="loadscript" value="Kies loadscript"/></div>
<input type="submit" class="formsubmit" name="upload_loadscript" value="Upload loadscripts" />
</form>
FORM;
echo $form;
You can also just leave the action field empty and the form will submit back to itself when posted.
I'm trying to create a simple fileupload form in my custom Wordpress template:
<form action="<?php echo $current_url = home_url(add_query_arg(array(),$wp->request)); ?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
On submit, the $_FILES array is empty.
I think the problem is in the action, as the url that is generated is the nice URL, instead of http://domain.com/somepost.php
What should I put into the action, so I get the $_FILES?
Try this
<?php
if(isset($_POST['submit'])){
if($_FILES["file"]["error"] > 0){
echo $_FILES["file"]["error"];
}
else{
echo $_FILES['file']["name"]; // your file name
}
}
?>
<form method="post" action="#" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" name="submit"/>
</form>