在 PHP echo 中嵌入 javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4123387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Embed javascript in PHP echo
提问by benhowdle89
echo "<td> + manuf + </td>";
Is this above ever going to work??
上面的这个能用吗??
I'm pulling results from a mysql db to edit the contents but need the jQuery functionality to edit it, hence the embedded javascript variable...
我正在从 mysql db 中提取结果来编辑内容,但需要 jQuery 功能来编辑它,因此嵌入了 javascript 变量...
EDIT:
编辑:
Sorry for the lack of context, its related to another question i've asked on here Mysql edit users orders they have placedthis is the end goal. To edit the order i place, i need to pull the results into an environment similar to how the user placed the order. So my thinking was to include the jQuery functionality to add items to a cart etc, then they could press submit and in the same way i used .Ajax to post the data to an insert php script i would post the values to an update php script! Is this backwards thinking, any advice welcomed!
抱歉缺乏上下文,它与我在此处提出的另一个问题有关,Mysql 编辑用户下的订单是最终目标。要编辑我下的订单,我需要将结果提取到类似于用户下订单的环境中。所以我的想法是包含 jQuery 功能以将项目添加到购物车等,然后他们可以按提交,并以我使用 .Ajax 将数据发布到插入 php 脚本的相同方式我会将值发布到更新 php 脚本!这是逆向思维吗,欢迎任何建议!
采纳答案by RobertPitt
I suggest you take a look at the follwing.
我建议你看看下面的。
Now your simplest solution under you circumstances is to do go for the json_encodemethod. Let me show you an example:
现在,在您的情况下,您最简单的解决方案是使用json_encode方法。让我给你看一个例子:
$json_data = array(
'manuf' => $some_munaf_data
);
echo "<script type=\"text/javascript\">";
echo "var Data = " . json_encode(json_data);
echo "</script>";
This will produce an object called Data, and would look like so:
这将产生一个名为 的对象Data,看起来像这样:
<script type="text/javascript">
var Data = {
munaf : "You value of $some_munaf_data"
}
</script>
Then when you need the data just use Data.munafand it will hold the value from the PHP Side.
然后,当您需要数据时,只需使用Data.munaf它,它就会保存来自 PHP 端的值。
回答by Scoop
Would you not echo out the jQuery within a Javascript code island? You need the client-based code (jQuery) to be able to execute after the server-side code (PHP).
您不会在 Javascript 代码岛中回显 jQuery 吗?您需要基于客户端的代码 (jQuery) 才能在服务器端代码 (PHP) 之后执行。
echo '<td><script language = "JavaScript" type = "text/JavaScript">document.write("");</script></td>';
回答by robertc
Try just emitting the MySQL content with PHP:
尝试只用 PHP 发出 MySQL 内容:
echo "<td id='manuf'>".$manuf."</td>"
Then get the contents with jQuery like this:
然后像这样使用 jQuery 获取内容:
var manuf = $('#manuf').text();
回答by Conex
Consume you have the table echoed with php:
消费你有与 php 相呼应的表:
<table id="sometab">
<tr>
<td>
</td>
<tr>
</table>
The jquery for printing resuls in any td is :nth-child(2) takes 2 table td object :
在任何 td 中打印结果的 jquery 是 :nth-child(2) 需要 2 个表 td 对象:
<script type="text/javascript">
$(function(){
$("#sometab tr td:nth-child(2)").html("bla");
})
</script>
回答by XMen
i think you cant embed the jquery variable in the php like this . you just give the class name here from here when edit will be click you will get the variable as in submit click in other questions .
我认为你不能像这样在 php 中嵌入 jquery 变量。您只需在此处提供类名,当点击编辑时,您将获得变量,如在其他问题中提交点击一样。
回答by SlamDunk
Is "manuf" a JS variable or part of a PHP output e.g. part of generated ?
“manuf”是 JS 变量还是 PHP 输出的一部分,例如生成的一部分?
Basically this can easily be done by: mysql thru PHP(*I can't put table tag..sorry.):
基本上这可以通过以下方式轻松完成:mysql thru PHP(*我不能放置表标签..抱歉。):
while($row = mysql_fetch_object($result)) {
echo 'tr';
echo 'td a href="#" class="myres"'.$row->manuf.'/a /td';
echo '/tr';
}
then on your JS just attach a "click" handler
然后在你的 JS 上附加一个“点击”处理程序
$(function() {
$(".myres").click(function() {
//my update handler...
});
});
回答by Pekka
Is this above ever going to work??
上面的这个能用吗??
Nope. You'd need to output valid JavaScript for the browser to interpret:
不。您需要输出有效的 JavaScript 以供浏览器解释:
echo "<script>document.write('<td>'+manuf+'</td>')</script>";
But that is a dreadful construct, and I can't really see why you would need this, seeing as the td's contents are likely to be static at first.
但这是一个可怕的结构,我真的不明白你为什么需要这个,因为td的内容一开始可能是静态的。

