display form and query result on the same page - php

I know that this theme has been arround for a long time. But, what I want is to display a form and display the result of the request below, on the same page without erasing (reseting) the form.
So, always display the form, empty at first, and then always diplaying what the user has input. And then display the result of the request below the form.
I have tried to manipulate the if(isset($_POST['submit'])) stament but it either show the result on the top and not below and it reset the form value.

can you show us the Code, 2ndly its also possible via ajax

It's calling ajax call.
First of all you need three page for this (actually you can do that with one page but it seems not good.)
Show page: index.php Which have form and result div.
Ajax call script: main.js which is included in index.php with tag.
Service: service.php which is return to you data.

Related

Display form results on same page with action to a different page

I Have a form with the following in index.php:
action="calculate.php"
In calculate.php I do some math with data retrieved from index.php.
How can I display the results in some inputs in index.php ?
Ideally, you can use session set the value of the result in calculate.php then redirect to index.php from here, This is where you read the data, you could run some kind of check to see if the result is set in the session and then display it. Don't forget to unset the session result after displaying it.
This would work but It's not the best way to go forward. I would recommend that you do everything in a single file and use a post request to process and display the result, You can also move the calculation logic to functions which can be in a different file and invoke them as needed.
Why not use ajax? Use javascript/jquery to catch the submit event, use ajax to send the data to calculations.php, echo the results back.

Search in a modal, update DIV on original page

It's another late night and another seemingly simple issue that's causing a headache!
So, here's the situation. I have a simple HTML form that's in a Bootstrap modal. When this form is submitted, there's an AJAX POST to a receiving page, SESSIONS are set and the request is then forwarded to a simple DB query. This all works.
What I want to do is show the sessions on the original page without a page refresh.
I thought this would be easy so I tried using this on the original page;
$('#filteroptions').on('hidden.bs.modal', function () {
$("#breadcrumbs").load('includes/files/private/breadcrumb.php');
});
breadcrumb.php holds the output format and the file is populated immediately after the POST from the modal (called filteroptions)
I also tried to attach it to the POST success with a simple success process to load the file but each time, the breadcrumb.php fils to be loaded.
Curiously, if I ctrl+F5 the page after the first POST, there is no value shown BUT if I search again the DIV is updated each time I search after that.
Why would the request not fire the first time that the search is performed? Why do I need to refresh the page for everything to start working?
There is no caching to it's not a case of a dependantr file being cached after the refresh.
Thanks
The solution was to populate the div with nothing and then update it.
Previously, the div was only being drawn when it was populated thanks to the code in the breadcrumb file looking for a specific POST or SESSION variable.
It now allows for a blank value.

prevent dupplicate submitted by ajax

I'm using ajax to create list of replies.
In the end of the list I added textarea form that allow user to add reply (also with ajax).
The problem is that i call to my JS in my main PHP page so when user want to submit reply the page doesn't "know" the js code. If i add the js to the ajax php file/page the code will work but then it will be duplicated many times and when user will submit form the text will be submitted many times...
In my console i see that the JS file duplicate every time i load the replies list
How can i prevent it?
Disable the submit button right after user presses it once. Using jQuery:
$("#button").attr('disabled','disabled');
Make sure to remove disabled attribute on AJAX error so user can re-submit the form with new data. You can remove disabled attribute like this:
$('#button').removeAttr('disabled');
[improved]
if you want to not repeat data when navigation or F5 press, simply free the $_POST vars after doing whatever you want, and check if isnt set (before clean, of course) redirect you wherever you want. example:
/* HERE do the job with ajax response. */
if(!isset($_post['foo'])) header('Location: wherever.php');
$_POST['foo']=NULL;
If you're using $_GET... don't do it, use $_POST... $_POST is your friend.
Finaly ensure that if you press F5, you don't re-send form vars by post (otherwise you will get the same). If it's happening, clear your inputs with jQuery on page load.
$('#inputID').val('');

How can i submit data to a php page without going to that page right away?

I need to take form data from user input- i.e. radio buttons and use that to apply search filters to a database search.
However there are specific parameters which are giving me difficulty.
Specifically: The search filter options pane is a static fixture on the main page of the site. The query to be modified by the search filters is a separate php page which is called by an ajax function to display search results in the middle of the page without page refresh.
Is it even possible to submit variable values to another php page without going to that page and processing the php immediately? Or will the variables not be stored like that?
The code is too long but I'll give basic pseudocode:
Form action="Query Page to receive user input.php"
Some radio buttons:
20
15
10
Submit button--> Submits the radio button value to QueryPage.php but does not redirect
User clicks a category link (i.e. fitness) that calls the ajax function which displays the output of QueryPage.php. At this point QueryPage.php should perform the search with the specific user input filters that were selected earlier.
Is this possible?
Let me see if I understand correctly:
You're basically saying that your radio buttons will modify the search results, based on the what user selected?
If that's the case, I can think of 2 options:
1- When you make the ajax request for the search, first, grab the user input and send it to the QueryPage.php file with the search query. Do you have access to that function?
2- Post the user input using ajax (are you using jQuery or some other library for this?) to a UserInput.php file, where you'll store that data on the session, and then from QueryPage.php you just access the session and grab the values sent previously.
Does that answer your question? Sorry if it doens't, it's a bit hard to understand the problem.
You can use JQUERY and its events method. For example change,click,hover. In your case you want to use radio buttons, so you might wanna use the click event for that.
$("#radio").click(function () {
// SEND HTTP REQUEST
});
http://www.mkyong.com/jquery/how-to-select-a-radio-button-with-jquery/
Yes. You could just use .ajax(), or .post() or cURL. This will post your data to the specified page without redirecting.
Example using .post()
$.post("test.php", { name: "John", time: "2pm", fieldname: "your value" } );

PHP refresh, how can I stop the previous function being called again on page refresh?

I have a function that imports a files contents into mySQL and returns the results. If i refresh the page and click "yes" it will do it again doubling the output with the same content.
How can I stop this happening? In this particular case there is no URI in the address bar but on other functions there is.
You should just check if the contents in the database exist, if they don't, fill them. Otherwise don't run the function.
psuedo-code:
if !database.containsRecords
fillDatabase()
end
On top of this, it is always good practice to redirect after a POST request. So you would want:
fillDatabase();
header("Location page.php");
exit();
Query the database on each page load and see if it has already been populated. If it has been populated then don't attempt to populate it again.
You should use the POST-redirect-GET pattern.
After updating the database, send an HTTP redirect to a separate page that displays the results.
Refreshing the browser will reload that separate page.
I assume you use a POST form to upload the file.
You can include a hidden input field with an pseudo random unique id in your form.
If the user resends the data via POST you can check if you already processed this request.
Hidden fields are not save because a user might edit them, but you can detect accidental resubmits.

Categories