This question already has an answer here:
Extract Javascript variables of a page via PHP
(1 answer)
Closed 6 years ago.
Hello!
I want to use a variable on my website from a page I loaded using get:
$.get("fetchData.php", function( data ) {
//I want to use the variable `classes`
}, 'html');
The page I am loading has the following variable:
This can't just be copied since it changes every few weeks.
My question is how do I use that variable on my index.php?
So:
I am on index.php I load fetchData.php and I want to use the classes variable from the loaded page. How do I do that?
The easiest way is to use eval
$.get("fetchData.php", function( data ) {
eval(data);
console.log(classes);
}, 'html');
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
I have a code that takes the JSON of a webpage (from a link) and prints it into the console with a promise, using node-fetch (npm node-fetch) and .then
I need to get the content of this specific webpage to work with that content for a project I have, but I cannot see any solution on how to really use it
I have this code:
fetch('https://api.faceit.com/core/v1/nicknames/'+req.query.nickname)
.then(res => res.text())
.then(body => console.log("json -> "+body))
req.query.nickname is a text variable taken from a form that will serve for the link I use to print the JSON
now, when I get to .then(body => console.log("json -> "+body)) it correctly prints me the JSON but I have no clue on how to use the body that I print
Now, after all this, I have 2 questions
how can I used the "body" variable to work with the data I get out of that link?
is there any different way (better or not) to get the content from a page, stored into a variable or an Object, that I can work with?
thanks for everyone for the help in advance. If you need an example of the JSON I need to work with, you can use this https://api.faceit.com/core/v1/nicknames/noxter
Use res.json() instead of res.text() to get the data as an object instead of a string.
Then you can use DOM methods to update the page as usual.
This question already has answers here:
How does Codeigniter receive the ajax post data in controller
(3 answers)
Closed 3 years ago.
I want to pass a variable from JS script to PHP function on a CI Controller. I tried doing this
$.post(base_url+'Controller/function/', {id_formula:id_formula});
but I still get a CodeIgniter error
Message: Missing argument 1 for Controller::function()
Can you help me to solve it? please
That because your controller probably looks like this:
function function($id_formula)
{
...
}
But you are not actually passing "id_formula" as a URL get, but as a post.
Change it to:
function function()
{
$id_formula = $this->input->post('id_formula');
}
This question already has answers here:
Pass JavaScript variable to Flask url_for
(3 answers)
Closed 5 years ago.
$("#getVerification").click(function () {
var tmp=$("#cellphone").val();
console.log(tmp);
$.getJSON("{{ url_for('auth.sendsms', cellphone=tmp )}}",
function(data){
});
});
as the code above, I want to use the string variable tmp as a parameter in function url_for, but I can't figure out how,
Thanks A Lot!
You can't pass javascript variables to Jinja2 because of the way the template gets rendered.
Server side processing is done before client side.
Here is the order.
Jinja2 processes the template into proper markup.
The browser parses the markup into DOM and renders it.
The way this works only allows for passing Jinja2 variables to Javascript.
You need to do without using url_for Jinja2 directive and build your URL client side.
var tmp=$("#cellphone").val();
console.log(tmp);
$.getJSON(["<url_path for auth.send_sms>", tmp].join("/"),
function(data){
});
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 6 years ago.
I have an idea how to pass PHP value into a jQuery variable, I have done it in the past and it worked. But I tried the below code but for some reason it not working. I dont know where the problem lies.
I am trying to hide a custom virtuemart drop down cart I designed when the cart is empty.
jQuery(document).ready(function ($) {
var cart = "<?php if ($data->totalProduct) echo $data->cart_show; ?>"
jQuery('.btn-cart1').hover(if (cart === "") {
jQuery(this).find('.dropdown').hide();
}
else {
jQuery(this).find('.dropdown').hide();
jQuery('.btn-cart1').hover(
function() { jQuery(this).find('.dropdown').stop().show(500)},
function() { jQuery(this).find('.dropdown').stop().hide(500)})
}
});
PHP is server side scripting language and is executed well before your javascript executes on web browser.
The correct execution of your code depends on the values of $data->totalProduct & $data->cart_show after the PHP has been executed on your server side and the values are places in your javascript.
It is not a good programming practice and I would strongly suggest you to use AJAX instead to access the values of PHP variables.
This question already has answers here:
How can I get the title of a webpage given the url (an external url) using JQuery/JS
(3 answers)
How to get the content of a remote page with JavaScript?
(6 answers)
Closed 2 years ago.
How I can read the title of a remote web page with javascript? Suppose the web page is:
www.google.com
I want to read the title of that page; how should I do this?
You won't be able to get this data with jQuery alone, however you can use jQuery to communicate with PHP, or some other server-side language that can do the heavy lifting for you. For instance, suppose we have the following in a PHP script on our server:
<?php # getTitle.php
if ( $_POST["url"] ) {
$doc = new DOMDocument();
#$doc->loadHTML( file_get_contents( $_POST["url"] ) );
$xpt = new DOMXPath( $doc );
$output = $xpt->query("//title")->item(0)->nodeValue;
} else {
$output = "URL not provided";
}
echo $output;
?>
With this, we could have the following jQuery:
$.post("getTitle.php", { url:'http://example.com' }, function( data ) {
alert(data);
});
Getting the content of a remote page you have no control over is going to be a problem because of the same-origin-policy. For more information look here: How to get the content of a remote page with JavaScript?
Try this
alert(document.title);
in your case i guess you would only be using the document.title
The effective method is to write some Server side code (using PHP/ASP/.NET) and pass the URL via AJAX in script and get the title of any remote page.