1

This is my php code:

<?php
  $typeid = 65;
  $subledgerid = 'subledger'.$typeid;
  $loadledger = 'loadledgers';
   ?>
   <script type="text/javascript">view_subledger('<?php echo $subledgerid;?>',<?php echo $typeid;?>,'<?php echo $loadledger;?>');</script>

This is not calling the view_subledger().

But when i call something like this it will work fine:

<script type="text/javascript">view_subledger('subledger65',65,'loadledgers');</script>

How can i call this?

4
  • show us the output result(page source code). check line 7 : <?php } ?> Commented Nov 28, 2012 at 8:05
  • Is $subledgerid; numeric? Than you are missing the prefix subledger. Anyhow, post us the HTML output that you have when you are doing stuf with PHP. Commented Nov 28, 2012 at 8:06
  • 1
    The last line has a closing brace that doesn't match up. What is above this code in the file?
    – Darkzaelus
    Commented Nov 28, 2012 at 8:09
  • @MikedeKlerk $subledgerid is alphanumeric....
    – Kichu
    Commented Nov 28, 2012 at 8:28

3 Answers 3

2

Here is the safest way to do this:

<?php

$typeid = 65;
$subledgerid = 'subledger'.$typeid;
$loadledger = 'loadledgers';

?>

<script type="text/javascript">
view_subledger.apply(window, <?php print json_encode(array(
  $typeid,
  $subledgerid,
  $loadledger
)); ?>);
</script>

Which generates following code:

<script type="text/javascript">
view_subledger.apply(window, [65,"subledger65","loadledgers"]);
</script>

json_encode() will ensure that variables are escaped properly and with .apply() method you can pass parameter array to JS function.

Another suggested version:

<?php
$typeid = 65;
$subledgerid = 'subledger'.$typeid;
$loadledger = 'loadledgers';
$param_str = implode(', ', array_map('json_encode', array(
  $typeid,
  $subledgerid,
  $loadledger
)));
?>

<script type="text/javascript">
view_subledger(<?php print $param_str; ?>);
</script>

Generates following:

<script type="text/javascript">
view_subledger(65, "subledger65", "loadledgers");
</script>
3
  • 1
    window should be the passed this object. Commented Nov 28, 2012 at 8:12
  • I also suggest changing the method signature to view_subledger(data) in this case. Commented Nov 28, 2012 at 8:17
  • 1) author of the question can change function signature i think; 2) yes json_encode() can be used for any - non array or non object values too.
    – ioseb
    Commented Nov 28, 2012 at 8:24
0

Why is <?php } ?> after the script? Should be like:

<?php
$typeid = 65;
$subledgerid = 'subledger'.$typeid;
$loadledger = 'loadledgers';
?>
<script type="text/javascript">view_subledger('<?php echo $subledgerid;?>',<?php echo $typeid;?>,'<?php echo $loadledger;?>');</script>
1
  • Probably a copy-paste issue Commented Nov 28, 2012 at 8:10
0

This is all the code you have in the file? All this looks OK and must work as far as $subledgerid, $typeid,$loadledger are correct. But what about this line?

  <?php } ?>

It must crash the script

1
  • Probably a copy-paste issue Commented Nov 28, 2012 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.