Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm currently in the middle of programming something and I've ran into an annoying problem...

my function here takes an ID that is set when the user on my website clicks a button, now long story short this particular function generates graphs depending on the button clicked, my issue is the information for the graphs is taken from a MYSQL database in php earlier, and named dynamically, so the button with the ID one, connects to some php information called $exampleHere1;

heres my code, this is working however currently it's hard coded.

function createGlobals(ID){

        stringVisitsCost = [<?php echo $stringVisitsCost1 ; ?>];
        stringVisitsImpressions = [<?php echo $stringVisitsImpressions1; ?>];
        stringSMImpressions = [<?php echo $stringSMImpressions1; ?>];

        drawChart();

    }

As you can see the ID is passed in when the button is pressed earlier on, I've tested that and it does pass in the correct ID, my issue is I'm not sure how I'm going to make this function make use of the ID as the information is stored in PHP, and of course PHP Cannot take information from Javascript.

It would be amazing if there was some way of slotting the ID variable in the short php segment, with some fancy syntax, or any solutions you can suggest would be useful really to avoid me having to use a non-salable case statement or something...

Thanks very much in advance guys! Have a great day,

I'll be working on this all night so any suggestions I'll try to reply as soon as possible, Thanks again guys.

EDIT: following the comments I've tried to use AJAX to parse the variable to php, though this is my first time using ajax

function createGlobals(ID){

        //post the ID from JS to PHP using AJAX
        $.ajax({
                url: '<?php echo $_SERVER['PHP_SELF'];?>',
                type: 'POST',
                data: { ID: ID },
                success: function(data) {

                    stringVisitsCost = [<?php

                    if(isset($_POST['ID'])){

                        $ID = $_POST['ID'];

                        echo ${"stringVisitsCost" . $ID};

                    }?>]

                }
        });

        drawChart();

    }

I like this idea of using AJAX, however it's not currently working, potentially a due to a syntax issue, or my general lack of knowledge in ajax, is this correct syntax or is the issue with trying to achieve this in the first place?

share|improve this question
    
Use ajax to pass the ID, and return with a json array or object. –  lolka_bolka Nov 20 '14 at 16:39
    
As you have guessed, js and php dont work together like that - by time the js runs, the php has already finished and closed down. You have 2 options. One is to make an ajax request, the other is to output all data into a javascript object/array, and select that by property/key. –  Steve Nov 20 '14 at 16:39
    
I've edited the OP thanks to your answers @lolka_bolka, and Steve Any suggestions when it comes to that AJAX issue (I assume)? Thanks for the fast replies guys –  ashlewis Nov 20 '14 at 17:25
    
Given your ajax rewrite: that data in your success handling function is the output that PHP generates when you call the url that's echoed into the url property. PHP no longer plays a part in that the data is just plain string data, with nothing in it that can spontaneously "execute". Now it's Javascript's task to interpret that data, and extract the values from it, so usually you make PHP generate a JSON string, and then in javascript you parse that string to a real object with JSON.parse(data) (remember to try/catch, because you could be getting malformed JSON) –  Mike 'Pomax' Kamermans Nov 20 '14 at 17:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.