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
}
?>
Related
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!
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.
...
I have 2 FORMS on a single page, One below the other.
I would like to have such that second form should be always in disable mode.
and Once the first form submit button is pressed and validated second should get activated to enter the data in it.
Is there anything in PHP which can help me on this
You have 2 ways:
1) send validation of first form using ajax, and, if you receive 'true', enable second form.
2) make a POST from first form, if everything is good, set "validated" to 'true' and reload the same page. In the second form "enabling" must be only if you have $validated = true;
The logic below should help you out as a starting point:
<form method="post">
<input type="text" name="name" />
<input type="submit" name="form1" value="Proceed" />
</form>
<form method="post">
<input type="text" name="email"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
<input type="submit" name="form2" value="Submit"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
</form>
Of course, it would be much more reliable to use either AJAX to validate the first form, or to have the forms appear on separate pages.
<?php
if(isset($_POST['next'])) {
if($_POST['name']!="") {
$disabled = "";
$val = $_POST['name'];
} else {
$disabled = " disabled='disabled'";
$val="";
}
} else {
$disabled = " disabled='disabled'";
$val="";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form id="frm1" name="frm1" method="POST" action="">
<label>Name</label><input type="text" id="name" name="name" value="<?php echo $val;?>"/>
<input type="submit" name="next" id="next_frm" value="Next"/>
</form>
<form name="frm2" id="frm2" method="POST" action="">
<label>Address</label><input type="text" name="address" id="address" value="" <?php echo $disabled;?>/>
<input type="submit" name="save" id="save" value="Save" <?php echo $disabled;?>/>
</form>
</body>
</html>
This is somewhat you were looking for ,I hope
You can do it by setting a class on all inputs within second form and set them as disabled of course someone who knows a bit of javascript will be able to change it.
So you can do it as your visual layer, but then check in PHP as well if second form can be passed in case someone wanted to sneak something in.
More complicated approach would be to show images that look like form fields and only change them to inputs where the first form is submitted. That can be done on client or server side
So in reality you will have 3 forms, but one would be "fake"
Thats simple just use if else condition.
// this if condition checks whether the form 1 is submitted or not. If form1 is submitted than form 2 is displayed else form1 wil only be displayed
if(isset($_POST['submit']))
{
//Display your form 2.
}
else
{
//Display your form1.
}
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.
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'];
}