1

I am a beginner at php/ javascript and I am trying to access Mysql database using a prepared statment in php. Once I get the row needed, I want to send it to Javascript for processing (comparison, incrementation... ) I checked similar questions to mine but most of them propose solutions similr to this one:

echo 'var result= ' . json_encode($exampleArray) . ';';

I don't want to print the data esult but rather to send it to javascript? How Can I do this?

1
  • echo is needed to add text to the htnl source/javascript. Give a try and see what happens. Html sorce and Javascript is inly text and php creates this text , which a browser than can interpret Commented Jan 28, 2020 at 17:14

2 Answers 2

2

You should consider doing this the other way around using Ajax. You get the Javascript side to send a request to a php script that will calculate everything and send back the desired data as a string. You can then use that string in Javascript without ever needing to print the value in your front end.

First send an Ajax request just before using the string.

Make sure you have the jQuery plugin and that this code is inside a jQuery document ready.

You can Embed a CDN of jQuery here https://code.jquery.com/

$( document ).ready(function() {
$.Ajax('/ajax.php', 
{
    dataType: 'json', // type of response data
    timeout: 500,     // timeout milliseconds
    success: function (data,status,xhr) {   // success callback function
        //Your string is inside data.responss
        alert(data.responss+" is your value");
    },
    error: function (jqXhr, textStatus, errorMessage) { // error callback 
        $('p').append('Error: ' + errorMessage);
    }
});
});

Then in your ajax.php file you echo the var in json format

<?php
// [...]
$data["responss"] = 2000;
echo json_encode($data);
?>

Your result will be inside data.responss and available inside the success {} You can create a fallout event if something goes wrong using the error portion or by testing if data.responss is what you are expecting.

Keep in mind that PHP is server side and Javascript is client side. PHP will always be executed first. So using Ajax, you can call the PHP result at the right time in your load. So sending something to Javascript (Client side) can't be done from the server side because it's already executed. You can print your Javascript from PHP but then you would have the value in clear text inside the body of your HTML.

Anyway, hope that makes sens to you. Let me know if you have problems implementing this.

Sign up to request clarification or add additional context in comments.

1 Comment

Glab this helped. Good luck ;-)
0

Please note that

echo 'var result= ' . json_encode($exampleArray) . ';';

is not about printing. In server side, this creates the contents of page being sent to client. The Client page has javascript variable result is available for you to process (compare, increment... ). You shall use

JSON.parse()

can be used to retrieve subset values and continue your process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.