How can I get a PHP value from an HTML form? - php

I have an HTML form as follows:
<form enctype="multipart/form-data" action=" " method="post">
<input name="username" type="text"/>
<input type="submit" value="Upload" class="btn btn-primary"/><br/>
</form>
I want that the user of this form enters data in the input box. Then I would like this data to be the value of a PHP string - e.g. $username = "MY_NAME"; where MY_NAME is the value of the HTML form entered by the user.
If the input by the user in the input box is e.g. "STACKOVERFLOW" I want the PHP string to be $username = "STACKOVERFLOW";

When the form submits, you need to get the values from the $_POST array
You can do print_r($_POST) to see everything it contains (all of the form fields) and reference them individually as well.
Username will be $_POST['username']
I recommend reading a tutorial on working with forms and PHP... here's a good one
Since you're obviously a beginner I'll help you out a bit more:
Give your submit button a name:
<form enctype="multipart/form-data" action="" method="post">
<input name="username" type="text"/>
<input type="submit" name="submit" value="Upload" class="btn btn-primary"/><br/>
</form>
Because action is blank, it will POST to the current page. At the top of your file, you can check to see if the form was submitted by checking if $_POST['submit'] is set (I gave your submit button that name).
if(isset($_POST['submit'])) {
// form submitted, now we can look at the data that came through
// the value inside the brackets comes from the name attribute of the input field. (just like submit above)
$username = $_POST['username'];
// Now you can do whatever with this variable.
}
// close the PHP tag and your HTML will be below

First check if the form has been submitted:
<form enctype="multipart/form-data" action=" " method="post">
<input name="username" type="text"/>
<input type="submit" name="Submit" value="Upload" class="btn btn-primary"/><br/>
</form>
if($_POST['Submit'] == "Upload")
{
$username = $_POST['username'];
}

Related

A working php form

I am unable to find a simple working php form anywhere on the net. There is plenty of forms that are in the same file as the rest of the HTML code, but it is just plain confusing when you have 2 or 3 forms in the same index.html file.
What I'm looking for is just a basic HTML form with method='post' and action attribute set to action='action.php' , and then making it work in that .php file.
You can put HTML code in PHP file. Here is an example how the code works.
HTML
<form action="post" action="action.php">
<input type="submit" name="test"/>
</form>
To check if a form has been submitted or not, use this
PHP
if ($_POST['test']) {} // the key "test" should be the same as the name "test" in input type submit.
Consider you here three form in single html/php page like
<form action="post" action="action.php">
<input type="submit" name="signin_form"/>
</form>
<form action="post" action="action.php">
<input type="submit" name="signup_form"/>
</form>
<form action="post" action="action.php">
<input type="submit" name="password_reset_form"/>
</form>
you can see the name attribute on submit button now when user will submit the form the $_POST super global will have the value of submitted button name. and you can check this using this something like
<?php
if(isset($_POST['signin_form']))
{
// user submitted the sign in form
}
if(isset($_POST['signup_form']))
{
// user submitted the sign up form
}
if(isset($_POST['password_reset_form']))
{
// user submitted the password reset form form
}
?>
you can also add a hidden field in each form with the value of form name like
<form action="post" action="action.php">
<input type="hidden" name="form_type" value="sign_in"/>
<input type="submit" value="submit"/>
</form>
<form action="post" action="action.php">
<input type="hidden" name="form_type" value="sign_up"/>
<input type="submit" value="submit"/>
</form>
<form action="post" action="action.php">
<input type="hidden" name="form_type" value="password_reset"/>
<input type="submit" value="submit"/>
</form>
now you can check in same manner
<?php
if(isset($_POST['form_type']) && $_POST['form_type'] == 'sign_in')
{
// user submitted the sign in form
}
if(isset($_POST['form_type']) && $_POST['form_type'] == 'sign_up')
{
// user submitted the sign up form
}
if(isset($_POST['form_type']) && $_POST['form_type'] == 'password_reset')
{
// user submitted the password reset form form
}
?>

$_POST doesn't work with my form

Now i made a simple form with 2 input one for name and one for button
i want in php file echo name input
<form action="del.php">
<input type="text" name="name" />
<input type="submit" name="btn" value="button"/>
</form>
in del.php
<?php
session_start();
$_SESSION['name'] = $_POST['name'];
echo "".$_POST['name']."";
giving me white screen when every time
i want to echo any thing that typed in this input
You are missing method attribute in your form.
<form action="del.php" method="POST">
<input type="text" name="name" />
<input type="submit" name="btn" value="button"/>
</form>
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute).
The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post").
Source: https://www.w3schools.com/tags/att_form_method.asp
the default method when a form is submitted is GET, you need to specify the POST method (method="post") in your <form> tag:
<form action="del.php" method="post">
Your form is not POSTing its GETing (see the url params which will be set example.com?name=) this is because you not set the from method for POST:
<form action="del.php" method="post">
<input type="text" name="name" />
<input type="submit" name="btn" value="button"/>
</form>
And within your PHP you should check its post and check that the values are set:
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = isset($_POST['name']) ? $_POST['name'] : null;
// set your session - not sure you need this :/
$_SESSION['name'] = $name;
// echo out your result but also protect from XSS by using htmlentities
echo htmlentities($name);
}

HTML Form Logic Issue ( 2 Forms ) form-1 is not remembering data

Ill attempt to explain my issue - please bear with me.
I have 2 forms on a page, the fist form is populates the second form with address details but once called it loses all the details in it.
page1.php
<form action="anotherPage.php" id="database_submit" name="Form1">
<input type="text" name="Door Number" form="database_submit">
<input type="submit" value="Submit">
</form>
//when the below form is submitted it returns to this page
//and loses all data entered in the above form
<form action="page1.php" id="get_postcode" name="Form2">
<input type="text" name="getPostCode" form="get_postcode">
<input type="submit" value="Submit">
</form>
Is it possible to keep values entered in form1 when the user clicks form2, I have tried many things, but seem to have been unsuccessful (that's why I'm here). Hope you guys can help.
No you have to populate the first form fields through your php..
Get the values using post method then pass them to the form fields..
Have some hidden fields in the next form..
<input type="text" name="Door Number" form="database_submit"
style="display:none">
<input type="submit" value="Submit" style="display:none">
Pass the values from form 1 using javascript to form2 fields
Submit the form using method=post
<form action="page1.php" id="get_postcode" name="Form2" method="post">
hen access them when the page1.php reloads after form submission--
<?php
$dn = !empty($_POST['Door Number']) ? $_POST['firstname'] : '';
?>
<input type="text" name="Door Number" form="database_submit" value="<?php
echo $dn;?>">
In this way you can achieve what you want..
hope this helps!

If have two forms (or more). Is it possible to fetch which form has sent the upload request?

Form 1:
<?php
echo $this->upload_message;
?>
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit"></form>
Form 2:
<?php
echo $this->upload_message;
?>
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit"></form>
Is it possible to fetch which form is submitted without giving any extra attributes or so to above forms? The forms are created dynamically based on what user enter for amount number of forms. (In this case user has entered 2 forms)
I want to do this so $this->upload_message would be accurate only for the form that is used for uploading.
Alter the name tags on your <input type="submit"> buttons. Have one as name="submit" and the other as name="submit_two" (for example, bad naming convention), then process code as
if (isset($_POST['submit'])) {
// do stuff
} elseif (isset($_POST['submit_two'])) {
// do other stuff
}
Yes, it's possible.
The cleanest way, in my opinion, is to put an hidden input tag in each form:
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
<input type="hidden" name="active_form" value="1">
(...)
and
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
<input type="hidden" name="active_form" value="2">
(...)
then, in the page that process the form, you can check it in this way:
if( $_POST['active_form'] == 1)
{
(...)
}
elseif( $_POST['active_form'] == 2)
{
(...)
}
If your form is generated dynamically based on the user input(The forms are created dynamically based on what user enter for amount number of forms), in this case you can use three type of solution as far as I know,
You can introduce a new hidden field for each form based on the form number.
Eg:
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit">
<input type="hidden" value="1" name="form_id"/>
</form>
in php
switch($_POST['form_id']) {
//the form data to be processed..
}
or
You can update the input field submit button naming based on the form number.
Eg:
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit_{form_id}">
you can add a additional parameter in the form method.
...

A second button to start php script, how?

I have read the answer to this question, to execute PHP scripts with the click of a button. But what if I have a "nested button", like this :
<?php
if(!empty($_POST['act'])) {
echo "Ready to rock!";
$someVar = "Rock n Roll";
if(!empty($_POST['act2'])) {
echo $someVar;
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
<?php
}
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="submit" value="Show It!">
</form>
<?php } ?>
I heard my problem can be solved with jQuery, but I no idea.
anyone please.
To execute a script on the server you use the action property of your form:
<form method="POST" action="myscript.php">
When clicking a input type="submit" the browser will go to to action of the form surrounding the input type="submit"
Nesting is not a issue, as the browser always will look for the 'surrounding' form.
Problem is in second form, so it will never calls in this code, because it fails in first $_POST variable IF statement, because in second form you do not POST variable "act". so you need to add it
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
with this form you should see echo $someVar;
p.s. if form action property is emtpy, by default it submits form to the same php script
Just like #DTukans said here, you need the hidden field. If you would post the second form, the value of act will be lost if you are not having a hidden field with the value of act from the first form.
In php you can also check which submit button you submitted by giving the input[type="submit"] a name, such as <input type="submit" name="form2">, then you could check if you submitted that form by:
if (isset($_POST['form2'])) {}, but this is not the case here.
Use the hidden input and you will be good to go.

Categories