SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String creation problems!

    Hi.

    Sorry for the poor subject title! I have know idea what to class this as.

    I am trying to create a link in php and I am really struggling.

    This is what I should end up with

    Code HTML4Strict:
    <a onclick="return bookWebinar(385875262,"http%3a%2f%2fwww.google.com") href="#"><img src="my_content/images/book.jpg" width="131" height="45" alt="Book Now" border="0" /></a>

    However when looking at the source code of the generated html this is what I am ending up with
    Code HTML4Strict:
    <a href="#" http%3a%2f%2fwww.google.com")"="" onclick="return bookWebinar(385875262,">
    <img border="0" width="131" height="45" alt="Book Now" src="my_content/images/book.jpg">
    </a>

    This is the php I am using to generate this.

    Code PHP:
    $urltodirect = urlencode("http://www.google.com");
    $prams = $webinarKey .",".'"'.$urltodirect.'"';
    $button = '<a onclick="return bookWebinar('.$prams.')" href="#"><img src="my_content/images/book.jpg" width="131" height="45" alt="Book Now" border="0" /></a>';

    If I drop the $urltodirect from the php then it works just fine and creates the button/link and it works.

    What am I missing please!

    Thanks
    Chris

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You are trying to output html and js using PHP.

    To avoid the nightmare of which quote goes where, try using PHPs heredoc syntax to interpolate PHP variables into your string:

    Start off with:
    PHP Code:
    $link = <<<EOL

    Boo!

    EOL;

    echo 
    $link
    To make sure you understand that that last heredoc delimeting line EOL; HAS to be the first thing on that line ...

    Then go to putting your target string in heredoc, make sure that works.... (added some hard returns for ease of reading)

    PHP Code:
    $link = <<<EOL

    <a onclick="return bookWebinar(385875262,'http//www.google.com')" href="#">
    <img src="my_content/images/book.jpg" width="131" height="45" alt="Book Now" border="0" />
    </a>

    EOL;

    echo 
    $link
    And then finally start adding your REAL variables back in to the string, do it one at a time and test on each iteration:

    PHP Code:
    $urltoredirect "http://www.google.com";
    $link = <<<EOL

    <a onclick="return bookWebinar(385875262,'
    {$urltoredirect}')" href="#">
    <img src="my_content/images/book.jpg" width="131" height="45" alt="Book Now" border="0" />
    </a>

    EOL;

    echo 
    $link
    You can optionally use the {} parenthesis to delineate the variables if you wish, as I have done.

    That way you can mix single and double quotes so that your output is valid html/js without worrying about PHP string concatenation quote requirements mucking the water up.

    HTH restore some sanity!

  3. #3
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much Cups.

    I think the forum added some white space to the end of the examples as that threw me for a little while!

    Never mind sanity now restored and I can get this 5 minute job finished that I started 3 hours ago!!!

    Cheers
    Chris

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    We've all been there ...

    Eventually the "am I going mad?" feeling becomes more familiar, and you even get used to it for a while!

    Then after many code cycles of paring code back and back again to find really silly errors that you should eventually have a light bulb moment and realise you should start at the bottom and get into the habit of writing a bit, testing it, forcing PHP to prove what you think SHOULD happen (or should NOT happen in many cases) then carrying on.

    Extreme cases cause you to arrive at the door of Test Driven Development.

    Good tricks to learn :

    use var_dump() on all your variables, especially POST/GET and use echo to in order to inspect what is going on.
    turn on error reporting on you development server

    When working with JS and PHP :

    inspect the html source code of your page
    use heredoc syntax in PHP to assemble the code
    get the JS working FIRST with hard-coded values, then replace the values with variables
    check out the IDEs which support JS validation/markup

    Mixing PHP/JS can be a real nightmare though - it compounds the possibilities for mistakes.


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
  •