SitePoint Sponsor |
|
User Tag List
Results 1 to 11 of 11
Thread: php and js concat problem
-
Apr 21, 2009, 04:24 #1
- 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
-
Apr 21, 2009, 04:52 #2
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."')Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Apr 22, 2009, 01:47 #3
- 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";
?>
-
Apr 22, 2009, 02:57 #4
-
Apr 22, 2009, 02:57 #5
- 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.
-
Apr 22, 2009, 02:58 #6
-
Apr 22, 2009, 03:13 #7
- 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?
-
Apr 22, 2009, 03:30 #8
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.Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Apr 22, 2009, 04:32 #9
- 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?
-
Apr 22, 2009, 05:05 #10
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>
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.Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Apr 22, 2009, 05:58 #11
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.
Bookmarks