Solved

Trying to concatenate php and javascript

Posted on 2012-03-29
3
370 Views
Last Modified: 2012-08-14
Hi.  I have been trying to concatenate the following code for a pop up window using javascript but have not been successful.  Does anyone have any ideas?  I have worked around similar problems in other parts of this project by using include files and other tricks but, in this case, I really need the concatenated line because I am generating HTML code.  I am getting the error "Parse error: syntax error, unexpected T_STRING....".  I'm sure the problem is that there are single and double quotes already in the string so using a single or double quote for the concatenation is confusing php.  But I don't know how to change it.  I am using very similar javascript on another page and the popup window works fine.  However, that page does not need to concatenate any values so I just used straight html outside of php tags.

Thanks for your help.
Alexis
     $texttoadd3 = $texttoadd3. 
 '<div class="emailweb"><a href="http://www.xxxxx.com/new/contact.php?advertiserid=$id" onclick="window.open('http://www.xxxxx.com/new/contact.php?advertiserid=$id','popup','width=440,height=420,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=450,top=200'); return false">Email Us</a></div>';

Here's the straight html code that I used on another page and it worked perfectly.
<a style="color: #153E7E;text-decoration: none;" href="http://www.xxxxx.com/new/contact.php?advertiserid=<? echo $id;?>" onclick="window.open('http://www.xxxxx.com/new/contact.php?advertiserid=<? echo $id;?>','popup','width=440,height=420,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=450,top=200'); return false">Email Us</a>

Open in new window

0
Comment
Question by:alexisbr
3 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 400 total points
Comment Utility
0
 
LVL 7

Assisted Solution

by:designatedinitializer
designatedinitializer earned 100 total points
Comment Utility
The most straightforward way to do it is with heredoc syntax.
This way you don't have to escape any characters (almost). You can simply copy & paste it from your original HTML.
Like this:

$texttoadd3 = <<<THING
<div class="emailweb">
<a href="http://www.xxxxx.com/new/contact.php?advertiserid=$id" onclick="window.open(\'http://www.xxxxx.com/new/contact.php?advertiserid=$id\',\'popup\',\'width=440,height=420,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=450,top=200\'); return false">Email Us</a>
</div>
THING

Open in new window


check out the man page at:
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
0
 

Author Comment

by:alexisbr
Comment Utility
Thanks to both of you.  I tried to first solution from DaveBaldwin and it worked (with a very slight modfication) so I went with that.  The $id variable was not showing the value.  I had not separated the value in the concatenation statement when I took it out of the straight html.  I pasted the final working line below.

Thanks again for your help.  
Alexis
   
 $texttoadd3 = $texttoadd3. 
 '<div class="emailweb"><a href="http://www.xxxxx.com/new/contact.php?advertiserid=' . $id . ' onclick="window.open(\'http://www.xxxxx.com/new/contact.php?advertiserid=' . $id . '\',\'popup\',\'width=440,height=420,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=450,top=200\'); return false">Email Us</a></div>';

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now