kobena Posted July 8, 2011 Share Posted July 8, 2011 Hi mates, I have three php variables and want to use them in my javascript to get data:[$value1,$value2,$value3]; assume that $value1=1.5; $value2=1.6; $value3=1.7; Can anyone show me how to get around this? so that i get data:[1,5,1.6,1.7] in my script? Link to comment Share on other sites More sharing options...
Paul Swanson Posted July 8, 2011 Share Posted July 8, 2011 Use PHP to write the javascript, just like any other text. This example uses the heredoc syntax. <?php echo <<<EOT <script type="text/javascript"> data:[$value1, $value2, $value3]; </script> EOT; ?> Or, you could embed PHP tags within javascript: <script type="text/javascript"> data:[<?php echo $value1 . ',' . $value2 . ',' . $value3; ?>]; </script> It's easier to use PHP variables with javascript than it is to use javascript variables with PHP. For that you would write the javascript variable to a cookie, then use PHP to read the cookie. 2 Link to comment Share on other sites More sharing options...
HartleySan Posted July 9, 2011 Share Posted July 9, 2011 If you want to try another method, you could also use Ajax. That's built for send information between JavaScript and PHP. If you're like more information about that, lemme know. Link to comment Share on other sites More sharing options...
kobena Posted July 9, 2011 Author Share Posted July 9, 2011 If you want to try another method, you could also use Ajax. That's built for send information between JavaScript and PHP. If you're like more information about that, lemme know. Please lemme know how to go about it using Ajax. Link to comment Share on other sites More sharing options...
HartleySan Posted July 10, 2011 Share Posted July 10, 2011 You have to use what is called the XMLHttpRequest object in JS. Basically, you initialize that depending on your browser, and then you send a request to a PHP page, which can access the database. After getting the data you want from the database, you can format it however you want, and then send it back to the JS, from which you can do whatever you want with it. If you want to know all the details, I highly recommend Larry's Ajax book or checking out some online tutorials. If you're not interested in learning all that, then maybe Paul's method above is best for a simple, quick solution. The following is the basic flow of Ajax: JavaScript var oAjax = getXMLHttpRequestObject(); // You have to write this function, which will make an appropriate Ajax object, depending on your browser. // Read Larry's book or search online for how to write this function. var sName = 'Billy Bob'; // I'm setting this here, but this value can easily be gotten dynamically from a form, etc. oAjax.open('get', 'someScript.php?name=' + sName); oAjax.send(null); // This is always null for the get method. oAjax.onreadystatechange = function () { // The Ajax custom event. if (oAjax.readyState === 4) { // Need to test for this. if (oAjax.status === 200) { // ...And this. var aInfoFromPHP = oAjax.responseText.split('****'); // Splitting on the random delimited I chose in the PHP script. document.getElementById('someDIV').innerHTML = sName + ' is ' + aInfoFromPHP[0] + ' years old, and his girlfriend's name is ' + aInfoFromPHP[1] + '.'; } } }; someScript.php <?php $name = $_GET['name']; $returnString = ''; $q = "SELECT * FROM table-name WHERE name='$name'"; $r = mysqli_query($dbc, $q); while ($row = mysqli_fetch_array($q, MYSQLI_ASSOC)) { $returnString = $row['age'] . '****' . $row['girlfriend']; } echo $returnString; ?> Well, that's real rough, and not too commented, but hopefully it gives you an idea of the process. 1 Link to comment Share on other sites More sharing options...
kobena Posted July 10, 2011 Author Share Posted July 10, 2011 You have to use what is called the XMLHttpRequest object in JS. Basically, you initialize that depending on your browser, and then you send a request to a PHP page, which can access the database. After getting the data you want from the database, you can format it however you want, and then send it back to the JS, from which you can do whatever you want with it. If you want to know all the details, I highly recommend Larry's Ajax book or checking out some online tutorials. If you're not interested in learning all that, then maybe Paul's method above is best for a simple, quick solution. The following is the basic flow of Ajax: JavaScript var oAjax = getXMLHttpRequestObject(); // You have to write this function, which will make an appropriate Ajax object, depending on your browser. // Read Larry's book or search online for how to write this function. var sName = 'Billy Bob'; // I'm setting this here, but this value can easily be gotten dynamically from a form, etc. oAjax.open('get', 'someScript.php?name=' + sName); oAjax.send(null); // This is always null for the get method. oAjax.onreadystatechange = function () { // The Ajax custom event. if (oAjax.readyState === 4) { // Need to test for this. if (oAjax.status === 200) { // ...And this. var aInfoFromPHP = oAjax.responseText.split('****'); // Splitting on the random delimited I chose in the PHP script. document.getElementById('someDIV').innerHTML = sName + ' is ' + aInfoFromPHP[0] + ' years old, and his girlfriend's name is ' + aInfoFromPHP[1] + '.'; } } }; someScript.php <?php $name = $_GET['name']; $returnString = ''; $q = "SELECT * FROM table-name WHERE name='$name'"; $r = mysqli_query($dbc, $q); while ($row = mysqli_fetch_array($q, MYSQLI_ASSOC)) { $returnString = $row['age'] . '****' . $row['girlfriend']; } echo $returnString; ?> Well, that's real rough, and not too commented, but hopefully it gives you an idea of the process. Thanks a lot. Worked for me. Link to comment Share on other sites More sharing options...
Recommended Posts