0

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).

Alireza A2F
  • 519
  • 4
  • 26
  • Possible duplicate of [Post and get at the same time in php](https://stackoverflow.com/questions/2749406/post-and-get-at-the-same-time-in-php) – Will Aug 08 '19 at 11:59
  • _“that should to be submitted with GET”_ - why, based on _what_? You should chose the appropriate HTTP method depending on what it is you actually want to do here, and not on which data is supposed to stay “hidden”. Is it a request that is only supposed to _read_ existing data from the server? Then use GET. Is the request supposed to _create_ something on the server (new post/comment/whatever)? Then use POST. – misorude Aug 08 '19 at 12:00

3 Answers3

2

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • @AlirezaA2F — The edit makes me think it should be a plain GET request. – Quentin Aug 08 '19 at 12:17
  • Is it safe to show contact's ID in the browser's URL bar? – Alireza A2F Aug 08 '19 at 12:20
  • @AlirezaA2F — If you send it to the browser in any way at all, then the user can see if. If it you send it from the browser in any way at all, then the user can change it. Unless it's something that someone else could usefully memorise (like a password) if they looked over the user's shoulder then there's no problem with displaying it to the user. – Quentin Aug 08 '19 at 12:34
0

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.

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
-1

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

iamousseni
  • 130
  • 1
  • 5