I have a regular form which submits to my database but I want to include a file upload in the form, so as to receive the form input in the expected table using PHP post and also receive the uploaded image.
Use <input type="file"> to include a file upload in the form.
Use something like this to send Files as well as other inputs to the processing page where you can access files and post variables.
<form action="#" method="post" enctype="multipart/form-data">
<input type="text" name="txtbox">
<br />
<input type="file" name="filebox">
<br />
<input type="submit" name="submit" value="submit">
</form>
When this is submitted you can access the data from $_FILES and $_POST
There are various input types in HTML just as the below:
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">
Each of the above inputs demand different conditions and formats to be satisfied.
For your query the answer is either:
<input type="file">
For webpages that require inputs in a file format. Or,
<input type="image">
For webpages requiring input in an image format.
For further details view the below link for help:
https://www.w3schools.com/html/html_form_input_types.asp
Related
How to insert and update multiple images from multiple input fields in one form with PHP Ajax.
For example:
<form>
<input type="file" name="img1">
<input type="file" name="img2">
<input type="file" name="img3">
<input type="submit" value="Save">
</form>
Put your input name into an array.
<form>
<input type="file" name="img[]">
<input type="file" name="img[]">
<input type="file" name="img[]">
<input type="submit" value="Save">
</form>
I have two submit buttons in one form and when I used the first submit, all the text input fields become blank. I try to solve this by using a session to store the textfield inputs.However, I don't know how I can put back the value to the input field that I stored in the session after the first submit is used.
The HTML code:
<form method="post" enctype="multipart/form-data">
<input type="text" name="headline" id="headline" required />
<input type="file" name="files[]" multiple>
<input type="submit" value="Upload Images" name="submit_image">
<input type="submit" name="submit_post" value="Submit Your Post"/>
</form>
The php code:
<?php
session_start();
if (isset($_POST["submit_image"])) {
if (!empty($_POST['headline'])) {
$_SESSION["headline_session"] = $_POST ["headline"];
}
}
$headline_session = $_SESSION["headline_session"];
?>
Why you want to use a session!
you can echo the value of $post parameter in a property called (value) like this
<form method="post" enctype="multipart/form-data">
<input type="text" name="headline" id="headline" value="<?php echo $_POST ['headline']; ?>" required />
<input type="file" name="files[]" multiple>
<input type="submit" value="Upload Images" name="submit_image">
<input type="submit" name="submit_post" value="Submit Your Post"/>
</form>
You can still save things to session if you need them later but I recommend just echoing out the previous value.
<form method="post" enctype="multipart/form-data">
<input type="text" name="headline" id="headline" value="<?php echo $_POST['headline']; ?>" required />
<input type="file" name="files[]" multiple>
<input type="submit" value="Upload Images" name="submit_image">
<input type="submit" name="submit_post" value="Submit Your Post"/>
</form>
Should you need to compare the previous submissions with what was already submitted then this is one way to do it.
Good work thinking forward :) happy coding
I tried all but no form is working with method or simple action like http://google.com `
<form action="http://google.com" method="post">
<input type="text" />
<input type="submit" />
</form>
even this is not working
use this
<form role="form" action="http://google.com" method="post">
<input type="text" name="text" />
<input type="submit" name="submit" />
</form>
role attribute for FORM
name attribute for input
In PHP, $_POST always accept name attribute from form elements:-
You need to write
<input type="text" name='name_of_your_field' />
instead of
<input type="text" />
You need to refer this Link.
i have
<form action = "https://creator.zoho.com/api/xml/fileupload/scope=creatorapi&authtoken=**************" method="post" enctype="multipart/form-data">
<input type="text" name="applinkname" value="sample">
<input type="text" name="formname" value="Employee">
<input type="text" name="fieldname" value="File_upload">
<input type="text" name="recordId" value="1235667754455">
<input type="text" name="filename" value="profile.jpg" >
<input type="file" name="file">
<input type="submit" value="Update">
</form>
But i must send 2500 files, how can i transform this html to php, my problem is <input type="file" name="file">, how convert it to give a value in php ?
thanks you
You can use $_REQUEST['FILE']=$SOME VARIABLE;
And name is same what you use in your database
then apply enctype="multipart/form-data".
<body>
<form method="post" action="xx.php" >
Enter Title of the Post<INPUT type="text" name="title">
<br/>
Enter Description
<textarea rows="10" cols="50" wrap="physical" name="post">
</textarea><br/>
<input type="Submit" value="Post">
<br/><br/>
<form enctype="multipart/form-data" action="xx.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload: <input name="uploaded_file" type="file" />
<input type="submit" value="Upload" />
</form>
How Do I get the user to write the text and browse the image, only after which pressing a single button would both upload the text and the file?
You can't submit two forms with the same button. You'll need to combine the two fields into a single form.
You can move the file input to the first <form> (which btw, you haven't closed) and use javascript to check if text has been entered and file has been selected.
Don't nest your form tags (It's invalid HTML). You can place all the inputs in one form so they are posted together.
e.g.
<form enctype="multipart/form-data" action="xx.php" method="post">
Enter Title of the Post<INPUT type="text" name="title">
<br/>
Enter Description
<textarea rows="10" cols="50" wrap="physical" name="post">
</textarea>
<br/>
<br/>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload: <input name="uploaded_file" type="file" />
<input type="submit" value="Upload" />
</form>
Oh, as a sidenote, you may not want to use a name like "post" in your input control.