So you can include for example functions.php in your file and then you can use different functions in that page to validate user input.
But how can I process multiple forms using the same file?
Example:
include formprocessor.php
formprocessor would contain all the functions related to each form:
Form A action=formAfunction()
Form B action=formBfunction()
Form C action=formCfunction()
Can formprocessor.php contain a function specifically for handling each form?
Each form would have their own function to process their data.
If this is possible it is way more convenient than having a processor file for each form on a site. 10 forms 10 processing files?
Or 10 forms 1 processing file.
You can see an input type=hidden that contains a name for each form, then in your form processor, read the name that was submitted and go to the specific part of the code accordingly.
You could add a name to the submit button for each form in html
<form method='post' ... >
....
<input type='submit' name='formA' ...>
</form>
<form method='post' ... >
....
<input type='submit' name='formB' ...>
</form>
Then use something like this in your php code to handle it:
if(isset($_POST['formA'])){
formAfunction(); //handle formA
}elseif(isset($_POST['formB'])){
formBfunction(); //handle formB
} //.... etc
Have a hidden process inside your form. IE
<form id = "login" action = "calls.php" method = "POST">
<input id="process" name="process" type="hidden" value="loginpr">
<input type="submit">
</form>
<form id = "logout" action = "calls.php" method= "POST">
<input id="process" name="process" type="hidden" value="logoutpr">
<input type="submit>
</form>
Then you have your calls.php file which switches based off the the process called.
if(isset($_POST['process'])){
switch($_POST['process']) {
case 'loginpr':
loginfunction();
break;
case 'logoutpr':
logoutfunction();
break;
}
}
else { echo "The process is not linked within this form"; }
calls.php would be your formproccessor.php file
Related
This question already has answers here:
Multiple forms and one processing page
(3 answers)
Closed 4 years ago.
In PHP you can define input's name just like this: if (isset($_POST['name'])) {}, but my question is how can I define exactly this post (<form method='post' action='action.php'> with this name (name='post_name') exists.
So the whole HTML would be like this:
<form method='post' name='post_name' action='action.php'><input name='name' /></form>
I'm trying to achieve this, because I have more forms on my page and I want to work with exactly this one. I can theoretically change action='', but I want to have it in same file or do this: if (isset($_post_name)) {}, but it doesn't work.
To identify the each submitted form, you can use:
As hidden input field.
Set a name or value of the submit button.
<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="frmname" value=""/>
</form>
You can use the same action file by using a GET parameter. Instead of naming the form element itself you can change the action this way:
<form method='post' action='action.php?formname=form1'>...</form>
<form method='post' action='action.php?formname=form2'>...</form>
Then in php:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($_GET['formname'] == 'form1') {
//stuff
}
else if($_GET['formname'] == 'form2' {
//other stuff
}
}
Or you can simply add an hidden input element to define which form is posted.
I am sorry if has any unclear sentences beacuse I am bad in explaining thingy..
So i have three sample file name.html, time.html and insert.php.. in name.html, after i click the button and it will pop up another window (time.html). Then in time.html after i click the submit button, the insert.php will execute.
name.html
<form method = "post">
<h3>Name: </h3><input type ="text" name= "student_name" id="studentName">
<input type="button" name="submit">
</form>
time.html
<form method="post" action="insert.php">
<input type="time" name="time_name" id="my_time">
<br><br>
<input type="submit" name="myButton" id="myButton">
</form>
insert.php
<?php
$name = $_POST['student_name'];
$time = $_POST['time_name'];
if(mysqli_query($connect, "INSERT INTO student (student_name, time)
VALUES ('$name', '$time')")){
....}else{ ...}
My question: Is there a way that the insert.php can retrieve both name and time information in the html files but not only in time.html.
If I add in the insert.php file in name.html, the php file will execute first.
<form method = "post" action= "insert.php">
<h3>Name: </h3><input type ="text" name= "student_name" id="studentName">
<input type="button" name="submit">
</form>
::Most of the sentence makes no sense, if any unclear sentence please let me know.. Also, i couldn't think any better title for this question..
Edit: Seems like i found the solution.. I just add an action in the name.php.
In name.html i change to name.php
<form method = "post" action = "<? include 'time.php' ?>" >
<h3>Name: </h3><input type ="text" name= "student_name" id="studentName">
<input type="button" name="submit">
</form>
In time.html change to time.php
Thanks for helping me out!!
Your question: Is there a way that the insert.php can retrieve both name
and time information in the html files but not only in time.html.
If I add in the insert.php file in name.html, the php file will
execute first.
The way you explained is a bit confusing ! but anyway if you want include the php to your name.html !
1. Either instruct Apache to treat .html files as PHP(See This) or make it as a .php file
and if you want to restrict your php code from executing first or automatically ! update your code with
if(isset($_POST['something'])){ //all other stuff here }
On your code
<?php
if(isset($_POST['submit'])) { //executes only when the submit button clicked !
$name = $_POST['student_name'];
$time = $_POST['time_name'];
if(mysqli_query($connect, "INSERT INTO student (student_name, time)
VALUES ('$name', '$time')")){
....}else{ ...}
You can also make some more conditions to check whether $name & $time values are entered or not also simply you can validate it with making it as required in html itself
Also in a better way you can do the same with AJAX and some JS See the answer here
You have two files with two forms. And you have 1 php file to process both. And you want the data from both files in your insert.
Then why don't you create only 1 file, 1 form, ask for both info, insert once.
So your form would become:
<form action="index.php" method="POST">
<h3>Name: </h3><input type="text" name="student_name" id="studentName">
<h3>Time: </h3><input type="time" name="time_name" id="my_time">
<input type="button" name="submit">
</form>
Obviously this above code is to illustrate, I do not take care of everything. Then you have to check SQL injection, validate your inputs, ...
As far as I know, there is no way to get data from 2 forms into a single action, since every time you click submit, it executes the action.
Unless your first form defines it's action as the second form. Then the second form creates a to pass the result of the first form along with it's input to the action file that does the insert to database.
I'm echoing out a form that uses the method GET to send material via several input type="hidden". Like this:
echo '
<form action="somewhere.php" method="GET" name="whatever" id="whatever">
<input type="hidden" name="get_method" value=" . '$foo' . ">
<input type="hidden" name="want_to_send_with_post" value=" . '$my_post' . ">
</form>
';
This works fine, but how do I send one of these input types as POST, since it's the form that determines the method? Is there a way that an individual input type can override the form method? I know that an input button can override the form method (see here), but that's creating a separate entity. I want to send both GET and Post simultaneously.
The reason: I want to send category names with Get and a text message with POST.
Yeah, you can! Specify the GET data in the URL you're heading to.
(I would edit the GET info with javascript as the user inputs their data)
<input type="text" id="get"> <!--user types get stuff here-->
<form action="somewhere.php?get=something" method="POST" id="form">
<input type="hidden" name="post" value="<?= $mypost ?>" />
</form>
Use JS to change the GET action
var form = doc.getId("form")
var get = doc.getId("get").onkeyup = updateLoc;
function updateLoc() {
form.action = "somewhere.php?get=" + get.value;
}
Then PHP can deal with $_POST["post"] and $_GET["get"] separately.
As the title says This is the code that I tried with. The forms must appear one by one because information from previous forms determine how the next ones will look.
$(document).ready(function(){
$('#first_form').submit(function(){
$('#first_form').fadeOut('fast');
$('#second_form').fadeIn('fast');
});
});
<form action="new_patch.php" method="POST" id="first_form">
Title: <input type="text" name="patch" placeholder="Patch 4.20">
<br/>
Number of Champions: <input type="number" name="champ_number" min="1" max="99">
<br/>
<input type="submit" value="submit">
</form>
<form action="new_patch.php" method="POST" id="second_form" style="display: none;" >
<input type="text" value="text">
<input type="submit" value="submit">
<?php
$champ_number = null;
if(isset($_POST['champ_number']))
{
$champ_number = $_POST['champ_number'];
for($champ_number;$champ_number>0;$champ_number--)
{
echo "<br/>Champion ".$champ_number."<input type=\"number\" name=".$champ_number." min=\"1\" max=\"99\">";
}
}
?>
</form>
You're mixing client-side and server-side form code. Submitting the form will reload the page entirely, so from the looks of your code it will fade in the new form when the old form is submitted, but then reload the page so the old form will show again anyway.
You could either:
Let the PHP determine how the next form appears based on the submission of the first form, e.g. if (isset($_POST["First_form_submit"]) { Show second form... }
Probably better and more user-friendly: make the second form appear below once the user has filled in the relevant inputs on the first form before they've submitted
you can use:
$('#first_form').submit(function(){
$('#first_form').fadeOut(function() {
$('#second_form').fadeIn('fast');
});
return false;
});
From the jQuery documentation the syntax is fadeIn( [duration ] [, complete ] ) it accepts a duration and a onComplete callback that you can use to execute the next action when the first is completed.
I did this once too, just add a submit class to the button and make it like this:
<input type="submit" value="submit" class="submit">
Change script to a click function.
$(document).ready(function(event){
event.preventDefault();
$('.submit').click(function(){
$('#first_form').fadeOut(400);
$('#second_form').fadeIn(400);
});
});
PS, also you need to prevent submit default...otherwise it will just submit the form, see this JSfiddle
When I create i form - I do something like this:
<form name="form-name" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
[...some elements...]
<input type="submit" name="form-name" value="button">
</form>
Now I need to get the value of the name="" of the submit button, and not the actual value="".
In this case : "form-name".
And here's why:
When I submit a form; I write the action to database - and therefor need the name of the form submitted.
I know I can just have a hidden field with the form name. But I would like to make it simpler by just extracting the name from the submit button because I have a couple of other hidden form elements that I need to add on every single form I create to make my template system work.
And no javascript...
So, let's say your HTML form is this:
<form name="form-name" method="post" action="">
<input type="submit" name="form-name" value="button">
</form>
And you want to get what is inside name="form-name" in this case the form-name
Well, then in the PHP side you can, treat the $_POST global as associative array, and extract the key from it like this:
<?php
if(isset($_POST)){
foreach($_POST as $key=>$each){
echo $key; // this will output "form-name"
}
}
I might have come up with a solution to my question...
Here's a example form:
<form name="vehicle-vinNr" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
<input type="hidden" name="hello" value="world" readonly>
<input type="text" name="element">
<input type="submit" name="vehicle-vinNr" value="send">
</form>
First I need to extract and place the element-names into a new array:
<?php
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
$_FORM_ELEMENT_names[] = $_FORM_ELEMENT_name;
}
}
?>
In this case the array now contains:
hello
element
vehicle-vinNr
If the submit-button is, and always is, the last element in the form - this would work:
$_FORM_name = end($_FORM_ELEMENT_names); // vehicle-vinNr
But sometimes the submit-button is not the last element, so I needed to make a change:
If I always start the name of the submit-button with submit_ - e.g. submit__vehicle-vinNr or with multiple submit buttons for different actions like submit_update__vehicle-vinNr/submit_delete_vehicle-vinNr I can just do this:
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
if(strstr($_FORM_ELEMENT_name,'submit_')){
$_FORM_ELEMENT_submit_name = explode('__',$_FORM_ELEMENT_name);
$_FORM_name = $_FORM_ELEMENT_submit_name[1]; // vehicle-vinNr
}
}
}
This is the solution I came up with - any thoughts?