Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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');
}
share|improve this question
    
strip "textarea" from the echo statement, and it will output in normal text. –  php_purest May 19 at 4:18
    
as in the var textTextArea? –  user2168066 May 19 at 4:24
    
I meant <textarea> –  php_purest May 19 at 4:25
    
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 at 4:27
    
I also noticed you're not using an echo, when you should normally open php, and leave it for speed. –  php_purest May 19 at 4:27

4 Answers 4

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

share|improve this answer
    
That is what I was attempting, $reqNumber is an int field from my database. If it is not processing as a string is there a way to make it? –  user2168066 May 19 at 4:39
    
@user2168066 If it is a number, it should be fine without quotes. –  wong ian May 19 at 4:49

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);
share|improve this answer
    
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 at 4:59

Based on this other question: http://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);?>';
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.