Alireza A2F
Alireza A2F

Reputation: 519

Submit some data using GET, some using POST to the same page

Is there a way to submit some of the data using GET method and the rest using POST method to the same page?

There are some sensitive info in the page that needs to be submitted with POST, also there are some info like page_number and search_phrase that should to be submitted with GET.

I've tried creating two forms one with GET method, the other with POST method and used JavaScript to submit them at the same time but only the GET form is submitted.

The data which should be sent by GET is not constant so that it can not be appended to the end of form action like below:

<form action="form.php?page=3&search=sth">

EDIT

To describe more, there is a list which normally shows all the notes and it has paging and the data is searchable (GET), but there is a situation which a contact's ID is sent to this list, then only the notes that belong to this contact should be displayed with all the functions mentioned (paging and searching).

Upvotes: 0

Views: 941

Answers (3)

iamousseni
iamousseni

Reputation: 140

You can do it in this way (example): GET variable:

let page_number = 4;
let search_phrase = "Hello";
let form = document.getElementById('form');
let formData = new FormData(form);
let xhttp = new XMLHttpRequest();
xhttp.open('POST', 'https://www.destinationpage.com?page_number='+page_number+'&search_phrase='+ search_phrase);
xhttp.send(formData);

I suppose that is clear, but if you don't understand i can explain you

Upvotes: -1

Quentin
Quentin

Reputation: 943649

An HTTP request can get GET or POST (or various other things) but not both at the same time.

A normal form submission navigates the browser to a new page. You can't navigate to two new pages in the same window simultaneously.

You could use Ajax to make two HTTP requests without leaving the page.


However, your premise is flawed…

The choice between GET and POST has nothing to do with sensitivity. GET is used to retrieve data. POST is used to set data. (That's simplifying a little).

If you're setting data, then use POST.


Aside: You might want to look at the PRG pattern which redirects from a POST request to a bookmarkable, refreshable URL that the browser GETs.

Upvotes: 2

delboy1978uk
delboy1978uk

Reputation: 12375

Absolutely.

I frequently have the ID of the record in question as a GET var, and the data i am changing a POST var in the form.

Pagination is also a great example of a good use case for GET.

If you aren't using a router component that creates pretty url's, just add a query string and echo the ID variable in your form action.

http://example.com/some/page?id=<?= $id; ?>

And the rest can be posted from your form.

Upvotes: 0

Related Questions