定义的变量不回显值jquery和php

Defined Var not echoing value jquery and php

本文关键字:jquery php 变量 定义      更新时间:2023-09-26

嗨,我正试图让我的php变量在我的js代码中回显。从技术上讲,我知道php在js/jquery中不起作用,但我已经找到了解决方案,它们似乎没有像预期的那样起作用。这就是我的

<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>

这就是打印出来的

 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;});

因此,php在定义变量时会正确回声,但在我的函数中使用它们时,什么都不起作用。我尝试了不同的组合,但仍然一无所获。

基本上,在我的函数中,我需要"coM、seq和stR"来呼应这些值。

我做错了什么?

编辑:这是我的目标,我所说的我需要价值观的呼应。---

修改以添加自定义元字段值。

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

"你好"--我的元密钥"世界"——价值

data["hello"] = "world";

在它说世界的地方,我试图输出我创建的var。

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

代码使用您提供的值,但没有实际调用您定义的回调函数的代码。

我已经获取了您的代码并添加了一些测试,以表明这些值确实可用于以下两个函数:

// 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));

最后的警报输出如下:

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

因此,这证明了PHP提供的值在函数中很容易获得,通过变量stR,seq,coM。但是,您需要实际调用这些函数来查看数据对象中发生的任何事情。

注意:控制台并不是指你在";视图源";在浏览器中,但指向可以查询有关显示的web文档信息的工具。

编辑

如果您希望PHP值直接注入到相关的Javascript函数中,那么您不需要Javascript变量(stR,seq,coM),并且您可以在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>

这将以如下方式出现在浏览器中:

<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>