Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    Question right syntax to call a function inside a string?

    I beleive my code is not outputting the link
    Code:
     echo '<button onclick="openChildBrowser("http://www.google.com")">Open Google</button>';

    Please help, think I have got something wrong?! ' or " or \?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Code:
    echo '<button onclick="'.openChildBrowser("http://www.google.com").'">Open Google</button>';
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    31
    Thanks
    0
    Thanked 7 Times in 7 Posts
    I think openChildBrowser() is a JavaScript function, not a PHP function. If it's a JS function, then the issue may be the quotes, which should be alternated.

    Code:
    echo '<button onclick="openChildBrowser(\'http://www.google.com\')">Open Google</button>';

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This needs clarification.
    If openChildBrowser is a PHP function (which by name doesn't sound like it is), then fegall's answer is what you want. Otherwise, its a Javascript issue and kbduvall's code is what you need. But I need to know to determine if this is left in the PHP forum or moved to the Javascript forum.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    I was assuming that as it is posted in the PHP forum that the function is a PHP one. If it isn't PHP then there's nothing that requires that the HTML be echoed rather than simply swapping from PHP back to HTML and the simplest solution becomes:

    Code:
    ?><button onclick="openChildBrowser('http://www.google.com')">Open Google</button><?php
    Last edited by felgall; 05-31-2012 at 03:52 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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