Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP Syntax for Javascript question

Posted on 2013-01-22
1
Medium Priority
?
341 Views
Last Modified: 2013-01-22
Hi all,

I have the follow show/hide script that works correctly.

<html>
<head>
<title>my javascript show script</title>

<script type="text/javascript">
      function showStuff(id) {
      if (document.getElementById(id).style.display == 'block')
            document.getElementById(id).style.display = 'none';
            else
            document.getElementById(id).style.display = 'block'
      }
</script>

</head>
<body>

<p><a href="#" onclick="showStuff('answer1'); return false;">What price are your apples?</a><br>
<span id="answer1" style="display: none;">Our apples are 30 pence each. If you buy 10 or more we can sell them at a discounted bulk rate of 25 pence each.</span></p>

</body>
</html>

Open in new window


However when I want to add a PHP loop into the file it does not work. Perhaps I have the syntax wrong for this?

Top of PHP part:

$html = array('total_meditations_today' => '<p>Meditations Today: __  </p>',
              'total_journals_today' => '<p>Journal Entries Today: __ </p>',
			  'total_exercises_today' => '<p>Exercises Logged: Today: __  </p>',
			  'total_dyads_today' => '<p>Dyads Completed Today: __ </p>',
			  'total_ebooks_downloaded_today' => '<p>Ebooks Downloaded: __ </p>',
			  'total_ebooks_shelf_today' => '<p>Ebooks On Shelf: __ </p>'
			  );

$res = array('total_meditations_today' => $total_meditations_today,
             'total_journals_today' => $total_journals_today,
			 'total_exercises_today' => $total_exercises_today,
			 'total_dyads_today' => $total_dyads_today,
			 'total_ebooks_downloaded_today' => $total_ebooks_downloaded_today,
			 'total_ebooks_shelf_today' => $total_ebooks_shelf_today,
			 );
arsort($res, SORT_NUMERIC); //Sort an array in reverse order

?>


Body: 

<title>Untitled Document</title>
<script type="text/javascript">
      function showStuff(id) {
      if (document.getElementById(id).style.display == 'block')
            document.getElementById(id).style.display = 'none';
            else
            document.getElementById(id).style.display = 'block'
      }
</script>
</head>

<body>
<p><a href="#" onclick="showStuff('answer1'); return false;">Today:</a><br>
<span id="answer1" style="display: none;"><?php foreach ($res as $k => $v) {
    echo str_replace('__', $v, $html[$k]) . PHP_EOL;
} ?>
</span></p>

</body>
</html>

Open in new window


Any help would be appreciated.

D
0
Comment
Question by:TLN_CANADA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 12

Accepted Solution

by:
sivagnanam chandrakanth earned 2000 total points
ID: 38808653
Change your body like this

<body>
<p><a href="#" onclick="showStuff('answer1'); return false;">Today:</a><br></p>
<span id="answer1" style="display: none;">
<?php 
foreach ($res as $k => $v) {
    echo str_replace('__', $v, $html[$k]) . PHP_EOL;
} ?>
</span>

</body>
</html>

Open in new window


I have removed the </p> after </span> and placed it before <span>..

The problem is </p> tag breaks and creates <p> for all lines within foreach statement which in turn moves the content outside span
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 count occurrences of each item in an array.

721 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