$_POST and redirect with html submit button - php

I have a html form that has a series of radio groups along with php code that insert the radio group into a table. The insert works if I do not have a link to the next page, but if the link is in the following :
<form name ="input" action="demopg2.php" method="post"> does not insert but links to the next page.
<form name ="input" action="" method ="post"> inserts the data but does not link to the next page
the submit button code is:
<input type="submit" class ="button" name="submit" value="Submit" />

It sounds like you have:
A script which can both display a form and insert submitted data
Another script which shows the next page
You need to change the logic so that either:
The "insert submitted data" logic is moved from the first script to the second script or
The first script, if it has inserted data, redirects (with a Location header) to the second script instead of displaying the form
The second approach is generally the better one as it is more easily extended to give you a third possible output: Displaying the form with error messages if bad data is submitted.

Related

Nested href in a form

I have a script that I downloaded and want to add an href="#" to it but it's interfering with the form on the page. The form is triggered by a button, not sure of that matters. I tested my href outside the form but it still refreshes the page once before it works. Here is the code.
<form method="POST" action="/profile/save_profile_setting">
Update My Location
</form>
<button class="btn" type="submit" name="action" ><span><?php echo __( 'Save' );?></span></button>
My href just passes the id to run ajax to find a location, it's always worked but now the two aren't playing well together. I have the same href on a different page by itself and it works fine.
Thanks for any help.
I didn't understand what is your actual query. I understood roughly of your query. My understanding for your question is that: with tag, you want to execute ajax call and with button, you want to perform form action. Based on this assumption:
(1) If you want to avoid page refresh with tag, you need to do as follows:
Update My Location
Now, you can easily perform ajax with tag.
(2) To perform button action with form, you need to add button inside form instead of outside the form.

How to prevent multiple forms having the same input type=hidden values?

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.

Prevent multiple submissions

I have a page that sends data to itself, i.e.using PHP_SELF.
Upon page refresh, i.e. when the page reloads, I place two buttons - Refresh Again, where I
send data using again using PHP_SELF. - An alert box pops up asking for confirmation, that usually occurs when date is sent again to a page.
The purpose of the second button - Reload without sending data, must reload the page.
Without the above pop up box appearing, i.e. without data being sent.
Is there a way to flush out data before it is sent again to the
server or to a page?
I send data via $_GET.
I check whether I receive any data as follows:
if(isset($_GET['getVariable']))
/* I place the buttons here, in a form, the action parameter of which points to the same page, i.e. `PHP_SELF`
You could make the refresh without submitting a new form:
<form name="refresh" action="" method="post">
<input type="submit" value="Refresh without submitting" />
</form>
Or use JavaScript to refresh on the current button.
<input type="button" value="Refresh without submitting" onclick="location.reload();" />
Also, use action="" instead of PHP_SELF.

Button INPUT that saves data and links at the same time

I'm doing a questionnaire (form) and I need to put a submit button that does two things:
Be a button type INPUT (because I need to use this kind of button on my PHP code, I've if(#$_POST['Next']) for save the dates of the form in my DB).
That this button will have a link for go to the next screen of the questionnaire. I tried with
<a href="demo2.html" target="_blank">
<input class="buttonNext" name="submit" type="submit" value="NEXT &#8592">
</a>
This code doesn't work, but with IE browser, on the page appears a circle next to my button that is the link. So the button doesn't work; it only saves the data, but doesn't link to the next page.
How can I solve it?
The form's action determines the URL where the form is sent, and this is just good for you (it is sent to the PHP that processes this step). It can save the data and/or return the form again to show validation errors. Once you decide to go to the next step, you can redirect the user to that url.
use if(isset($_POST['Next'])){ instead of #

Upload a dynamic page to server

I have a page that contains a table that the user can update. This has been done using javascript.
When the user has finised updating, I want to have the raw HTML page uploaded to the server, and then the user will be redirected to a php page which will do some final processing and spit out a report.
Is this possible or should I come up with another method of reaching the same end... and if so how do I get the dynamically created HTML onto the server to be manipulated?
You could have a form with a hidden input, and load the html into it using jquery.
So, your html may look like this:
<table id="table1">
... (this is the table from which you want to get the html)
</table>
<form id="form1">
<input type="hidden" id="table_html" name="table_html" />
<input type="submit" value="Submit" />
</form>
<script>
$("#form1").submit(function() {
$("#table_html").val($("#table1").html());
});
</script>
I didn't test this or anything, but the idea is that when you submit the form, the script populates the hidden value with the table html.
Now, if you have complex stuff in the table (I assume you do since the user can edit stuff right there), then maybe sending all the html isn't the best thing, you'll get a lot of garbage with the "useful" stuff. The way I'd do it is have the table inside the form, and have everything in inputs. Probably the names of the inputs will be like "first_name[]", so php will get arrays of values when there are several rows with the same cells and stuff like that.

Categories