Convert PHP functions to Javascript

Asked

Viewed 375 times

0

I need to convert some functions in PHP to JS, because I will need to put all these functions inside a file . js.

I currently have the following in PHP:

$basicoUS_mensal=processaValor("url_aqui");

function processaValor($url) {
    $result = file_get_contents($url);
    $aspas = strpos($result, "'");
    $valor = substr($result, $aspas+1, -3);
    return $valor;
}

The URL returns as follows, so it has the function that takes only the value: Document.write('11,90');

On the PHP page I was using the following script, follows part of it:

var plano_basicoUS = {
    1: parseFloat(<? echo str_replace(',', '.', $basicoUS_mensal); ?>),
 };

Since . js does not work PHP, I need to change this part to work in the . js file.

In the above case the value of var plano_basicoUS[1] would be: 11.90

What alternative could I use?

1 answer

1

Just change the function processaValor:

$basicoUS_mensal=processaValor("url_aqui");

function processaValor($url) {
    $result = file_get_contents($url);
    $aspas = strpos($result, "'");
    $valor = substr($result, $aspas+1, -3);
    return $str_replace(',', '.', $valor);
}

This change will make the function processaValor return 11.90 to JS.

  • Look at me again heim John! rsrs But how would it look in JS? Pois la funciona o <? php ?>

  • The problem is how to get this value in JS, the way I’m using it only works if it’s in a PHP page, but I need it to stay inside a file. js.

  • I understand... I’d have to do a little gambit there, haha... Pass the script inside the echo, more or less like this: https://ideone.com/r6FR7X. Looking at the gringo stack, if you understand English, you have the recommended one. Do this in an API and access this data using a request: https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript

  • I had already seen this other question, but I could not implement rs. I don’t understand your gambit, take a look: I have the index.php file where the HTML content is, I have the value.js file where the script is above, and I have the value.php file where the values are with the PHP function. The problem is only in the.js file, in the part: var plano_basicoUS = {, where I need to take the value of the URL as if it were in PHP. If you could do everything in JS, put the URL right there, it would be even better, then you wouldn’t use the value.php file..

  • There really is no way to access this data in the .js. file. One output is for you to change the . HTML file extension to . PHP and then do what I did there - so you can pull the data straight from the other PHP file. The way it is, there’s no way you can use PHP in a file. JS, hehe

  • Another output, and the most correct in my opinion, is to create a mini API in the PHP file you have. By accessing valor.php, it would return you a JSON with the values of each category, more or less like this: https://ideone.com/h4RecK and then it is simple to consume this data using an AJAX request.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.