I have two forms form 1 and form 2.
The form 1 has 20 fields of data and it's action is to update.
In form 2 I have another action, but I want to get the 20 fields of data from form 1.
Is there any possible way to do this and how? Also form 1 and form 2 are in the same page.
I have already a solution which is putting 20 hidden fields in form 2 so form two can
get it from there which is tedious. But can I ask you guys is there a much easier idea?
You can save all the input from form 1 in a session variable and restore it in form 2. For that purpose, write a function which stores all $_GET and $_POST variables in an element of the $_SESSION array (using the parameter name as the key).
The "problem" is something that ever PHP developer has at some point solved.
The best way to do this would be to save data from previous form in $_SESSION. But this will be a valid solution, if you are filling out the form, while in the same session.
If you are building something like a e-commerce site, where user has to fill out several payment forms, or some large test-application, where students fill out multiform tests, it might be better to store data from each form in DB and associate it to UserId. This way the user gains ability to continue filling out the form from different location.
P.S. If you are using AJAX instead, you will mostly just add another layer of complexity on top of existing application. Especially if your skills in JavaScript are limited.
I think your solution isn't bad, simple and Swift !
But the only better way, is ajax.
In ajax you are not limited to a form, you can get every fields from every forms and then send them manually.
If you are not familier with ajax, and you want to use your non-ajax solution, you can use one form and put all fields of two last forms in this new single form, and use one action file to response to this form.In server side you can check the value of a field which is unique.
client side :
<form action="test.php">
<!-- fields of form 1 -->
<input type="text" name="text1"/>
<input type="text" name="text2"/>
.
.
.
<input type="text" name="text20"/>
<!-- fields of form 2 -->
<input type="text" name="text21"/>
<input type="text" name="text22"/>
.
.
.
<input type="text" name="text40"/>
<input type="submit" value="send"/>
</form>
server side:
<?php
if ( isset($_GET["text1"]) )
//do related jobs to form 1 here, and you can use all of filled fields
else
//do related jobs to form 2 here, and you can use all of filled fields
?>
Related
I'm a bit confused of do I need to prefix form input variables with i.e. ('car_', or 'bike_' corresponding to 'car_make', 'bike_make') or can I use same template for both forms without prefixing variables. And if so, do I still need to prefix the 'submit' field, or having different form name is enough to avoid data collision.
I have these two HTML forms on the same page:
<form action="" name="car_search_form" method="POST">
<input type="hidden" name="make" value="Audi" />
<input type="submit" name="car_do_search" value="Search Car" />
</form>
<form action="" name="bike_search_form" method="POST">
<input type="hidden" name="make" value="Schwinn" />
<input type="submit" name="bike_do_search" value="Search Bike" />
</form>
So, in the end I want to get correct value for $validCarMake via this code:
if(isset($_POST['car_do_search']))
{
$paramCarMake = isset($_POST['make']) ? sanitize_text_field($_POST['make']) : '';
$validCarMake = esc_sql($paramCarMake); // For SQL queries only
// TODO: Add data saving to database
}
Also, I want to understand the decision process of PHP engine. How it does deside which variable to choose - how it know on which button I clicked, and why not it does just submit all forms on the page? Also, if there would be any difference if I'd use "GET" method instead of "POST" besides that the one does put the variable values to URL? And how does the GET case it would process attached image to form submit then (as the maximum URL lenght is 255 chars as I know, and i.e. JPEG 100 kiB image contains thousands of chars. I'm asking this, because I also want to allow not just have that search on site's home page, but also allow to make search from a separate website's some kind of widget.
And the last question - if the HTML form processing differs somehow in PHP 7.X compared to PHP 5.X (i.e. PHP 5.4). I means does it caches somewhere the data, does it sends over the internet the attached images of the both forms and consumes network and server data, or it submit's only the data of the form on which I clicked the button.
As long as you keep the 2 input requests separate having the post arg "make" is completely fine. If you send the args in the same request the 2nd will override the first since it was last set.
As for how php decides on what is first it uses what is called order of precedence. This means what it comes to first it executes first unless explicitly told not to.
I want to understand the decision process of PHP engine. How it does decide which variable to choose - how it know on which button I clicked, and why not it does just submit all forms on the page?
When you click on a button, php will take the <button name="value"> value assigned in the name property of the input field or button clicked. This is how it can decide what's the form to submit. Consider that if you have two forms with the same name assigned to the submit button, the first one will override the second and php will only submit one form. This is because php execute operations with a logical order.
I have a form that i want to filled up by the end user in few steps.
in the first step i want to let them fill Name,Address,Mobile number.
And when click next button i want to appear another part of the form to fill the other details such as upload a image .
i did research online,i found 2 articles useful,
One thing is about sessions and the other thing is about hidden fields.
In my opinion i think sessions are not a good way to use for form submitting.Because some browsers are might have disabled sessions.
So ill go with hidden fields.
In my database i have a unique id which is auto increment.What i want to do is to let user to submit a form.In the first step i want them to submit their name,addres,mobile number(In this time it is inserting data to the database- Only for couple of columns.) And in my database i have a field for upload a image.In the first step it gets a null value.I want to update that null value in the next step by getting a that id from the first step
Please give me the basic idea to start this.A little help much appreciated!
Thanks in advance
you can try like this:-
You can add a class on the input which you want to hide
<input type="text" name="example" class="inputHidden">
after that add style in your page like this
<style>
.inputHidden{
display:none;
}
</style>
If you're trying to place the form data into new, hidden inputs - take the initial input.
<input type="text" name="example">
On the next page add a hidden input.
<input type="hidden" name="exampleP2" value="<?php echo $_POST['example']; ?>">
You can get form submited data and use it in input hidden fileds
for e.g. in first form you have
name ,email etc fields
in other form you have more fields
here you can use <input type="hidden" name ="firstname">
and thus you can add as many as you want
I have a simple log in form in php, and I have two forms each on in separated web-page.
when I fill the log in information and go to another form in another web-page, the new form is filled automatically with the same data of the log in form.
And when I go back to log in page after logging out, the credential fields are filled the the old data as well.
I tried value="" for each input and autocomplete="off" and the autocomplete wasn't disabled. and I don't want to cache the data on the forms.
How to prevent one form to use the data from another?
and how to prevent caching the data and autofilling?
have you tried to apply the autocomplete="off" on the form tag and not in the input tag?
<form action="" method="" autocomplete="off">
</form>
Have you tried:
autocomplete="nope"
(or any other random string value for autocomplete, as suggested by Mozilla)?
See: https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
I have developed a search form that searches a large SQL database using PHP and shows the results to the final user.
There is a GET request going on and then the URL looks like this:
http://localhost/search.php?value1=x&value2=y
Now, aside from showing the search results to the user, I want to show some options that they can
apply to the shown results.
For example, I will have an export to excel button on top of the results, as well as a delete button in order to
delete the results from the database. In my server, those 2 actions will be managed by 2 different files, say
deleteEntries.php and excelExport.php.
So, in my html I will have 2 forms, one pointing at deleteEntries.php and one pointing at excelExport.php.
The problem is that each form needs to have its own inputs type=hidden repeated.
The code
<input name="value1" value="x" type="hidden">
<input name="value2" value="y" type="hidden">
, which is dynamically generated through PHP, must be repeated for each form (how else can each server-side file know what the
user needs to export or to delete?). The code is dynamically generated because the search criteria and their values will differ
between searches.
Then, if the user chooses to delete the data, the URL will become
http://localhost/deleteEntries.php?value1=x&value2=y
In my specific occassion I have 3 forms and up to 11 values. So this means that I repeat 11 lines of HTML 3 times and I really don't
like that for many reasons.
One obvious solution would be a combobox where I let the user choose the action (e.g. export data, delete data, option 3)
and to manage all the actions with a single form and with a single server-side file by determining the selected action.
But I don't want to do this because it is less user-friendly and less developer-friendly.
Edit: The reason why I don't make a link for Export to excel like
Export to excel
is because in reality user is given more options that have to be selected from a form.
You can define the form action on a submit button as follows:
<form method="post">
<input name="value1" value="x" type="hidden">
<input name="value2" value="y" type="hidden">
<button type="submit" formaction="deleteEntries.php">Delete Entries</button>
<button type="submit" formaction="excelExport.php">Excel Export</button>
</form>
Each submitbutton will post to a different php script
This does not work in IE9 and earlier but I do not know if that is a problem
You could move the list of hidden inputs to a separate file and include that file in each form template.
Or you could have a list of your parameter names in an array in php. Then generate the hidden inputs from that. That way you can just have a single function call in each form to add in the hidden inputs. You can also loop over the same array in the form handler to read and validate the values received before they are used.
You can define onclick function to the buttons rather than sending the form and on the onclick function, you define 2 or more hidden hidden fields to the form you like which means you dont have declare those input on the page load but define it when the onclick function.
var y = document.createElement('value1');
document.form1.appendChild(y);
<form name = "form1" id = "form1" action = "deleteEntries.php" method = "post">
<button type = "submit" name="submit1" onclick="addinput()">DELETE</button
</form>
This the hidden input need not be included in all 3 forms and these fields can be generated dynamically on the particular form button onclick.
I am building a web service in which a user can login to a backend, submit a form and then other users will be able to see this information on the front-end of the web service.
The data is submitted to the PostgreSQL database fine BUT on the second submission of the form (users can go back to the form and update their information if neccessary) if there is a blank field in the form, the existing data in the form is overwritten with blank data.
How can I avoid this? I think it would be good to populate the form fields with data from the database and allow the user to edit the form like that but I am unsure how to implement this using PHP. At the moment a user would have to fill out the form in full over and over to prevent this kind of data loss.
Could someone give me an idea or point me in the right direction?
pre-fill the form fields with earlier entries from the DB
<input type="text" value="<?php echo $fname;?>" name="first_name" id="first_name" />