Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Passing php variable as Javascript argument

  1. #1
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default Passing php variable as Javascript argument

    My question is related to both Javascript and PHP

    This is supposed to be a simple task. I'm successfully passing 2 php variables to a javascript function.

    Code:
    PHP:
    $a; // this is name of employee
    $b; // this is address of employee which multiline and is different for every employee
    
    <a onClick="function('<?php echo $a ?>', '<?php echo $b ?>');">LINK</a>
    
    Javascript:
    
    <script language="JavaScript" type="text/javascript">
    function(a,b){
    
    }
    
    </script>

    But now i've modified my code, added line breaks through nl2br() in $b after fetching it from db, and trying to pass it to function(). Instead of sending it as JS function argument, it prints the $b.

    Code:
    PHP:
    $a; // this is name of employee
    $b; // this is address of employee which multiline and is different for every employee
    $c=nl2br($b);
    
    <a onClick="function('<?php echo $a ?>', '<?php echo $c ?>');">LINK</a>
    
    Javascript:
    
    <script language="JavaScript" type="text/javascript">
    function(a,b){
    
    }
    
    </script>

    Can anyone please guide me through this. Thanks a lot in advance!!!!!!!!

  2. #2
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    The code you posted wouldn't work, but you said it did work so there must be more code that you're not showing us...

    Could you please provide us with the full script?
    Or if not, an example of what $a and $b are and the contents of your js function.



    This little test script works fine...
    PHP Code:
    <?php
    $a 
    'Fatber Christmas';
    $b '27 Sunshine Street /n America';
    $c nl2br($b);
    ?>
    <html>
    <head>
    <script type="text/javascript">
    function test(a,b) {
        alert(a);
        alert(b);
    }
    </script>
    </head>
    <body>
    <a onClick="test('<?php echo $a ?>', '<?php echo $c ?>');">LINK</a>
    </body>
    </html>

  3. The Following User Says Thank You to keyboard For This Useful Post:

    zomb1e (08-11-2012)

  4. #3
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by keyboard1333 View Post
    The code you posted wouldn't work, but you said it did work so there must be more code that you're not showing us...

    Could you please provide us with the full script?
    Or if not, an example of what $a and $b are and the contents of your js function.



    This little test script works fine...
    PHP Code:
    <?php
    $a 
    'Fatber Christmas';
    $b '27 Sunshine Street /n America';
    $c nl2br($b);
    ?>
    <html>
    <head>
    <script type="text/javascript">
    function test(a,b) {
        alert(a);
        alert(b);
    }
    </script>
    </head>
    <body>
    <a onClick="test('<?php echo $a ?>', '<?php echo $c ?>');">LINK</a>
    </body>
    </html>
    Thanks for your reply!

    Let me explain to you what I'm trying to do. I want to take input from users in a textarea which could be a number of paragraphs and save it in MySQL db. Now, that's working fine as I've tried it myself.

    Now I want to display that information (which i've fetched from MySQL db through queries and saved them in php variables, eg, $a) through a javascript (passing php variables) which is called through 'onClick'.

    When I print $a without passing it to JS function it display entire string without any linebreaks, but when I try to pass $a to JS function and print it through an OnClikck event, nothing gets printed. (referring to the below code)

    Code:
    <?php
    $a = very long string
    $b = "Hello People!!!!";
    ?>
    <html>
    <head>
    <script type="text/javascript">
    function test(a,b) {
        document.getElementById('div1').innerHTML=a;
        document.getElementById('div2').innerHTML=b;
    }
    </script>
    </head>
    <body>
    <a onClick="test('<?php echo $a ?>', '<?php echo $b ?>');">LINK</a>
    <div id="div1"></div>
    <div id="div2"></div>
    </body>
    </html>
    Is there a way to ensure that I can print that long string through <div id="div1"></div>.
    Also I want to pass $a in a way that all the linebreaks are retained in it (like the way it is present in MySQL db).

    I also tried using below code, but this code prints $a when I call onClick event rather than passing it to test() function.

    Code:
    <?php
    $a = very long string
    $b = "Hello People!!!!";
    $a=nl2br($a);
    ?>
    <html>
    <head>
    <script type="text/javascript">
    function test(a,b) {
        document.getElementById('div1').innerHTML=a;
        document.getElementById('div2').innerHTML=b;
    }
    </script>
    </head>
    <body>
    <a onClick="test('<?php echo $a ?>', '<?php echo $b ?>');">LINK</a>
    <div id="div1"></div>
    <div id="div2"></div>
    </body>
    </html>
    Can you please help. I'd really appreciate any method or suggestion!!!

  5. #4
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    I'm busy at the moment, but I'll have a look at it later... (probably tonight)

  6. The Following User Says Thank You to keyboard For This Useful Post:

    zomb1e (08-12-2012)

  7. #5
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    how are you inserting the line breaks? you could manually put in <br /> tags or you could generate a brek tag evvery time a line reaches a certain length.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  8. The Following User Says Thank You to bernie1227 For This Useful Post:

    zomb1e (08-12-2012)

  9. #6
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Bernie -
    nl2br() converts all new lines to <br/> tags...

  10. The Following User Says Thank You to keyboard For This Useful Post:

    zomb1e (08-12-2012)

  11. #7
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    True, but still it is probably a good idea to try it with actuall br tags in order to see if that changes anything.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  12. The Following User Says Thank You to bernie1227 For This Useful Post:

    zomb1e (08-12-2012)

  13. #8
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    After looking through your code, I can't see anything wrong.... Would it be possible for you to provide us an example of $a?

  14. The Following User Says Thank You to keyboard For This Useful Post:

    zomb1e (08-12-2012)

  15. #9
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    Same with myself, I can't seem to see why it would print the $a variable, as there is no code in there which would do that, is this the full code you are using?

    @keyboard
    Father isn't spelt with a b
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  16. The Following User Says Thank You to bernie1227 For This Useful Post:

    zomb1e (08-12-2012)

  17. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    zomb1e, as much as is possible, please show us the actual code you are using in your testing. copy-and-paste is a great help. this:
    PHP Code:
    <?php
    $a 
    very long string
    $b 
    "Hello People!!!!";
    ?>
    is obviously not any part of the code you're actually using - if it were, you'd be getting a fatal parse error.
    Sharing the actual code will help prevent confusion while people are trying to help you figure things out.

    One more suggestion:

    When you run this, check the output source to make sure PHP is printing what you want it to.

    It's very helpful to remember that php and javascript don't actually interact - PHP is just writing text - so, really, you're not "passing variables" - you're just "writing text."

    (BTW, the code you posted -after I fixed the $a line - works just fine for me.)
    Last edited by traq; 08-12-2012 at 07:06 PM.

  18. The Following User Says Thank You to traq For This Useful Post:

    zomb1e (08-12-2012)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •