All,
I’m building a small PHP site using an MVC pattern. Inside my Controller, I have an array of articles that I want to send to the View page. However, I want the articles to appear one at a time in the View, and only change to the next one when the user clicks on one of several buttons. One button just allows the user to see the next article, other buttons allow the user to “grade” or endorse the article (this info is sent back to the controller) and move on to the next article.
How should I do that? The buttons are within a form, and I have Javascript functions attached to each. A simplified version of the code would look like that:
In Controller:
<?php
$array=array(10,11,12,13,14,15,16);
$_SESSION['next']=true;
foreach($array as $a){
if($_SESSION['next']){
$_SESSION['next']=false;
$view=$a;
}
}
?>
In View:
<script type="text/javascript">
function doOne(){}
function doTwo(){}
</script>
<form class="ReviewArticles" method='POST' accept-charset="UTF-8" action='index.php?action=reviewArticles'>
<p>
<textarea rows="4" cols="60" name='article' id='article' WRAP=SOFT>
<?php
echo $view;
?>
</textarea>
</p>
<p>
<button type="button" name='showNext1' id='showNext1' onclick="doOne()" >Show next 1</button>
<button type="button" name='showNext2' id='showNext2' onclick="doTwo()" >Show next 2</button>
</p>
</form>
I need to send back to the Controller some info to load the next item in the array. I also need to send some other data related to the user “endorsing” of the article, and I want those to take place together (only one button).
I would say you have on of two options:
1) Pull the article information via an AJAX request and display when the users wishes to move onto the next article. This is the best way in my opinion as it guarantees that the user has the most up to date versions of the article.
2) Store the article information in the page when it is loaded. i.e. write the data into a javascript array, hidden html elements, or whatever your preferred method. When the user hits the button, load the article using the data stored on the page.
Does that answer your question?