简体   繁体   中英

Defined Var not echoing value jquery and php

Hi I am trying to get my php variables to echo in my js code. I know technically php doesnt work in js/jquery but I've searched for workarounds and they dont seem to be working as expected. This is what I have --

<script type="text/javascript"> 
var stR = "<?php echo $htitle ?>";
var seq = "<?php echo $sequence ?>";
var coM = "<?php echo $comment ?>";
mrp_data_callbacks.push( function(index, data, stR) {
data["htitle-" + seq] = stR;
return data;
});
mrp_data_callbacks.push( function(index, data) {
data["comment-seq"] = coM;
return data;
});
</script>

And this is what is printed --

 var stR = "testing comments";
var seq = "0";
var coM = "testing";    

mrp_data_callbacks.push( function(index, data, stR) {data["htitle-" + seq] = stR;
return data;});

mrp_data_callbacks.push( function(index, data) {data["comment-seq"] = coM;
return data;});

So the php echos correctly when defining the variables but when using them in my functions, nothing works. I tried different combination of things and still nothing.

Basically in my functions I need "coM, seq, and stR" to echo the values.

What am I doing wrong?

EDIT: This is my goal and what I mean by I need the values to echo. ---

modify to add your custom meta field values.

jQuery(document).ready(function() { 
mrp_data_callbacks.push( function(index, data) {
data["hello"] = "world";
return data;
});
});

"Hello" -- My meta key "World" -- The Value

data["hello"] = "world";

Where it says world, I am trying to have to output of the var(s) I created.

 var stR = "testing comments";
 data["htitle-" + seq] = testing comments;

The code uses the values you provided, but there is no code that actually calls the callback functions you have defined.

I have taken your code and added some tests around it, so to show that the values are indeed available to the two functions:

// Initialise the array of functions.
// Probably this is done by a library you loaded,
// of which you have provided no information:
mrp_data_callbacks = [];

// --- Original code BEGIN ---
var stR = "testing comments";
var seq = "0";
var coM = "testing";    

mrp_data_callbacks.push( function(index, data, stR) {data["htitle-" + seq] = stR;
return data;});

mrp_data_callbacks.push( function(index, data) {data["comment-seq"] = coM;
return data;});
// --- Original code END ---

// Define data
var data = {};
// Call the above functions for testing the result:
mrp_data_callbacks.forEach( function(fun, index) {
    fun(index, data, stR);
});
// check if everything worked, and data has received 
// the expected properties. We use JSON.stringify to 
// have a complete view of the data object:
alert(JSON.stringify(data));

The alert at the end outputs this:

{"htitle-0":"testing comments","comment-seq":"testing"}

So, this proves the values provided by PHP are readily available in the functions, through the variables stR, seq, coM . But you need to actually call these functions to see anything happening in your data object.

NB: console does not refer to what you see in "view source" in your browser, but to the tool in which you can query information about the displayed web document.

Edit

If you want the PHP values to be directly injected in the relevant Javascript functions, then you don't need the Javascript variables ( stR, seq, coM ), and you can do as follows in PHP:

<script type="text/javascript"> 
    mrp_data_callbacks.push( function(index, data) {
        data["<?php echo "htitle-" . $sequence ?>"] = "<?php echo $htitle ?>";
        return data;
    });

    mrp_data_callbacks.push( function(index, data) {
        data["comment-seq"] = "<?php echo $comment ?>";
        return data;
    });
</script>

This will come to the browser as this:

<script type="text/javascript"> 
    mrp_data_callbacks.push( function(index, data) {
        data["htitle-0"] = "testing comments";
        return data;
    });

    mrp_data_callbacks.push( function(index, data) {
        data["comment-seq"] = "testing";
        return data;
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM