from php to jquery in a plugin

  • Unknown's avatar

    I try to pass an array from php to jquery.
    The idea is to load data from database once and then to load it in a javascript array to use the javascript array on events like a click to a button

    My plugin is ok and I call script like this:

    function aw_add_scripts() {
    	wp_enqueue_script( 'test_javascript', plugins_url( 'test.js' , __FILE__ ), array('jquery'), '1.0', false );
    }
    add_action( 'wp_enqueue_scripts', 'aw_add_scripts' );

    in every post I add the same thing with this code

    function echo_question($input) {
    	global $wpdb;
    	$id_pere_temp=0;
    	$id_question_temp=0;
    	$tab_question = $wpdb->get_results( "SELECT id_question, id_pere, id_fils_oui, id_fils_non, libelle_fr FROM question", ARRAY_A );
    	foreach($tab_question as $row){
    		if ($row[id_pere]==$id_pere_temp && $row[libelle_fr]<>''){
    			$chosen .= '<br> <br> <div id='libelle' class='text'>'.$row[libelle_fr].'</div>';
    		}
    	}
    	$chosen .=	"<input type='button' id='oui' class='chgtext' value='Oui' >";
    	$chosen .=	'<input type="button" id="non" class="chgtext" value="Non" >';
    
    	return $input . $chosen;
    }
    add_filter('the_content', 'echo_question');

    into test.js I try to do that
    var jArray = <?php echo json_encode($tab_question); ?>;

    I have try with
    jQuery(window).load(function(){
    or
    $(document).ready(function(){
    or
    (function($) {
    but each time the javascipt is loaded in the browser I have the error
    Uncaught SyntaxError: Unexpected token <
    I think the javascript is loaded in wrong moment

    I have found this tutorial but do not solve my problem

    How could I load my php array into my javascript array in my wordpress plugin?

    Do you know a plugin doing this, to copy?

    Many thanks in advance

    The blog I need help with is: (visible only to logged in users)

  • Unknown's avatar

    Hi, WordPress.com doesn’t allow installing 3rd party plugins.

  • Unknown's avatar

    WordPress.com doesn’t allow installing 3rd party plugins by mrlubomir
    What does it means?
    Does it mean that I have to ask this question in an other forum?
    I try to create a new plugin and dont succeed to load php array to javascript array
    Someone could know this issue or a plugin already doing this?

  • Unknown's avatar

    After a second read of you comment mrlubomir, I understand, your comment was related to the automatic sentence “The blog I need help with is bourico.wordpress.com” but I am not talking about this blog, I making the plugin on my computer and not online.
    So if anyone can help me to load php array to javascript array or if someone know a plugin already doing this even for other achievement?

  • This forum is for questions related to sites on WordPress.com.

    If you have a self-hosted WordPress (.org) site you need help with, the best place to ask for help is at http://en.support.wordpress.org

  • The topic ‘from php to jquery in a plugin’ is closed to new replies.