SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Jun 2006
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php and js concat problem

    <SCRIPT LANGUAGE="JavaScript">
    <!--
    var ht = 100;
    var wt = 100;
    //-->
    </SCRIPT>
    <?php
    $txt = "<a href=\"#\" onclick=\"javascript: window.open('". $mitem->link ."', '', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width="+wt+",height="+ht+"'); return false\" class=\"$menuclass\" ". $id .">". $mitem->name ."</a>\n";
    ?>

    When I am trying to concat ht and wt, the above code is giving error. What am I doing wrong?

    Regards,
    Nilanjan

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    h="+wt+",height="+ht+"')

    if it's javascript, escape the double quotes:

    h=\"+wt+\",height=\"+ht+\"')

    if it's PHP, use the dot as concatenation:

    h=".wt.",height=".ht."')

  3. #3
    SitePoint Addict
    Join Date
    Jun 2006
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a JS concatenation within php. Please check this. It's not working.

    <SCRIPT LANGUAGE="JavaScript">
    <!--
    wt=screen.availWidth;
    ht=screen.availHeight;
    //alert(wt);
    //alert(ht);
    //-->
    </SCRIPT>
    <?php
    echo "<a href=\"#\" onclick=\"javascript: window.open('my-commitment.php', '', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=".wt.",height=".ht.",top=0,left=0'); return false\" >Click</a>\n";
    ?>

  4. #4
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can't just use javascript variables as though they were php constants like that, there is no way that can work unless wt and ht are php constants.

    Why are you using php for this? If you want to include the javascript variables in the link, just use js alone.

  5. #5
    SitePoint Addict
    Join Date
    Jun 2006
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have changed the code now. Please check.


    <SCRIPT LANGUAGE="JavaScript">
    <!--
    wt=screen.availWidth;
    ht=screen.availHeight;
    //alert(wt);
    //alert(ht);
    //alert(wt+ht);
    //-->
    </SCRIPT>
    <?php
    echo "<a href=\"#\" onclick=\"javascript: window.open(\"my-commitment.php\", \"abc\", \"toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=\" + wt + \",height=\" + ht + \",top=0,left=0\"); return false;\" >Click</a>";
    ?>

    I am showing you part of the code. I can't use it with only JS. Please tell me what am I doing wrong here? Strangely popup is not opening.

  6. #6
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Also you need to escape your quotes for javascript as well as php - php will just output the " which will make the html tag invalid. You need to 'double escape' some of those quotes.

  7. #7
    SitePoint Addict
    Join Date
    Jun 2006
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    View source is showing the below code:

    <SCRIPT LANGUAGE="JavaScript">
    <!--
    wt=screen.availWidth;
    ht=screen.availHeight;
    //alert(wt);
    //alert(ht);
    //alert(wt+ht);
    //-->
    </SCRIPT>
    <a href="#" onclick="javascript: window.open("my-commitment.php", "abc", "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=" + wt + ",height=" + ht + ",top=0,left=0"); return false;" >Click</a>

    Can you please rewrite the right code?

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    If you want to know the right JS syntax to put the value of a JS variable in the onclick event, you'd better ask in the JS forum.
    If you already know the correct syntax, but are having problems creating it with PHP, then just post the correct syntax here, and we'll show you how to write the PHP echo command.

  9. #9
    SitePoint Addict
    Join Date
    Jun 2006
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there someone who can tell me what I have done wrong in this code. If I know the correct syntax then why should I post it here?

  10. #10
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by neel1979 View Post
    Is there someone who can tell me what I have done wrong in this code. If I know the correct syntax then why should I post it here?
    I thought I was quite clear in my explanation, but I guess not
    "Here" is the PHP forum. It seems to me your problem is not PHP, but the correct syntax of a JS command. If this is correct, then you'd better ask your question in the JS forum.

    Something like

    Why doesn't this code work?

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    wt=screen.availWidth;
    ht=screen.availHeight;
    //alert(wt);
    //alert(ht);
    //alert(wt+ht);
    //-->
    </SCRIPT>
    <a href="#" onclick="javascript: window.open("my-commitment.php", "abc", "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,widt h=" + wt + ",height=" + ht + ",top=0,left=0"); return false;" >Click</a>
    If you already know the exact syntax of the JS code you need, but you can't get PHP to create it for you, then post the JS code you need here, and we'll help you write the PHP code that will create that JS code.

    Until know you didn't post the correct JS code. You only posted the result of your PHP command, which is working fine. But the result is not what you want. I'm no JS expert, so unless you give me the correct JS, I won't be able to help you with the PHP part.

  11. #11
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    From the code you have posted so far, there is no requirement for the HTML to be written via PHP and echo. However, if you really do want the code as you've posted so far:

    PHP Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    wt=screen.availWidth;
    ht=screen.availHeight;
    //alert(wt);
    //alert(ht);
    //alert(wt+ht);
    //-->
    </SCRIPT>
    <?php

    echo '<a href="#" onclick="window.open(\'my-commitment.php\', \'abc\', \'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=\' + wt + \',height=\' + ht + \',top=0,left=0\'); return false;">Click</a>';

    ?>

    The JavaScript code within the onclick attribute could not contain double-quotes as that would close the attribute string prematurely. HTML doesn't have a concept of escaping quotes (ie, onclick="myfunc(\"blah\")" wouldn't work (the onclick value would be myfunc(\ only)). To that end, I've made the HTML use double quotes and the JavaScript use single quotes. Now, since the PHP is echoing a single-quote-delimited sting then the JavaScript's single quotes need to be escaped (once only) in order to make the string valid PHP syntax.

    Finally, this would have been a whole lot simpler (and prettier) if you wanted to use nice HTML/JavaScript code in the first place.
    Salathe
    Software Developer and PHP Manual Author.


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
  •