POST and GET methods - php

I want to transfer a data from one webpage to another page. But I don't want to use the <form method="post"> tag because there are no forms in my webpage. Just some sensitive data is there which needs to be transferred to other page.
Please answer the following questions:
What are the ways to transfer data from one page to another?
What are the ways to transfer data from one page to another without using <form> tag in HTML?
How can another PHP (or ASP) page can read the data which was sent to it by another page?

I don't want to use the <form method="post"> tag because there are no forms in my webpage
That is not a good reason to avoid using a form. You can add one.
What are the ways to transfer data from one page to another?
Through the URI (in a query string)
As part of the request body (a POST request)
Via cookies (which you can set with JS)
Via various local storage systems
Use a query string if the data needs to be bookmarkable. Use a POST request (with a form) if it makes changes on the server (e.g. adds or edits a database entry). Use a cookie (preferably set via HTTP after using methods 1 or 2) if the data needs to persist throughout the site. Use local storage for web applications that need to function offline.
What are the ways to transfer data from one page to another without using tag in HTML?
As above, but discount post requests (unless you make them using JavaScript and XMLHttpRequest).
How can another PHP (or ASP) page can read the data which was sent to it by another page?
With local storage, it can't. All the other data is available through the server environment ($_POST, $_GET and $_COOKIE in PHP, for example).

You could use a hidden input i.e. <input name="secret" type="hidden" value="superSecretData" />
You could create a random element which contains the data i.e. <div style="display: none">SuperSecretData</div>
3a. In the case of the former in php it would just be a matter of accessing $_POST['secret']
3b. In the case of the later you would need to use javascript of something of that sort to take the random element and send it along with the page.
Hope this helps

1.) EasyXDM using postMessage if available, hash tags, or flash
2.) same as 1
3.) again you can use EasyXDM
if you open the other page with window.open or similar, or iframes

Related

Using form post versus get for opening sub pages?

Is it good practice to use form post to send data to other pages versus just html link ($_GET) method.
Say if i have page users.php with all users of the site listed then i want to to page user_details.php that lists details about particular user - i can do it two ways.
Details
or
<form action="user_details.php" method="post">
<input type="hidden" name="user_id" value="742" />
<input type="button" name="nothing" value="User Details" />
</form>
or i could have code load every time that will put $_POST data into $_SESSION then on page user_details.php check user_id in $_SESSION is that even better security practice?
EDIT: This site requires authentication before they can see anything.
There only about 10 pages for admin total so i dont think they need to bookmark it.
For web applications, to me, the basic guideline is:
use GET for cases where you are not going to be modifying data (READ only)
use POST for cases where you are modifying data (WRITE/UPDATE only)
use POST for cases where you need to validate some data in order to return a variable result type (i.e. logins, contact forms, etc. where based on the data being sent, the behavior of the page returned can vary)
For typical web applications, there is also a consideration for whether you want a page/resource to be navigable with the data configuration. In other words, do you want someone to be able to bookmark that page and see that same exact data representation (a data read). If so, use GET.
This also falls nicely in line with REST paradigms where the following HTTP actions are typically supported:
GET -> read specified resources
POST -> create a new data element on data resource
PUT -> update an existing data element on resource
DELETE -> delete an existing data element on resource
One major benefit of using $_GET for this is that it allows your users to bookmark the url, essentially link to it instead of forcing the user to complete a form. If you use $_POST this isn't possible.
Using a querystring for this, however, isn't the most user friendly when it comes to URLs. It would be better to use some sort of pretty url structure like Wordpress does or Stackoverflow does for user pages. This can be done by editing your .htaccess file on your server.

auto-populate another form using a link, it is possible?

I'm trying to make an app on Android that send an URI that auto-populate the "RFC Emisor" and "RFC Receptor" of this web page:
https://verificacfdi.facturaelectronica.sat.gob.mx, if I'm correct those two inputs have the id of:
ctl00_MainContent_TxtRfcEmisor
ctl00_MainContent_TxtRfcReceptor
I already tried this but it didn't work:
https://verificacfdi.facturaelectronica.sat.gob.mx/&ctl00_MainContent_TxtRfcEmisor=123456789&ctl00_MainContent_TxtRfcReceptor=123456789
there is a way to achieve what I want?
The short answer is no. The browser won't automatically detect the URL parameter and pre-populate any form fields. A back-end PHP / ASP.NET page can read the value from the request and generate the HTML fields with the specified values. Alternatively, the page could use JavaScript to set the field values when the document finishes loading.
But all of this depends on changes to the target web page. If you do not have the ability to modify that page, I'm afraid there's very little you could do.
You might be able to duplicate the form on your own page, and send the form data to the target—effectively bypassing the form on the other page and 'faking' your own, but if the target system does some kind of validation to prevent posting forms across domain names, this probably won't work either. You may have create the form and process it yourself, replicating the entire form interaction programmatically when a user submits a form to your server. In any case, none of these options are particularly graceful.

saving content from textarea in php

i have a textarea in a php page and and i want to save it on click of save button. but i have the insert queries in another php page. how shall i save the content without page refresh.
my immediate thought was ajax. but is it safe to transfer content through javascript or should i use session variables to carry the whole text content
help me in it.
You can use AJAX. Make sure you use a POST request as the text may be too long to be sent by GET (that is, appended to the URL in a query string).
Sessions are not a valid option. Sessions are files that exist on the server. In order to put the contents of a textarea into a session, you first have to get it to the server, so it's not a solution to the problem of getting the text to the server.
i think the ajax is the best solution,
make in your server side (php) sanity before insert to db (like mysql_real_escape_string)
Session variables are only stored on the server, so they cannot be used to transfer data from the client to the server. I you want to submit data without having the user to reload the page, ajax is the way to go.

How to share a form between 2 websites

I have a form which I'd like to share between 2 different websites. The form is submitted and the data is entered into the database.
Right now each website has its own copy of the script and its own database, so when I want to make updates to the form, I have to make those changes twice. Is there a way to share the form between the 2 websites, yet make it look like its being served by each website like normal. I'm also wondering if it's possible to make all the data go to one database.
The basic options would be...
You could use an html iframe to show the same form on multiple websites.
You could copy the form code between sites
If both websites are on the same server, you may be able to get a php include to include the form (this is possible in some cases even if they are not)
You can certainlly get the database to share information, just ensure the user you use to connect to it is allowed to connect from anywhere - not just localhost and you can connect to the database remotely.
You could include the form inside the other website as an iframe.
There is a short tutorial here on howto do that.
In case the form is displayed inside a whole complex page i recommend placing the form inside its own page and iclude it in both websites using an iframe.
depends what you are looking for, if you use the same process script behind it, do what Mouhannad said, and add a field "return_url" so you come back on the right page.
if it is that you want to send the fields to 2 different locations, create a proxy script and post it by a curl to both locations.
you can simply make both forms (in both websites) point to the same php page (in one of the websites)
<form action="http://www.example.com/action.php" ... >
Then check for the url reference to send the user back to the same page
The user won't feel any difference and you will have the database connection in one place
good luck!
Edit:
I thought your main concerns were the backend code since you mentioned the database. You could use iframes as suggested by the others but I always try to avoid them as much as I can. You can find lots of material online about why you should avoid them.
The other solution is use cURL to get the form's html code from the external page and keep using my above suggestion to change the action url path. This way you have the same code both for the backend and frontend. The downside is that you are making an additional request to get the form's html code which adds to the performance of your website - caching should improve that!
You can use CURL to simulate form submitting on another host, just send POST request with $_POST data.

Getting $_POST variable from table

I'm trying to build a sort of resource allocation form. I'd like to be able to print a table from a database, and then allow users to click on each cell that they would like to reserve. Also, being able to drag and select multiple cells. Then send all of this via $_POST to another php script.
Problem is, I have no idea where to start.
Any suggestions?
The first and most critical thing you're going to need from what you described is a bunch of hidden fields to store the information you're interested in. You would have to write javascript code on the client side to store the users interaction with your page into these hidden fields.
To receive data via POST, you will need <input type="hidden" name"some_field"> for every bit of data you wish to "know" about that was changed on your page. Table information is not transmitted in a POST operation if it's just text, so you can't see the layout of the modified table on post back to the server.
If you don't have to POST this data to another form, it is probably a better idea to make callbacks via XMLHTTPREQUEST as the user interacts with your page, but I don't know the requirements of what you're trying to do.
I wrote one for my school recently; the trick is to either use buttons/links or addEventListener the cells to JavaScript. If you want the source code to my app, download this zip file:
http://azabani.com/files/busbook.zip
Edit:
My system works in the following way:
addEventListener to cell clicks, calling book()
book() then sets location to book.php
book.php does the database work
book.php sets the location header to immediately go back to the viewer
The system knows which week view to go back to based on session variables.

Categories