在PHP echo中嵌入javascript

问题描述:

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

EDIT:

Sorry for the lack of context, its related to another question i've asked on here Mysql edit users orders they have placed this 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!

  echo“&lt; td&gt; + manuf +&lt; / td&gt;”; 
  code  >  pre> 
 
 

以上是否会起作用? p>

我从mysql数据库中提取结果来编辑内容但需要jQuery功能 编辑它,因此嵌入的javascript变量...... p>

编辑: p>

对于缺乏上下文感到抱歉,它与另一个问题相关 我在这里询问 Mysql编辑他们已下达的用户订单 这是 最终目标。 要编辑我下的订单,我需要将结果拉入类似于用户下订单的环境。 所以我的想法是包括jQuery功能将项目添加到购物车等,然后他们可以按提交和我使用相同的方式.Ajax将数据发布到插入PHP脚本我会将值发布到更新PHP脚本 ! 这是倒退的想法,欢迎任何建议! p> div>

I suggest you take a look at the follwing.

Now your simplest solution under you circumstances is to do go for the json_encode method. Let me show you an example:

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

<script type="text/javascript">
var Data = {
    munaf : "You value of $some_munaf_data"
}
</script>

Then when you need the data just use Data.munaf and it will hold the value from the PHP Side.

Is this above ever going to work??

Nope. You'd need to output valid JavaScript for the browser to interpret:

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.

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

echo '<td><script language = "JavaScript" type = "text/JavaScript">document.write("");</script></td>';

Try just emitting the MySQL content with PHP:

echo "<td id='manuf'>".$manuf."</td>"

Then get the contents with jQuery like this:

var manuf = $('#manuf').text();

Consume you have the table echoed with 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 :

<script type="text/javascript">
$(function(){
   $("#sometab tr td:nth-child(2)").html("bla");
})   
</script>

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 .

Is "manuf" a JS variable or part of a PHP output e.g. part of generated ?

Basically this can easily be done by: mysql thru PHP(*I can't put table tag..sorry.):

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

$(function() {
   $(".myres").click(function() {
        //my update handler...
   });
});