Skip file input and use only if is necessary - php

I have this little form:
<form action="?act=process" method="post" enctype="multipart/form-data">
<input type="file" name="IMG">
<input type="text" name="BG">
<button type="submit">Save</button>
</form>
How can I skip the file input if I want to edit only the text input?
I need both because sometimes i need to channge the image.
I need to skip the input after POST submit. Only if the input wasn't changed

without seeing your PHP code for "after POST submit" I am guessing that you will simply want to code in a conditional check if the IMG input is empty or not before updating your database.
so it might look something like this:
<?php
if(!empty($_POST["IMG"]))
// update database

Related

How to give one file uploaded value to input (file) tag on second page?

i use a form in which i get a file from user and submit it with button and take the values to another page with help of post(method). On the other page i have some business to do with that file beside that i have another form on that page. Now i want when submit that form (as it execute the php self script) so i want beside the new values i get that same file again. i try different technique like putting the tmp name in a variable but that doesnot work and even i create another file button in the form of that page but i am unable to give it a value. In short i need idea how to get the file on submittion of that second page form which is send from first page ?
For simplicty let me show you an example so you understand my problem,
//the first page form
<form method="post" enctype="multipart/form-data" action="uploadExcelData">
<label for="csv_file">Upload CSV File</label>
<input type="file" id="csv_file" name="csv_file" />
<input type="submit" name="upload" class="btn btn-primary"
value="upload" />
</form>
//on submit it goes to another page which have this php code and html form
<?php
$file = $_FILES['csv_file']['tmp_name'];
if (isset($_POST['save'])) { // the second form on submit
echo $file; //this show nothing
}
?>
<form method="post" enctype="multipart/form-data">
<input type="submit" name="save" class="btn btn-primary btn-block"
value="Upload" />
</form>
keeping above code in mind i hope somebody will understand the problem and suggest me something but i just summarize my code too much the second form also have many combobox etc and also i tryed to have a second file tag in second form but the problem is i cant even assign the file value to it so that it is selected automatically.
i want to get the file as output which have that shitty excel data!!

Passing variable in a URL not working

So as usually I put in information in the url leading towards another page like this:
<form action="index.php?type=question">
<input class="log-btn extra" type="submit" value="Home">
</form>
Usually it always works, I even copy pasted it because I've used the exact same url before and on the other page it works but doesn't on this one it just returns empty after the questionmark like this:
index.php?
When you want to misuse a Button as a link with URL query parameters you can do it in two ways.
In your example you try to set query parameters in the action attribute of the form. That is working as long as the method is not get, because get-parameters and URL query is exactly the same thing and submitting the form will overwrite the query with the get parameters of the form elements.
<form action="home.php?type=question" method="post">
<input class="log-btn extra" type="submit" value="Home">
</form>
You can also set <form method="get"> and use a button:
<form action="home.php" method="get">
<button type="submit" name="type" value="question">Home</button>
</form>

is there a form alternative to text-area that is multi-line(not like input field)

I would like to know if there is a multiline textarea you can put in a form that looks like this:
<form action="form.php" method="POST">
<input name="field2" type="text" />
<input type="submit" name="submit" value="Submit">
</form>
as you can see, the php script is reliant on the FORM tag, is there any way I can make the textarea actually work in the form like an input element?(what i am trying to do is use php to save a text file server side when button clicked)
Yes. Just include the following within the form tags:
<textarea name='textfield' placeholder='type your text here'></textarea>

How to get a form name in php script?

For my php file, I need to grab the unique form name.
The php file is executed when a user clicks the submit button. However, there are multiple submit button each with the same id, but they all have unique names. I need the name when they click on the submit button.
you dont want elements in html with the same id - bad practice in general. Your page will likely load normally but an html validator will notice it as an error.
html validator: http://validator.w3.org/
without seeing your code, its difficult to give you a definitive answer. if you have miltuple forms you can use hidden inputs. e.g.
<input type="hidden" name="form_name" />
Otherwise you can use javascript to put data in the form when the button is clicked. example javascript using jquery
html:
<form id="formid" >
<button type="button" id="someid" onclick="submitForm('btn1')" />
<button type="button" id="someid" onclick="submitForm('btn2')" />
<input type="hidden" id="btnsubmitid" value="" />
</form>
js:
function submitForm(btnID){
$("#btnsubmitid").val(btnID);
$("#formid").submit();
}
1 way is to put a hidden input inside of your form.
<input type="hidden" name="formName" value="[name of form]" />
then in your php, you can get it using
$form-name = $_POST['formName'];
pretty sure there are other ways, but this came to mind first.

How to retain the input file type post data

How can I redirect a page with a file POST data on it? Something like
Page1.php
<html>
<form action="page2.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="Send"/>
</form>
</html>
Page2.php
<html>
<form action="page3.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"/> ### get the value of file from page1.php and send to page 3
<input type="submit" value="Send"/>
</form>
</html>
Is it possible?
Not that way... the file is uploaded when the user submits the form. You could then base64 encode that file and embed it in the HTML as a hidden field to be submitted on the second form, but that's very inefficient.
You should handle the file upload on the first submission. Save the file somewhere or stick it in your database, whichever works better for the files you're receiving. If you save the file, I'd recommend generating a unique name for it (like a UUID). Then use the filename or the primary key from the DB as a value in a hidden field in the second form, which you can use to find the file you already received when the user submits the second form.

Categories