Skip to content Skip to sidebar Skip to footer

Pass Javascript Variable Into Php Code

im trying to pass a variable from javascript into a line of PHP include code to create a more dynamic script. But so far I've not managed to achieve this. The logic is as follows:

Solution 1:

If I understood your question you can't do like that.

you can go with ajax request. If you use jquery you can do like this:

var thisfile = [userinput];
$(descrip2).load(thisfile);

or do ajax request by yourself..

Solution 2:

if your file is simple html text you can do this with file_get_contents() function that return content of file:

descrip2.innerHTML = "<?phpecho file_get_contents('textfile.txt'); ?>";

Solution 3:

If you want to use that many times you can encode it (base64) and load that to the form to hidden input for example. Then you will be able to reuse that many times (without calling the ajax every time).

Solution 4:

Server side scripts will always run and render before client side script so when writing your code bear this in mind

I am using jQuery but you can use normal javascript.

<divid="#welcomeMessage"></div>
$('#welcomeMessage').html("<?phpecho'hello'?>");

if using javascript

document.getElementById('welcomeMessage').innerHTML = "<?phpecho'hello'?>";

I am pretty sure this will work too

document.getElementById('welcomeMessage').innerHTML = "<?phpinclude_once('textfile.php'); ?>";

As long as your php file is actually outputting something.

Solution 5:

UPDATE:

From your question and from what you say in the comment. i assume you have a scenario like this:

  1. you have a file with some information.
  2. you want populate your div with theese information.

so before going further create a well formatted file to store all the info. example

  • data.xml
<?xml version="1.0" encoding="UTF-8"?><data><div><h2>lorem ipsum</h2><p>brown fox jump hover the lazy dog</p></div><div><h2>lorem ipsum 1</h2><p>brown fox jump hover the lazy dog 1</p></div></data>

then use AJAX to load the content and append it to the wanted element like below

<html><head><title></title><script>var descriptor;
            functioninit() {
                descriptor = document.getElementById("descriptor");
                loadFromFile('data.xml');
            }

            functionloadFromFile(thisfile) {
                var xmlhttp;
                if(window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = newXMLHttpRequest();
                } else {// code for IE6, IE5
                    xmlhttp = newActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function() {
                    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                        var data = xmlhttp.responseXML.documentElement.getElementsByTagName('div');
                        var divs = '';

                        for(var i = 0; i < data.length; i++) {

                            var div = data[i];

                            divs += '<div>';
                            divs += '<h2>' + div.getElementsByTagName('h2')[0].firstChild.nodeValue + '</h2>';
                            divs += '<p>' + div.getElementsByTagName('p')[0].firstChild.nodeValue + '</p>';
                            divs += '</div>';

                        }

                        descriptor.innerHTML = divs;
                    }
                }
                xmlhttp.open("GET", thisfile, true);
                xmlhttp.send();
            }
        </script></head><bodyonload="init()"><divid="descriptor"></div></body></html>

Post a Comment for "Pass Javascript Variable Into Php Code"