2

I am trying to live update content on the page then the user clicks buttons.

content section:

<p class="chapterContent"><?php echo $chapterContent[$chapter]; ?></p>

Here are my buttons:

<span class="navigationItem"><a href="" onclick= <?php $chapter = 0; ?>>1</a></span>  
<span class="navigationItem"><a href="" onclick= <?php $chapter = 1; ?>>2</a></span>

$chapterContent is an array with different strings. So then user clicks on 1 it loads $chapterContent[0] if he clicks 2 it loads $chapterContent[1] What I have does not work and how can I do it that the html code in class .chapterContent reloads with correct chapter? Any help appreciated!

3
  • 2
    You need to look at jQuery to achieve that. Commented May 25, 2015 at 23:40
  • 1
    jQuery or any other AJAX methodology. Don't ignore vanilla-js.com Commented May 25, 2015 at 23:51
  • can you show your full code & what is the error Commented May 25, 2015 at 23:52

1 Answer 1

2

What you needs is something like this!

<p id="chapterContent"></p>

<span class="navigationItem"><a href="" onclick="return display_text('0');">1</a></span>  
<span class="navigationItem"><a href="" onclick="return display_text('1');">2</a></span>

<script type="text/JavaScript">
<!--

function display_text( which )
{
    $.post( "display.php", { which : which }, function( data ){ document.getElementById( "chapterContent" ).innerHTML = html =  data }, "html" );

    return false;
}

//-->
</script>

put this inside the display.php file:

<?php echo $chapterContent[$_POST["which"]]; ?>

You should of course add code that adds security etc!

Sign up to request clarification or add additional context in comments.

Comments

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.