Changing content when reload [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
random images display
Have anyone see an image or other content that changing randomly when we reloading the page?
What is that? Can you tell me how to do that?

Guess, something like this would do the trick.
/* div for your future image */
<div id="img"></div>
<script>
window.onload = function(){
/* array of urls to your images and random integer number between 0 and array's length-1 */
var imgurls = [url1,url2,url3,...,urlN],
randn = Math.floor(Math.random()*imgurls.length);
/* create an HTML tag <img src="urlN"> in your div */
document.getElementById('img').innerHTML = '<img src=' + imgurls[randn] + '>';
}
</script>

Related

Link click counter, and show how many times link is clicked [duplicate]

This question already has an answer here:
Count Number of Clicks on a link (Without onclick) [closed]
(1 answer)
Closed 4 years ago.
i want to count clicks when this link is clicked. AND show "how many times(number) link clicked"in example.com
not to use mysql.and better php if not,then javascript is ok
example.com
For a pure JS implementation, you could use localStorage as follows:
<html>
<script>
function init() {
let count = localStorage.getItem('counter');
if(count === null){
count = 0;
localStorage.setItem('counter', count);
}
count = parseInt(count);
updateCount(count);
}
function incrementCounter() {
let count = parseInt(localStorage.getItem('counter'));
count = count + 1;
localStorage.setItem('counter', count);
updateCount(count);
return true;
}
function updateCount(count) {
document.getElementById("count").innerHTML = "Clicked "+count+" times!";
}
</script>
<body>
<p id="count">-</p>
<script type="text/javascript">
init();
</script>
Google
</body>

How do you make color-changing text withJS? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So, let's say I want to have a piece of text on my website, that changes color every second. I do know how to use JS to change the color of the text, but not how to continue to auto-change it based on certain parameters. For example, cycling through thousands of colors, not just one or two.
ok here is a simple way to blink text with colors
Js :
// List of colors
var spectrum = ['#f00', '#f66', '#969', '#00F', '#0FB53F'];
var cycle = spectrum.length-1;
// Cycle speed
var speed = 300;
var i = 0;
window.setInterval(function(){
document.getElementById('index').style.color = spectrum[i];
if (i < cycle) i++;
else i = 0;
}, speed);
HTML
<p id="index">Flashing text</p>
Demo
You can simple use html inside PHP:
echo "<font color='red'>Hello World</font>";
for a loop you can simple create an array with all the html collors:
$colors=array();
$colors[1]="red";
$colors[2]="blue";
// soo on....
foreach($colors as $color){
echo "<font color='$color'>Hello World</font>";
}
setInterval is what you want.
Fiddle
http://www.w3schools.com/jsref/met_win_setinterval.asp
var myColours = [...];
var index = 0;
var myElement = /*get your element*/
setInterval(function() {
/* Set your element. Are you using Javascript to get your element? */
myElement.style.color = myColours[index];
/* Are you using jQuery? */
myElement.css('color', myColours[index]);
index++;
if(index > myColours.length - 1){
index = 0;
}
}, 1000);

Insert values generated by PHP in a Javascript array [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
I'm trying to make a list where the user can select 5 items. Those items are generated through PHP. When the user clicks on 1 item that item needs to be saved in an Javascript array. Once the user selected 5 items, those needs to be saved into an mysql database.
Atm I can select items, but the array remains empty.
My code:
<script type="text/javascript">
var genrearray = new Array(4);
var i=1;
$(document).ready(function kleurveranderen() {
$(".keuzeDiv").on("click", function kleurveranderen() {
if(i<=5){
$(this).toggleClass("green");
genrearray[i] = document.getElementById("$Keuzescherm['Genrenaam']").innerHTML;
i++;
}
else{
alert("Teveel genres geselecteerd.");
}
})
});</script>
this is standing between script tags but for some reason he isnt showing them.
my php code:
<?php while($Keuzescherm= $allGenres->fetch_assoc())
{
echo "<div class='keuzeDiv' id='".$Keuzescherm['Genrenaam'] ."'>"
. $Keuzescherm['Genrenaam']."</div>";
}?>
the id of the div is the same as the value.
my css code to indicate that the div is selected
.keuzeDiv{
background-color:#dddddd;
padding-top:5%;
color: black;
padding-bottom: 10px;
}
.green{
background-color:#8eb461;
color:white;}
So my question is: How can I fill the array with the php values and save that array to a MySql database?
I think what you should be doing is:
genrearray[i] = $(this).text();
You can't reference PHP variables in Javascript. And the variable you were trying to reference is from the while loop, so its value outside the loop is not very useful.

how to post counter value from javascript to database [duplicate]

This question already exists:
how to post value of 3 input fields into database
Closed 9 years ago.
i have a java script function i want to post value of counter to my data base... how to do...this is a function to create 2 texbox and 1 select box on click... but whn i am applying another input tag to get and post counter value another input text box is getting reated... i dont want this i just want to post total value to counter whn submitt buttion is clicked
<script>
var counter=1;
function generateRow() {
var count="<font color='red'>"+counter+"</font>";
var temp ="<p> <div class='_25'><input type='textbox' id='textbox' name='stop"+counter+"' placeholder='Stop Name'></input></div> <div class='_25'><input type='textbox' id='textbox' name='timing"+counter+"' placeholder='Timing'></input></div> <div class='_25'><select id='ampm"+counter+"' name='ampm"+counter+"'><option>a.m</option><option>p.m</option></select> </div>";
var newdiv = document.createElement('div');
newdiv.innerHTML = temp + count;
var yourDiv = document.getElementById('div');
yourDiv.appendChild(newdiv);
counter++;
}
</script>
The absolute most basic way I can think of is:
Add an iframe somewhere in your page:
<iframe id="iframe"></iframe>
Create a php file counter.php
<?php
$counter = (int) $_GET['counter'];
$query = mysql_query('INSERT INTO table (counter) VALUES (' . $counter . ')');
// OR YOUR OWN QUERY TO DO THIS
?>
And add to your function the following javascript code:
document.getElementById('iframe').src = 'counter.php?counter=' + counter;
Obviously you can do it more complicated via ajax and read the json response etc but at its most basic level, this will suit you. Basically the trick is, you set the iframe source to the counter.php and pass the counter in a get parameter. When an iframe source is changed, it automatically loads, hence the server will then recieve the counter parameter and process it.
You can add display:none to the iframe to hide it visually.
Given the tags on the question:
Make an HTTP request (possibly using XMLHttpRequest) to a PHP handled URL that reads the data (likely from $_POST) and uses the PDO API to make an SQL query using that data.

Show HTML in Javascript If else statement [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript - document.write error?
I have a piece of Javascript that I'm using that checks to see if a div is a certain style and then runs an if else statement:
window.onload = function () {
if (document.getElementById('slidingDiv').style.display == 'none') {
document.write('<img src="images/show-more-arrow.jpg" width="61" height="45">');
} else {
document.write('<img src="images/show-less-arrow.jpg" width="61" height="45">');
}
};
This works OK, but it only shows this HTML image on the screen. I need it to add the HTML to the existing code on the page.
Many thanks
Use appendChild or innerHTML
example:
document.getElementById("elemId").innerHTML = '<img src="images/show-less-arrow.jpg" width="61" height="45">'
var wrapper = document.createElement('div');
var image = document.createElement('img');
image.src='images/show-more-arrow.jpg';
wrapper.appendChild(image);
Something like this.

Categories