asked on
<?php
// ADD PHP VARABLE TO ARRAY
$text= array();
foreach($guestbook as $post) {
$text[] = $post;
}
?>
<script type="text/javascript">
var fromPHP=<? echo json_encode($text); ?>;
(function myLoop (i) {
setTimeout(function () {
for (i=0; i<fromPHP.length; i++) {
var a= fromPHP[i];
// update html with new text
$("p").html(a);
}
if (--i) myLoop(i); // decrement i and call myLoop again if i > 0
}, 8000)})(999999); // pass the number of iterations as an argument
</script>
var i = 0;
function myLoop()
{
// Get the next item from the array of posts. We do this by
// incrementing the i var and then taking the mod of this with the
// length which will produce an index in the range 0 to length-1
var index = ( (i++) % fromPHP.length );
// Fade out existing content, set new content and fade it back in again
$("p").fadeOut('slow', function() {
$("p").html(fromPHP[index]).fadeIn();
});
}
$(function() {
setInterval("myLoop()", 8000);
});
I have not tested this but the principle should be sound.
ASKER
// print_r ($text); my arrays are collecting the data! test successful!
// print_r ($name);
if (array_key_exists('post',$_POST)) {
echo $text[rand(0,3)];
echo $name[rand(0,3)];
return;
// if a request is made then process and return
}
else {
//echo "hi";
}
?>
<script type="text/javascript">
/*
* PhpFiddles REST API with JSON HTTP response supports Cross-Origin Resource Sharing,
* and can be used with normal Ajax call in any web pages, for example, jQuery Ajax
*/
$(document).ready(function() {
setInterval(getNextPost, 2000);
});
function getNextPost() {
//alert('calling');
$.ajax({
url: "<?php echo $_SERVER["SCRIPT_NAME"]; ?>",
type: "POST", // or "GET"
data: 'post=1',
cache: false,
success: function(data) {
$('.quote').text(data);
$('.test').name(data);
},
error: function(data) {
alert(data.result);
}
});
}
</script>
<div class="quote-box">
<p class="quote">... updating ...</p>
</div>
<img class="quote-thingy" src="<?php echo $this->getThemePath()?>/images/9664dfda-13ef-49c9-b1e4-7bd6171e8aee_11-nubbin.png" width="42px" alt="9664dfda-13ef-49c9-b1e4-7bd6171e8aee_11-nubbin.png">
<div class="w-clearfix by-section">
<img class="person-icon" src="<?php echo $this->getThemePath()?>/images/82116c16-8d7d-444d-8ee6-de9ecd0fbba5_13-person-dude.png" width="66px" alt="82116c16-8d7d-444d-8ee6-de9ecd0fbba5_13-person-dude.png">
<h3 class="test">... updating ...</h3>
</div>
echo $text[rand(0,3)];
echo $name[rand(0,3)];
will only return a random value from the first 4 elements of each array. $rand_index = rand(0,3);
echo $text[$rand_index];
echo $name[$rand_index];
$('#quote').text(data);
$('#test').text(data);
if (array_key_exists('post',$_POST)) {
echo $text[rand(0,3)];
echo $name[rand(0,3)];
return;
// if a request is made then process and return
}
$result = array();
$index = rand(0,3); // In case text and name are related
if (array_key_exists('post',$_POST)) {
$result['text'] = $text[$index];
$result['name'] = $name[$index];
}
die(json_encode($result));
In your success function $.ajax({
url: "<?php echo $_SERVER["SCRIPT_NAME"]; ?>",
type: "POST", // or "GET"
data: 'post=1',
cache: false,
dataType: 'json', // Add this
success: function(data) {
// JSON object returned
$('.quote').text(data.text);
$('.test').name(data.name);
},
error: function(data) {
alert(data.result);
}
});
echo json_encode($result);
return;
ASKER
$result = array();
$index = rand(0,3); // In case text and name are related
if (array_key_exists('post',$_POST)) {
$result['text'] = $text[$index];
$result['name'] = $name[$index];
// print_r ($result); this outputs one text and name successfully.
echo json_encode($result);
return;
}
?>
<script type="text/javascript">
$(document).ready(function() {
setInterval(getNextPost, 2000);
});
function getNextPost() {
//alert('calling');
$.ajax({
url: "<?php echo $_SERVER["SCRIPT_NAME"]; ?>",
type: "POST", // or "GET"
data: 'post=1',
cache: false,
dataType: 'json',
success: function(data) {
// JSON object returned
$('#quote').text(data.text);
$('#test').name(data.name);
},
error: function(data) {
alert(data.result);
// am getting undefined :(
}
});
}
</script>
<div class="quote-box">
<p id="quote" class="quote">... updating ...</p>
</div>
<img class="quote-thingy" src="<?php echo $this->getThemePath()?>/images/9664dfda-13ef-49c9-b1e4-7bd6171e8aee_11-nubbin.png" width="42px" alt="9664dfda-13ef-49c9-b1e4-7bd6171e8aee_11-nubbin.png">
<div class="w-clearfix by-section">
<img class="person-icon" src="<?php echo $this->getThemePath()?>/images/82116c16-8d7d-444d-8ee6-de9ecd0fbba5_13-person-dude.png" width="66px" alt="82116c16-8d7d-444d-8ee6-de9ecd0fbba5_13-person-dude.png">
<h3 id="test" class="test">... updating ...</h3>
</div>
ASKER
ASKER
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<?php
$text= array('wow you guys rock','what a champion','will use you guys next time, everytime','i\'ll be back');
$name= array('bob','beeb','hotbill','i\bebbbf');
$result = array(
'text' => '',
'name' => ''
);
$index = rand(0,3); // In case text and name are related
if (isset($_POST['post'])) {
$result['text'] = $text[$index];
$result['name'] = $name[$index];
// print_r ($result); this outputs one text and name successfully.
echo json_encode($result);
return;
}
?>
<script type="text/javascript">
$(document).ready(function() {
setInterval(getNextPost, 2000);
});
function getNextPost() {
//alert('calling');
$.ajax({
url: "<?php echo $_SERVER["SCRIPT_NAME"]; ?>",
type: "POST", // or "GET"
data: 'post=1',
cache: false,
dataType: 'json',
success: function(data) {
// JSON object returned
$('#quote').text(data.text);
$('#test').name(data.name);
},
error: function(data) {
alert(data.result);
// am getting undefined :(
}
});
}
</script>
<p id="quote" class="quote">... updating ...</p>
<h3 id="test" class="test">... updating ...</h3>
ASKER
<?php
$text= array('wow you guys rock','what a champion','will use you guys next time, everytime','i\'ll be back');
$name= array('bob','beeb','hotbill','i\bebbbf');
$result = array(
'text' => '',
'name' => ''
);
$index = rand(0,3); // In case text and name are related
if (isset($_POST['post'])) {
$result['text'] = $text[$index];
$result['name'] = $name[$index];
// print_r ($result); this outputs one text and name successfully.
echo json_encode($result);
return;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
setInterval(getNextPost, 2000);
});
function getNextPost() {
//alert('calling');
$.ajax({
url: "<?php echo $_SERVER["SCRIPT_NAME"]; ?>",
type: "POST", // or "GET"
data: 'post=1',
cache: false,
dataType: 'json',
success: function(data) {
// JSON object returned
$('#quote').text(data.text);
$('#test').text(data.name);
},
error: function(data) {
alert(data.result);
// am getting undefined :(
}
});
}
</script>
</head>
<p id="quote">... updating ...</p>
<h3 id="test">... updating ...</h3>
</body>
</html>
ASKER
ASKER
ASKER
ASKER
JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.
TRUSTED BY
To make it easier, use jQuery:
Open in new window