benhowdle89
benhowdle89

Reputation: 37464

Embed javascript in PHP echo

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!

Upvotes: 2

Views: 17257

Answers (7)

XMen
XMen

Reputation: 30248

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 .

Upvotes: 0

RobertPitt
RobertPitt

Reputation: 57268

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.

Upvotes: 2

SlamDunk
SlamDunk

Reputation: 44

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

Upvotes: 0

Scoop
Scoop

Reputation: 310

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

Upvotes: 1

Conex
Conex

Reputation: 822

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>

Upvotes: 0

robertc
robertc

Reputation: 75707

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

Upvotes: 0

Pekka
Pekka

Reputation: 449465

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.

Upvotes: 0

Related Questions