I need to display information from an SQL database which resides on a server, to a remote webworks mobile device. I am extremely new to passing information from a server so bear with me. My normal understanding is that I would have an HTML file that accesses a php script which then itself connects to the database and displays the information.
However, in webworks the HTML/Javascript files reside on the device and are separated from any php file so I need a method to communicate to get the data from the database. I have looked through JSON and read all the tutorials on w3schools and I understand the syntax but I don't understand how to use it. How could it connect to a database? My aim is to simply display the table entries on a mobile device app running HTML5 webworks. Again I am very new to this so any explanation would be very helpful.
Chances are, you should get a book. This is not something that can be explained in detail in a short answer on this site.
in summary however, you can either
1) send requests to your php script by submitting a form on an html page, which will load a new page filled with whatever PHP sends back. in this case you do not need to use JSON at all as PHP would be returning a full html page.
2) you can use AJAX. AJAX is a javascript method of sending requests to the server (PHP), and getting a response without ever loading a new page. you would use AJAX to send a request to the php page, the php page would access the database and send back a response, the javascript would then take the response and do whatever it needs with it. the response data is usually formatted in a JSON format, because PHP can easily create JSON, and javascript can easily decode JSON once it receives it as a response. to make using AJAX simpler, you may want to look in to using jQuery, a javascript library which can simplify the process.
Related
I am building an app that extracts data from a website and displays them in my app. I am using PHPQuery to extract data in my server-side code.
However, one page contains an .asp form with two dropdown menus. I need to select an option in both of them and then parse the resulting html. I need to do this server-side, so javascript doesn't seem to be the option.
How can I do so? Can it be done using PHPQuery or some other technology is required?
The page in question is: http://www.bput.ac.in/exam_schedule_ALL.asp
Since you're using PHP and phpQuery, I suggest you also try cURL.
Explore what the form submits via JavaScript and replicate that via cURL. Do this to get the format of the posted (assumption) data, which you can then replicate in a cURL request to the same endpoints. JavaScript won't be necessary, and you can get the same results you need. In this case, you won't need the item mentioned next.
Alternatively, if you have a browser, such as webkit, phantomJS, etc, you can write an automation script to run those steps and return the results, depending on exactly what you need returned. See more complete examples here: https://stackoverflow.com/a/17449757/573688 for how others suggest you do this. NOTE this is not usually necessary if you just need to emulate POST requests.
This is a non-coded answer because it's not entirely clear what direction helps you the most.
On page works JavaScript, which should make an AJAX request to the server and pass the selected value from both SELECT.
The server receives the AJAX request, performs the request to the correct address (you can use phpQuery) and prints the response (gets to the response on AJAX request).
JavaScript on the page receives a response to an AJAX request and performs actions affected.
I'm trying to parse data from http://skytech.si/
I looked around a bit and I find out that the site uses http://skytech.si/skytechsys/data.php?c=tabela to show data. When I open this file in my browser I get nothing. Is the file protected and can run only from server side or something?
Is there any way to get data from it? If I cold get HTML data (perhaps in a table?) I would probably know how to parse it.
If not, would it be still possible to parse website and how?
I had a look at the requests made;
http://skytech.si/skytechsys/?c=graf&l=bf0b3c12e9b2c2d65bd5ae8925886b57
http://skytech.si/skytechsys/?c=tabela
Forbidden
You don't have permission to access /skytechsys/ on this server.
This website doesn't allow 'outside' GET requests. You could try parsing the data via file-put-contents but I don't think you will be able to get specific data tables (aside from those on that home) due to AJAX requests that need to be made. I believe the /data? is the controller to handle data which is not exposed via the API.
When you open this URL in your browser you send GET request. Data returned under this address is accessible after sending POST request with params as follows c:tabela, l:undefined, x:undefined. Analyze headers next time and look on Network log if you are using Chrome/Chromium.
If that website does not expose an API, it is not recommended to parse the data, as their HTML structure is prone to change.
See:
http://php.net/manual/en/function.file-put-contents.php
And then you can interpret it with an HTML-parsing engine or with an regular expression (not recommended).
I need to run a search using Ajax, such that the response I get from Ajax (should not contain HTML, it should only contain data) is fetched on webpage and then parse that response with HTML on the page and display.
I want to know can it be done, if yes then how to do it. Also is it going to make process run faster or consume less resources on server?
Since you have made no attempt to try to code this, I will give you a couple pointers.
1.) It is very possible, I do it on login forms.
2.) Post data to a external page, then encode the response on that page to an array in JSON. echo out the JSON on the external page.
3.) After your ajax post is finished, you can carry out a function similar to this:
function(data){alert(data.given_name_on_external_page)}
or something similar. Once you google around for Ajax form examples you should be able to grasp a little better.
4.) Now for displaying this on a web page, it is fairly easy.
HTML
<div id='response'></div>
Javascript
function(data){document.GetElementById('response').html=data.data};
That should be enough for you to understand what needs to be done, I will leave the rest to you and your ability to use google :).
use escape and load() functionality in JQUERY
I have developed a free shopping cart plug-in for small websites, I am currently using name=value&name=value to submit items to the basket.
The process is html form -> jQuery serialize -> AJAX post -> php,
I have read that JSON is a more secure way to pass this data. I was hoping some kind person could explain to me why this is or point me to any web resources on the subject.
JSON posted with XHR is no more (or less) secure than any other request.
They all must be handled appropriately.
There's no difference between AJAX POST or any other POST. It's all the same.
HTTP is a simple protocol, whether JavaScript sends headers or a custom built script - it doesn't matter to the underlying server since all it sees is plain text that it interprets.
There's no "more" security if you use AJAX or regular POST-ing, anyone can send any sort of data to the target script so you need to handle it properly.
It's not related to security at all.
JSON is just a different way to transmit the data - instead of posting a querystring-like string you send a string that is valid JSON.
By default in AJAX, data is posted in xml format using some protocol. while parsing data from xml format, we will get all node values in string format. Hence data type of the value submitted/received is not known. If required we need to typecast the data.
where as in JSON format data types has been persisted till some extent.
POSTing JSON data requires that certain types of headers be sent back and forth between the client and server. For instance, the client needs to send content type, the server needs to respond with allow-options for content type and access origin. POSTed JSON doesn't come through the PHP $_POST variable, rather it is carried in $HTTP_RAW_POST_DATA.
Without the proper headers, a browser kills the response and its data, preventing the page from looking at or processing anything. Or at least, it's supposed to.
Is generally more "secure" for preventing cross site scripting problems, but the data and calls are still subject to hacking the posted data and headers and such.
What i basically want to do is to get content from a website and load it into a div of another website. This should be no problem so far.
The problem is, that the content that should be fetched is located on a different server and i have no source access to it.
I'd prefer a solution using JavaScript of jQuery.
Can i use a .htacces redirect to fetch the content from a remote server with client-side (js) techniques?
I will also go with other solutions though.
Thanks a lot in advance!
You can't execute an AJAX call against a different domain, due to the same-origin policy. You can add a <script> tag to the DOM which points at a Javascript file on another domain. If this JS file contains some JSON data that you can use, you're all set.
The only problem is you need to get at the JSON data somehow, which is where JSON-P callbacks come into the picture. If the foreign resource supports JSON-P, it will give you something that looks like
your_callback( { // JSON data } );
You then specify your code in the callback.
See JSONP for more.
If JSONP isn't an option, then the best bet is to probably fetch the data server-side, say with a cron job every few minutes, and store it locally on your own site.
You can use a server-side XMLHTTP request to grab your content from the other server. You can then parse it on you server (A.K.A screen-scraping) and serve-up the portion you want along with your web page.
If the content from the other website is just an HTML doc that you want to display on your site, you could also use an iframe to pull it in. You won't have access to any of its content because of browser security rules.
You will likely have to "scrape" the data you need and store it on your server.
This is a great tutorial on how to cache data from an external site. It is actually written to fetch and store XML, so it'll need some modification. Also, if your site doesn't allow file_get_contents then you may have to modify it to use cUrl.