Get $_POST from type=file - php

I have form for example:
<input type="text" />
<input type="file" />
<input type="submit" />
You have to fill the text and the file, if you dont fill the text for example you have an error message, but I want to preserve what the user filled in the file's input.
How to do it with $_POST or somthing else?

I want to preserve what the user filled in the file's input.
Impossible.

NOTE:
$_POST is not the superglobal that will provide information related to 'file' inputs'. Look into you $_FILES superglobal for that kind of information.

You have to specify the "name" attribute of each input to something different:
<input type="text" name="title" />
<input type="file" name="picture"/>
<input type="submit" name="submit_button_1"/>
<input type="submit" name="submit_button_2"/>
Setting the name attribute of the submit input can be useful when you have several of them and you want to know on which one you clicked.

whatever user submits from client-side is not controlled by server-side, but you can:
use javascript to "scan" the extension name of file (.php), and prevent other files from not being able to be submitted. However this doesn't stop people renaming other files to .php.
the question is what you gonna do with the submitted php file? keep it in database? or execute it? you need to scan its content to make sure it doesn't contain malicious code. I am not very sure how php can do this, though.

Related

Use PHP to determine the type value from input

My question, it´s very short.
is it possible to detect the kind of types we send as POST from a form?
If I have some fields and one field for each input type as text, the other textarea, the other file, etc, is it possible to use Php to detect, in each case, the type from these fields.
<form method="post">
<input type="text" name="test1" value="ok">
<textarea name="text2"></textarea>
<input type="file" name="test3" value="ok">
</form>
And finally determine whether the type is text, textarea, file, etc
I know it´s possible with Jquery buy i don´t find nothing about this with php.
Thank´s for the help in advanced
No, request contains only key-value pairs. But you can add additional hidden fields that passes these data. i.e. (for larger forms I's suggest it automaticaly with JS):
<input type="hidden" name="text2_type" value="textarea">
<input type="hidden" name="test3_type" value="file">
or
<input type="hidden" name="types[text2]" value="textarea">
<input type="hidden" name="types[test3]" value="file">
Other solution
Also you can just introduce naming convention for your fields, i.e.:
<textarea name="text2_textarea"></textarea>
or
<textarea name="textarea[text2]"></textarea>
so in your PHP you can check if key ends with _texteraea or is in array $_POST['textarea'] to determine the type o field.

Cab PHP create an Input field?

I'm trying to create a cookie for a web page. The cookie value will vary based on the users name. Does PHP have an input type function? I just want to add an input field to the page an then the PHP will use that to define the users name for the page. I have the create cookie code, just can't figure out how to get the name from the screen and insert it to the cookie code. Appreciate any suggestions. This is on a WP website.
Not natively because php does not execute in browser, it executes on your server, but it can be used to write an HTML input.
The syntax would look something like this:
echo '<input type="text" name="myinput">';
or
?>
<input type="text" name="myinput">
<?php
You would then use a form post, CURL, or AJAX function to send the data back to the server where a second PHP script would process the input.
That said, it would help to post your create cookie code, since you may not even need to send it back to the server, but just handle it all in the browser using Javascript in which case your submit button only needs to pass the input to a Javascript function instead of posting it.
Is this something you are looking for?
Here it just takes the value user input from the browser and set it as a cookie
<?php
if(isset($_POST['name']) && !empty($_POST['name'])){
setcookie('setcookie_name',$_POST['name']); // setting cookie
}
?>
<form action="" method="post">
<input name="name" value="" placeholder="Enter your name" />
<input name="submit" type="submit" value="Submit"/>
</form>

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.

POST a custom Tag?

I have the following input in a form
<input name="email" type="text" id="email"size="50" english="Email address" />
I have a custom tag called english, My question is can I send this as post data and can I recover it on my new page ?
Any help would be much appreciated , Thanks
If you use JavaScript to submit your form, you can read you custom tags' values ad append them to the form data to send. Otherwise, clean HTML form just submits only input tags value.
The best method I can think of right now is to have hidden field with the label as value. Like
<input name="email_label" type="hidden" id="email_label" value="Email address" />
The short answer is: no. The post data received from the HTML in your question will be an array with email as the key, and whatever the user typed as the value.
The solution depends on the problem you're trying to solve. Consider using a hidden input tag instead. For example:
<input name="language" type="hidden" value="English" />
Alternatively, a neater solution would be to store the language in the session (assuming that does what you need). You should never rely on the front end of a website "telling" the back end stuff like this, at least to a certain degree. The back end should just "know".

How to save information in inputs inside a form so it doesn't disappear when form is submitted to itself?

I have a picture upload inside a form...
The file is a php file btw...
Problem is whenever this form is filled in, and the user clicks to upload the first picture, the form is submitted to itself and all the fields which the user may have filled in will go blank...
I know of one way to do it, alot of 'isset' in my php code, but is there any simpler or maybe better way I don't know of?
Thanks
You echo back the POST variable on your fields.
<form method="POST">
<input type="text" name="name" value="<?php echo $_POST['name']?>" />
<input type="submit" name="submit" />
</form>
When the form is submitted to self, the same data will be filled.
Well i do not know of anything else. I always use this:
<input type="text" value="<?= isset($value) ? $value : ""; ?>">
I think it is not too much code in the Templates, but it does the Trick.
Alternatively you could use some Frameworks wich abstract everything for you, but i cannot recommend some...

Categories