-1

I am having issues with syntax . I want to insert a PHP code inside JavaScript. And an error occurs, can anybody help me with this ?

this is the error,

var data1 = $('#assignedWeight').val("<h3>" + <?php echo number_format(data[0]['id'],2); ?>  + "<sup style='font-size: 20px'>Kg</sup></h3><p>TOTAL PROJECT WEIGHT ASSIGNED</p>").val();

The original JS is like this,

showData.push(data[i]["id"]);
var data1 = $('#assignedWeight').val(data[0]['id']).val();
$('#assignedWeight').text(data1);

and i want to insert this code inside the val() in line 2

<h3>
    <?php echo number_format($totalAssignedWeight,2); ?><sup style="font-size: 20px">Kg</sup>
</h3>
<p>TOTAL PROJECT WEIGHT ASSIGNED</p>
Yves M.
  • 29,855
  • 23
  • 108
  • 144
  • 4
    What is the point of this? Javascript is executed on the client side (the browser) wheras PHP is executed on the server side meaning that once the javascript executes there is no more PHP execution. – Maffelu Nov 20 '14 at 08:35
  • What error are you getting? Are you gettin an error on server or client side? – Jørgen R Nov 20 '14 at 08:35
  • [Posible Duplicate](http://stackoverflow.com/questions/3352576/how-to-embed-php-in-javascript) – jrenk Nov 20 '14 at 08:35
  • As Maffelu has already stated, what is the point of this exercise exactly? – Jhecht Nov 20 '14 at 08:36
  • So i am making short poll that refreshes an oracle function but if i just pass the val(data[0]['id']).val() directly into the
    then its gonna mess up the layout. I need the

    and the number_format in order to make the layout correct. Do you have any other suggestion ?

    – user3435659 Nov 20 '14 at 08:38
  • I wonder if giving us more of the code might help clarify what you're trying to do. From the code snippets you've given it looks like (although it may not be the case) that you're not understanding the difference between client and server side – DevDonkey Nov 20 '14 at 08:40
  • you just store that php value in java script variable and then pass it to other variable – saurabh kamble Nov 20 '14 at 08:41

2 Answers2

2

As I pointed out in my comment Javascript executes after the server side code (PHP) is executed. An alternative to this would be to send in any dynamic data through an Ajax request down to the server again and thereby manage to execute the PHP code with the data required through javascript.

Edit:

If all you need PHP for is to format number you can use Intl.NumberFormat:

var number = 3500;

console.log(new Intl.NumberFormat().format(number));
// → '3,500' if in US English locale

Jsfiddle: http://jsfiddle.net/x6nu7a7c/

Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat

Maffelu
  • 2,018
  • 4
  • 24
  • 35
1

First of all: What you're trying to do does not really make sense and, as the other pointed already out, should be handled completely differently.

I still want to give an answer to your question though, since there are sometimes cases when you want/have to build parts of your javascript with php.

Your mistake is, that you think you have to treat your php output like a javascript variable. You ended your double quotes and wanted to insert it there. But this is wrong. From the javascript point of view (which knows nothing about php, since it's rendered on the client side and therefore AFTER all the php was processed (on the server)) the output from php is just a plain text and not a variable. So what you have to do is, to NOT end your double quotes.

var data1 = $('#assignedWeight').val("<h3><?php echo number_format(data[0]['id'],2); ?><sup style='font-size: 20px'>Kg</sup></h3><p>TOTAL PROJECT WEIGHT ASSIGNED</p>").val();

Also note that: if your php output can contain double quotes (") you'd need to escape them with an appropriate javascript function or else you will have javascript errors.

Second note: Obviously this code snippet will only work in files which are actually processed by the php processor (mostly these are only .php files). If you'd move this code snippet to an actual js-file (which is how you do things normally) it won't work anymore.

OschtärEi
  • 2,255
  • 3
  • 20
  • 41