Embed javascript in PHP echo

16,483

Solution 1

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.

Solution 2

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

Solution 3

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();
Share:
16,483
benhowdle89
Author by

benhowdle89

Consultant software engineer and advisor to companies building software products.

Updated on June 14, 2022

Comments

  • benhowdle89
    benhowdle89 almost 2 years
    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!