How to get the content of another page without loading in browser - php

Context:
There is an asp page in another web site that contains a search form. After submitting this form with the search value, the page displays the results of the search in an iframe.
Question:
I want to use the returned result in my page without loading that page in browser. I have got permission from the other web site administrator. I know php and jQuery. How should I proceed?
Example:
abcd.asp contains a form which contains <input name="regno"/> and a submit button. The form submits to itself and results Name, date of birth and Marks is returned in table format in an iframe.
Notes:
I have a page with same <input> and upon submitting it will use the above asp page and get details and display it in my page.

Easiest way:
Use jQuery to submit your page via AJAX to a local PHP file.
When the local PHP file gets the request, do a curl post request to your remote page and fetch the results.
Return these results in your local PHP script so that these will be available to your AJAX request
Your ajax request now has the remote contents, so just use jQuery to push them into the document.

You should use AJAX to perform this task. It loads a particular section of data without reloading a page. More info about what is ajax is here

Your best option is to use jQuery's ajax method. To make this work with different domains you need to enable crossdomain support. This is very easy in jQuery:
$.support.cors = true; // enable cross domain support
Now you can make a request to another domain. Of course the server on that domain must also accept cross-domain requests. ;)
Option 1 - HTTP GET & JSONP
As Twisted1919 pointed out you have to use jsonp if you want to make a HTTP GET request. If you're not familiar with jsonp It requires a bit of reading to understand the concept. But there is excellent support for this in jQuery (as described in jQuery.ajax()).
Option 2 - HTTP POST
If you're using http post to make your request you can simply use jQuery.post() for this. With cros domain enabled an the server accepting your request this should work out of the box.

Related

Reprocess results got from server

I am making a webpage which, largely, aims to submit a form to another webpage, process the results further, and display them on browser.
[1] Input form is ready. The form data is to be submitted to http://toolkit.tuebingen.mpg.de/hhpred/
[2] After the form is submitted, my webpage is redirected to the "Waiting" page of that website.
How do I stop this redirect? I cannot use AJAX, because it cannot work between different domains. (Can I ?)
Further, I need to get the response data and process it.
A possible solution can be: a PHP script, which should work as:
form-->submitted to my server-->submitted from my server to http: //toolkit ... -->response received by server --> processed further -->diplayed on browser
Currently I have created the webpage with flask. I have also used flask for "process further part".
My Question is: How can this process be acheived?
If I use PHP, how to integrate it with Flask?
Maybe you can use this: http://api.jquery.com/jQuery.ajax/ which supports crossdomain requests...at least in a way

PHP Ajax return source

I've got the following problem, I send some value from jQuery to PHP via AJAX. My PHP script receives those value, and I would like that this script prints values in iframe in my www site. But instead, the script response contains all its source code to AJAX as response to the alert message. Does anybody know how can I stop returning code to AJAX and execute it by returning the values to be used in my iframe?
EDIT:
further details: the problem is that my application has got two iframes from different servers, and I need to send values from one iframe to another. Because of cross domain restrictions, I cant do this via JS directly, so I figured out to send values via AJAX from one iframe to PHP on first server and PHP from first server to PHP on another server which is in iframe on my main page, and then show the data. so I must use AJAX to send it.
You can't. PHP doesn't know about the iframe.
You are, presumably, using XHR to make the request, therefore XHR will receive the response.
If you want to load a document into an iframe, then load a document into the iframe (e.g. by setting its src to the URI for the document), don't play around with Ajax.

Is there a way to execute external Javascript from PHP when Javascript is disabled client side?

I am using PHP to generate a dynamic web form that uses an external (to my site) javascript file to set a hidden form input value. My form submits to my own PHP page before reposting to an external site. The external site requires this hidden value for any submission.
Many of the browser that are using my form do not have javascript enabled, so the tracking fails and I can not submit to the external site. Is there any method of executing the javascript file to get the tracking information from my php code without sending it to the clients browser?
This is a pseudo code mock up of the code that I want to handle the form's post:
if ($doPOST) {
//Check POSTed parameters for tracking id ($tid)
if ($tid == ''){
//Execute external javascript to get tid
}
//Post form data to external site
}
I have looked around and could not find any relevant information, but if you know of a site where this is explained let me know. For clarification, I am not looking for information about to incude a javascript file client side.
Edit:
This is the Javascript executed by the form. I have no power to change this script. The number 48891 is not hardcoded in the script it changes with execution:
onReady=(function(ie){var d=document;return ie?function(c){var n=d.firstChild,f=function(){try{c(n.doScroll('left'))}catch(e){setTimeout(f,10)}};f()}:/webkit|safari|khtml/i.test(navigator.userAgent)?function(c){var f=function(){/loaded|complete/.test(d.readyState)?c():setTimeout(f,10)};f()}:function(c){d.addEventListener("DOMContentLoaded",c,false)}})(/*#cc_on 1#*/);
var test_track=function(data){onReady(function(){document.getElementById('test_track').value = data.TID})}
test_track({"TID" : "488891"});
Yes, you can execute JavaScript server-side. However, it wouldn't make much sense just for a snippet, and you wouldn't be able to access the DOM. See the v8cgi project for a server side JavaScript implementation.
Just rewrite it in PHP.
No. If JavaScript is disabled on the clients browser you cannot get info out of the browser. I suggest you use PHP for tracking
If you are under time pressure (I am not saying this is a good idea), you can parse out the TID value and write it in the hidden form value 'test_track'.
You can get the content of the javascript with:
$js = file_get_contents('http://example.com/source.js');

jQuery mobile get data from remote server with PHP

My problem is:
I have mobile application using jquery+jquery mobile+phonegap. In this app I have several pages with forms. I want to submit this forms to remote server where is php file for handling variables and it return simple text as output. My big problem is that I don't know how get this data in my application without redirecting to Android browser. I search over internet and I can't find how figure it out
This sounds like you should be making an ajax request from jquery to the php file that handles the request and will return the appropriate response. Take a look at the jquery documentation to see how to make an ajax request, where the url field is the url of the php file and the data field is the variables you want to pass to the php file.
http://api.jquery.com/jQuery.ajax/

Can a site block an ajax call referring to another, third party site?

So I have a bookmarklett that seems to not work on some sites.
Here's what happens. The bookmarklett (exists on your bookmark bar) fires a javascript sequence that will construct a div with content on any page you are currently on.
Once the div is constructed, it will send an ajax GET request to a php page on my server (that is, the page the bookmarlett is loaded on is not within my server, this is somewhere else, ajax requests data from my server to be sent to the website the bookmarklett is loaded into).
Apparently, I have not seen this error myself, I was told that the ajax call failed to be received, so no dynamic data was passed into my constructed div.
The order of events is as followed:
Bookmark Clicked -> Creates a modal popup div on the page being viewed -> runs an ajax call to my server, passing a unique ID of the user to retrieve some data -> then populates the dynamic content with this retrieved data.
Apparently on some site the ajax request was never received back.
Can some sites block my bookmarklett from gathering data via ajax from my site?
Is it possible that the site messed up the request being sent to my server for a response?
If so, is there any work around? Or do i need to ditch it all together.
This is how my ajax request works, and I have not seen it fail, but apparently it has.. It may have been an error on the users part (may have touched some of the code supplying the data to send with the GET request, I'm not sure though)
jquery.ajax({
type:"GET",
url:"mysite.com/api/getStuff",
data:"format=jsonp&userid=1234",
success:function(data){
// do stuff with the retrieved data
},
error:function(err, msg){
// response was not received
}
});
XMLHTTPRequest is subject to the same origin policy which is imposed by most browsers. For security reasons, it restricts XHR to the same protocol, domain, and port as the original page request.
Visiting the link I provided will also start you on your way to finding workarounds for this if it's something you want to do.
Crossdomain ajax requests are restricted. Use 'jsonp' or 'script' dataType.
Note that "Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation". More info is available on official jquery documentation
I know from experience that some mod_rewrite rulz messed up my ajax requests, i think you might have some problems with using ajax + mod_rewrite.

Categories