0

I have a website with some Javascript rotating content with a fade. The script works fine until I add a PHP while loop inside it, then it fails. I'm using the PHP to pull content from wordpress posts.

here is the isolated code (http://jsfiddle.net/dzz8M/2/). You'll notice the script works as desired if you remove the PHP and you can see here (http://www.smartcallclaims.com/indexx.php/) that the php is outputting fine.

When you check the source of that link and look for the JavaScript from the JSfiddle you'll see the PHP has generated it's content inside the script. (I know it adds a <p> tag, this is not the issue)

Can some please tell me why the PHP breaks the javascript?

3
  • 1
    As a note, Java and Javascript are two separate things. Also, a jsfiddle, and your site do not help in this matter. Please post the php you are using to generate your other code." Commented Feb 2, 2013 at 9:06
  • All things Daedalus says, and, if you set up a fiddle: set it up properly... Commented Feb 2, 2013 at 9:38
  • I've left the PHP in the JSfiddle page for reference.... The Jsfiddle page was created for the purpose of allowing the community to both see and test the PHP/Javascript for themselves rather than coming back with pointless replies. Commented Feb 2, 2013 at 9:39

1 Answer 1

1

Your problem is the one unescaped linebreak in the quotes:

"\&nbsp;&nbsp;&nbsp;&nbsp;<p>this is a good web test</p>
<div id='quoteauthor'>Ben Harrison</div>",

This is not valid JavaScript. You have to escape it somehow, for example like by using str_replace() in the PHP functions, that create those strings.

str_replace("\n","&nbsp;",$string)
Sign up to request clarification or add additional context in comments.

5 Comments

Do you mean after the </p>? I think I know what you mean.
You're dead right, the PHP is generating the line break server-side, it's not in my code. Have you any suggestions as to how the line break could stay in there without it messing up the Javascript? The str_replace, would that be able to go on the line above the PHP in question? Thanks for helping, unlike the posters above who clearly haven't read the question correctly.
@Sue.My.Chin Do you have access to the function the_excerpt()? The str_replace() should go in there.
the_excerpt() is a custom field within the post type. All you can input is text. I can however create a separate field to use instead of the excerpt. I'll try this now and report back.
I created a separate, plain text-field and it didn't create the line break. Everything renders perfectly. :) I could kiss you Sirko. Thank you very much for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.