Pass data to other website search - php

Is there a way to search on www.qantas.com.au or other sites that don't use GET method, from my own form?
I mean something like this : http://site.com/search.php?data=myData
I don't want to return result on my webpage, I just want to send data with a href

Depending on the website, many forms may contain CSRF tokens intended to prevent this behavior (imagine if a third party site could 'submit' a form for you to another site, perhaps to trans fer money or write embarassing posts)
That said, for sites that don't implement this feature, it should be possible just to copy their form (from <form> to </form>) including the action="/their/website/controller" and method="POST" (if you want it posted)
If you want to design your own form that submits the same data, just make sure the input fields have a name="blah" the same as the form data value that you want to submit
AJAX is also an option if you dont want to use a form. (see http://api.jquery.com/jQuery.post/)

Related

Ajax - multi-lingual pages with forms

I am trying to understand if the following scenario is possible:
a .PHP page that has a form and inputs for the user to submit information
The page itself offers several different languages for the user to view it in
The user can change languages but the form and the inputs retain their values (if the user has added information) - so the page cannot be re-loaded (as it would lose the information)
I think this might be achieved using an Ajax solution but I'd appreciate any thoughts or advice.
You actually want to change the page, so reload might still be the best choice. Otherwise you need to change every string on the page with javascript, which can get tedious.
You can do the reload without loosing the data. Just add the language information to the form and send another parameter to avoid the actual action that would normally happen when you send the form on the php side. Render the page in the new language and insert the transmitted data in the form.

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.

Save/restore form data after locale change

I have a database app written in PHP (jQuery/JS on the front end) that has bilingual labels/text. Currently one can only change one's language on a maintenance page (form submission, then PHP updates a session variable with their new language choice), but the users would like me to add a language pulldown that would appear in the corner of all pages. When the page contains a form, I don't want users to lose their partially entered data if they happen to change the language, so I need to save/restore the form data somehow. Is there an easy way to do that? I know I can use jQuery to serialize the form, but then what? Send that added onto the URL and pick it up in PHP? Then what? Write some routine to loop through the form fields and handle them properly (inputs, selects, radio boxes, etc. are all different)? It seems like there should be an easier way. I don't mind restricting myself to HTML5-supported solutions or adding jQuery plugins.
How about localStorage?
If user has filled any input fields, save them to localStorage and delete the data after user submits the form.
My suggestion is to:
Submit the Language and any wanted user data when changing language to the server using $.ajax or $.post

PHP Inserting row into MySQL - Which method is more appropriate?

There are two most notable ways of inserting a row into a MySQL database using PHP:
Create a single PHP file which uses a loop to detect whether isset($_POST['submit_button'] has been submitted, and if the form has not been submitted then display the HTML form. If the form has been submitted, during the loop insert the data into the MySQL table.
Create the HTML form on page1.html and when the form is submitted parse the data and insert into the MySQL table on page2.php.
Both methods work perfectly fine - however, based on your own opinion, is one better than the other (such as security, maintainability etc...)?
Given the choice, I would choose neither. Instead I would opt for the Post-Redirect-Get (PRG) pattern, by which the form posts to a secondary page which only processes the input, but produces no output itself. Upon successful or unsuccessful completion of processing, the script redirects to a final page, which may be the original form page to display messages, errors, or request resubmission.
Typically, session variables would be used to pass information back to the final redirection point, whether that means values from $_POST to repopulate a form, or success/error codes.
This goes a long way toward solving issues with accidental form resubmission via the browser back button.
Your second method of posting to a different page is like an incomplete form of PRG.
I can say that from a usability standpoint, I prefer the first method, because it allows you to create sticky forms, i.e.
<input id="foo" name="foo" value="<?php echo $_POST['foo']; ?>" />
For cases where it is practical, I'd try to get away from having a second user pageload at all. page1.html submits via AJAX to a web service provided via page2.php. And yes, there are many cases where this isn't appropriate, but the most common patterns I can think of where the form handler is simply inserting rows into a database are well suited to an AJAX submit.

Pagination and $_POST

I want to do a search with pagination, but I don't know how to 'store' the data in the $_POST array, should I do it with sessions?
Rolensen
If you are doing a search, you are trying to GET data from the server, and not send data to it -- which means you probably should use GET, and not POST.
Also, it would allow your users to bookmark the result pages (or send those links by e-mail, IM, ...), which is always nice ; and, also, use the back/forward buttons of the browser without getting an alert box, which is nice too ^^
(Oh, and, BTW, it would help solve your problem ;-) )
Yes, you can use sessions or hidden fields and even better GET method in your form.
It is possible to use both GET and POST in form, just add appropriate attribute method to form tag:
<form action="index.php?page=5" method="POST">
So the pager links are submit buttons while rest of the data is stored in hidden fields. But that's not a good way to do this because you cannot pass someone link (on IM for example) to your search results.
But the best way is to store somewhere POST input data (look here: http://www.symfony-project.org/plugins/, when you input your query once, it is stored and remembered so you dont need to fill form multiple times)

Categories