Obtaining value obtained by javascript using PHP - javascript

I was able to extract user information from the social Graph API and display it on my site within tags . How do I now get this information, and use it in a PHP script? In other words, say I want to use PHP to calculate the weather, but only based on the preferred weather on a users facebook profile(hypothetical situation). Now, assuming I have obtained the users preferred weather using javascript, how do I use PHP to get that information in order to perform my calculations? I guess one way would be to use regular expressions to match the values on the same page, but that seems tedious. Any help/direction is appreciated.

Are you using php to access the Facebook API or the pre built HTML they provide? You should use php to retrieve the data, do what you need to do with the data, and then display it on a webpage, rather than the other way around.

You can use javascript to get the data, then send it to your php script using AJAX, but this is a round-about way of doing it.
You can make the call the to the Graph API directly from your PHP script. Something like this:
$r = new HttpRequest('https://socialgraph.googleapis.com/<method_name>', HttpRequest::METH_GET);
You will need to parse the data when you get the response -- json_decode() should do the trick.

Related

How to get the values of session using js

I want to know if the user has logged in or not and then display different elements of the navigation based on the username (login by email and password Not username). However, I have no idea how to deal with session. If php and html is separated in two documents, how can I store the required values using session in php document and then get them using javascript in html document? Or should I use cookies instead?
There are a several approaches to do this.
1) You can make a PHP file which will format your $_SESSION data, and all the other data you want as a JSON string (json_encode function in PHP lang). Then use echo to return it. Then use an AJAX request to get this file from javascript or JQuery, and you will receive the data you want. That's a bad approach for this purpose, from my point of view, because after loading the page you send another request to receive a static data, which will not change on the page like email or username.
2) Better approach: PHP is preprocessor hypertext. You can make an php file and write html tags in it, with php output(example: <div><?=$_SESSION['email']?></div>). Search for more info in google ("php inside html").
3) Much better. In modern web programming world its a mistake to use php inside html because you should think not only about how to make something work, you should think how you will maintain it after 3, 6, 12 months later, too. If you use just php inside html in a big project, then, with time, you realize that your code is hard to read and looks ugly. There are plugins that can make your view more readable and maintainable (Twig, Blade, Volt and others). I recommend you use one of them.
The session is a server side thing, you cannot access it using javascript. You can write an Http handler (that will share the sessionid if any) and return the value from there using AJAX

jsoup to retrieve data from web page in key value pairs

please someone help me out , using jsoup how could be traverse data , the data i want to fetch is totally in JavaScript of web page , and written into key-value pairs , so there's no any html tags for that and no any img tags , any one help me out how can i traverse that to fetch my data . in jsoup i can;t find any way to traverse this data with key value pairs like in JSON. first i tried using API but this is limited option ,
edge_sidecar_to_children
under this all links i want to fetch , any body help me out from this please
See the data that i want to display
Jsoup is not a browser and it does not run JavaScript. So if a JavaScript needs to run to populate some HTML from JSON, you are out of luck.
Two solutions:
1) Parse the JSON within the page. Jsoup can then only be used to get the page and maybe get the raw JSON, but then you should use a specialized library for JSON parsing.
2) Use a browser, i.e. something like Selenium webdriver. This will start a real (headless) browser that you can control via Java. This way all JavaScript will run and you can fetch the HTML afterwards. This approach may be resource hungry and probably runs a lot slower than method 1.

JavaScript GET to retrieve one variable

I haven't really written any javascript but am building an iOS application that will utilize JavaScriptCore's framework to read a javascript code to get a variable. What I'm looking to do is set up a GET (I think) so that I can retrieve JSON data from a url and then pull a specific string from the JSON data. Within the GET method, I'll need to add credentials and one parameter. What is the best practice to do this?
As per Rory's statement above the server you are requesting the json data from must either be on the same domain as your Application/Js code or support the CORS headers.
If the above is true, then you can either use JQUery as suggested above, or for a more minimalist approach the W3Schools has a tutorial on basic Ajax.
https://www.w3schools.com/xml/dom_httprequest.asp

Using a javascript function to insert IP into database?

I've been using PHP to insert a user's IP address into a database like this:
INSERT INTO ip_hits (address) VALUES ('$user_ip')
One of our new vendors is requiring that we use a Javascript function to do this and I'm not very familiar with how to use Javascript for this sort of thing.
Basically I simply need a Javascript function, that when executed, will insert the current visitor's IP address into our database. Is this possible to do? I've been looking for simple examples of doing this, but all of the answers seem quite different.
You can use ajax, sending the IP to your controller, that will insert it in your DB as you did till now.
without Jquery : How to make an AJAX call without jQuery?
with Jquery, use $.post() method : https://api.jquery.com/jQuery.post/

How do I populate form fields on a page from a database based on the page url?

Hello first I’d like to say, please excuse my ignorance to this all, as I’m very new to this all. I just started and still trying to understand this.
So far I have a database set up and I’m trying to retrieve values from a database to fill in a form on a page when it loads. The record or row/values that need to be retrieved from a database depend on the page’s URL.
I’m ok with html and css but still trying to learn more about jquery, JavaScript, sql, php and so on and so forth. I realize I have a ways to go and honestly some of the guides and tutorials online are kind of confusing because everyone has a different way of coding. So I’m a bit confused.
I’ve included a simple chart to breakdown what I’m trying to do.
If someone could point me in the right direction I’d be really grateful! Thanks.
If I understand you well, you want to setup a form and populate some fields of this form with a query forms a database, the primary key of the record being relative to the url.
The first step is to build the url, you can pass some parameters to an url by adding a ? at the end of the url followed by the parameter name, the = sign and the parameter value. If you have more than one parameter, you should separate each parameter with the sign &.
So your url could be something like this :
www.examplesite.com/page&.html?key=key_A1
Then, you'll have to choose if you want to build the page on the server side with a language like php, in which case you retrieve the parameter, query the database, build the html form and send it to the client.
You can also go client side with plain javascript or jquery, in which case you will still have to do some server side programming to query the database but will use an ajax call to get the data and will populate the fields in Javascript or JQuery.
You can do this using Javscript(using Ajax)
U need the key to search for get the results from database
Using ajax call get the data, You might have to write code to get the details from database using any server side prog language
Using javascript to fill the form input with the received data.
Google for jquery ajax examples, and how to populate input using javascript.
There are many frameworks out there that have DataBinding built-in that do the same job a lot easier. My favourite would be Angular Js, you can try Ember and lot more out there, choose the one you feel comfortable with.

Categories

Resources