0

I have am trying to set the value of my PHP echo to a Javascript variable. The JS var then sets that value to an element in an XML page to print a barcode on a label printer.

So far the javascript variable gets the element from a text area and seems to work fine, but when i set the variable to my PHP echo the data format appears to be incorrect. I think I may be setting the echo incorrectly but have ran out of ideas.

At the moment the textarea is populated by the echo so it works, but I need to do without the textarea.

With the textarea

  <?php
        $sqlUpd = "SELECT name, req, printlabel FROM req_requisitions WHERE id = '".$_GET["recordid"]."'";
            $name = $row1['name'];
            $reqNumber = $row1['req'];
            $print = $row1['printlabel'];
        }
?>

segment of my JS-------------------------------------------------------

      function onload()
{
    var textTextArea = document.getElementById("textTextArea");
    var printButton = document.getElementById('printButton');

    // prints the label
    printButton.onclick = function()
    {
        try
        {
            // open label
            var labelXml = loadXMLDoc("barcode.xml");
            var label = dymo.label.framework.openLabelXml(labelXml);

            // set label text
            label.setObjectText("BARCODE", textTextArea.value);

            // select printer to print on
            // for simplicity sake just use the first LabelWriter printer
            var printers = dymo.label.framework.getPrinters();
            if (printers.length == 0)
                throw "No DYMO printers are installed. Install DYMO printers.";

            var printerName = "";
            for (var i = 0; i < printers.length; ++i)
            {
                var printer = printers[i];
                if (printer.printerType == "LabelWriterPrinter")
                {
                    printerName = printer.name;
                    break;
                }
            }

    // prints the label
    printButton.onclick = function()

 My Text Area-------------------------------------------------------

  <div id="textDiv">
    <label for="textTextArea">Label text:</label><br/>
    <textarea name="textTextArea" id="textTextArea"  rows='5' cols='40'><?php echo $reqNumber; ?></textarea>
</div>

Here is what I would like to change to

function onload()  {
    var textTextArea = <?php echo $reqNumber;?>;
    var printButton = document.getElementById('printButton');
}
Lupin
  • 1,225
  • 11
  • 17
user2168066
  • 627
  • 11
  • 21
  • strip "textarea" from the echo statement, and it will output in normal text. – a coder May 19 '15 at 04:18
  • as in the var textTextArea? – user2168066 May 19 '15 at 04:24
  • That part is just putting the value of the echo in the textarea, because the JS variable only seems to accept why i type in that field. What I am trying to do is set var textTextArea = "php echo" instead of "getElementById("textarea"); – user2168066 May 19 '15 at 04:27
  • I also noticed you're not using an echo, when you should normally open php, and leave it for speed. – a coder May 19 '15 at 04:27
  • don't do it, set it to a variable by $textTextArea="php echo"; – a coder May 19 '15 at 04:29
  • You missed to quote the string in «var textTextArea = ;». It should be «var textTextArea = "";». (I don't know if it could be a typo while you written this post or if it is the actual problem). But, without this quotes, your javascript will throw an exception. – bitifet May 19 '15 at 04:56

4 Answers4

1

It cannot pass the value to js but to the textarea?

Is this "$requNumber" is a String?

If yes, try var textTextArea = '<?php echo $reqNumber;?>';

wong ian
  • 123
  • 9
0

Based on this other question: https://stackoverflow.com/a/1035658/2331182

You can convert an integer into string.

Try this:

var textTextArea = '<?php echo (string)$reqNumber;?>';

or

var textTextArea = '<?php echo strval($reqNumber);?>';
Community
  • 1
  • 1
Burning Crystals
  • 1,157
  • 3
  • 19
  • 35
0

I am not sure if this is the best practice, but I usually use cookies when I want to pass data from php to js and vice versa, assuming the data is not sensitive, like passwords,or any such.

PHP

setcookie('reqnum',$reqNumber);

JS

var x = readCookie('reqnum');

I am not really sure if thats the best practice though, but reduces a lot of clutter. Make sure you code it defensive to handle errors.

Kishor
  • 1,513
  • 2
  • 15
  • 25
-1

this:

function onload()  {
    var textTextArea = '<?php echo $reqNumber;?>';
    var printButton = document.getElementById('printButton');
}

Will only work if the JS code is in the same file, if this is an external file this will not work. notice that as @wong ian mentioned you are missing quotes.

i think using jQuery will be better and then you can add the PHP variable as an attribute to an element and then just access it by jQuery

<div id="textDiv">
    <label for="textTextArea">Label text:</label><br/>
    <textarea name="textTextArea" id="myId"  rows='5' cols='40' data_var='<?php echo $reqNumber; ?>'><?php echo $reqNumber; ?></textarea>
</div>

and in the JS

textTextArea = $("#myId").attr('data_var);
Lupin
  • 1,225
  • 11
  • 17
  • Yes the JS is on the same page. I think it's some setting within Dymo print, So the solution to having to have the textarea to print properly but doing it without the text area is just setting the style="display:none". so really it has the appearance of working correctly – user2168066 May 19 '15 at 04:59