This question already has answers here:
PHP loop; how to print each result and delay it for a second before echoing another result?
(2 answers)
Closed 8 years ago.
I have something like this
<?php
for ($x=0; $x<10; $x++) {
echo $x;
}
?>
Now i need to display that $x data one after another with one second delay; i believe i need javascript to do that.
I've found various of similar scripts, but none worked for me, can anyone post any example on how to do that?
Thanks in advance
Yes, you'll need JavaScript for this. All PHP can do is emit the values to client-side code. Then it's up to the client-side code to display those values.
So, for example, your PHP code can populate a JavaScript array:
<script type="text/javaScript">
var values = <?php echo json_encode($php_variable); ?>;
</script>
In this case $php_variable would be an array containing the values you want to display. Now your client-side code has an array called values which it can iterate. You can use setTimeout to schedule something to happen with a delay. Iterating over that array, it might look something like this:
var index = 0;
var displayValue = function () {
if (index >= values.length) { return; }
var value = values[index++];
// display "value" somewhere on your page
setTimeout(displayValue, 1000);
};
displayValue();
This should display each value in the array with 1-second increments. Note that I sort of glazed over the "display somewhere on your page" part, since that's up to you really. Where/how are you looking to display this? This works a little differently client-side than it does server-side, since the entire HTML document is already rendered. You need to identify where in that document you want to display the value and display it there, not just "echo" it.
Related
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 5 years ago.
So I need to request an image from a database, this then gets put in a variable, and gets called up in an echo.
However, i'm in a shortage of quotemarks in this case, seeing as the PHP echo uses double quotes, And then after calling up the onmouseover I use the single quotemarks, but after that I cannot use the double quotemarks again for the URL that gets caled up,
Going around this by putting the whole command in a variable didn't work.
Putting the whole command into the database didn't work.
Putting the command or url in a shorthand if statement didn't work.
And putting the Javascript code into a function, Also does not work.
Here is the code I'm talking about.
$afbeeldingurl = $row["afbeeldingurl"];
$overurl = $row["overafbeelding url"];
echo "<div><a href='../".$url.".html'><img onmouseover=mouseover() src='images/".$afbeeldingurl."' alt='' /></a>";
<script>
function mouseover()
{
this.src='images/<?php$overurl?>';
}
</script>
I thank you in advance :)
(Note, Only Javascript allowed, I cannot call up Jquery)
this.src='images/<?php$overurl?>';
Try switch this to
this.src='images/<?php echo $overurl; ?>';
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
I'm not sure if this is even possible, but here goes.
I have a PHP function called code_check_result($code_name). It checks in the database and returns a bunch of information regarding the supplied code's name.
The name of the code is found within a span tag in the page. I've made a javascript function that opens a new window which should then execute the PHP function. However, the variable needed in the PHP function is stored in a jquery var as I have selected the span tag and stored whatever's inside.
Below you can see the part of the code where the window is created (just the part with the PHP function).
myWindow.document.write('Tjek: ' + code_name + '</br></br></br></br><?php code_check_result("kode2"); ?>');
"kode2" is the actual name of a code. The above works exactly as it's supposed to. However, if I substitute the "kode2" string with the javascript variable, 'code_name', it doesn't work. Like this:
myWindow.document.write('Tjek: ' + code_name + '</br></br></br></br><?php code_check_result("' + code_name + '"); ?>');
The first occurence of code_name writes it to the page, but the second one within the PHP function doesn't work.
I'm pretty sure this is just because the PHP is executed before the 'code_name' variable is written as a string. But I'm not entirely sure.
So, I'd appreciate it if you could either help me figuring out how to do this, OR tell me to abandon this idea because it's not possible and then give me some pointers as to how else to solve this.
Given this:
the variable needed in the PHP function is stored in a jquery var
and this:
I've made a javascript function that opens a new window
It sounds like you just need to pass a value to the URL being opened in that window. So where you might have something like this:
window.open('somePage.php');
You would instead have this:
window.open('somePage.php?someValue=' + myVariable);
The URL of the window being opened is a normal URL like any other. You can include values on the query string, and the PHP code in somePage.php can read them from the$_GET collection.
It will be easier to create $_POST variable with jQuery. Something like this:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$.ajax({
type: "POST",
url: "[URL GOES HERE]",
data:{ VARNAME: VALUE },
success: function(data){
console.log(data);
}
})
</script>
This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 7 years ago.
I am currently looking for a way on how can I access a javascript variable and use it in my php script (same page). I heard about using ajax, but I don't really know where to start, I have little experience when using ajax, could someone please help me start with this problem? The code below is just an example on what I am trying to do:
<script>
var fruit = "apple";
</script>
I am trying to get the value of the variable fruit for example using a php script
<?php
$fruit = code to get the value maybe....
?>
My example above must be wrong and I know ajax is the way to go, but could someone please help me on how to approach or start with it? I would appreciate it! Thanks!
in the js you use ajax through jquery to send the vars to php
var fruit = "apple";
$.get('server.php',{fruit:fruit});
in server.php
$fruit = $_GET['fruit'];
of course opening server.php directly will cause an error, you have to run the javascript code in the browser, use the developer tools to inspect the request and the php file output.
Even better you can get back the server.php output in javascript like this:
var fruit = "apple";
$.get('server.php',{fruit:fruit})
.done(function( data ) {
alert( "Data Loaded: " + data );
});
This question already has answers here:
Efficient way to Pass variables from PHP to JavaScript [duplicate]
(8 answers)
Closed 9 years ago.
What is the best way to pass a server-side PHP variable to Javascript?
To simplify the problem assume that we have a variable in PHP ($phpVar) and we want to assign its value to a Javascript variable (jsVar)
Javascript files are loaded in html - they are not created dynamic!
Some food for thought:
1. Print with PHP before loading Javascript files:
<script language="javascript" type="text/javascript">
var jsVar= <?php echo $phpVar?>;
</script>
2. Store in DOM (in hidden elements)
a. in PHP:
<span data-name="phpVar" data-value="<?php echo $phpVar?>"></span>
b. Read in Javascript files (assuming jQuery available):
var jsVar= $('span[data-name="phpVar"]').attr('data-value');
3. Ask it with AJAX after page has loaded
Obviosly not the best solution. Doesn't fit to all scenarios and requires an additional request...
In conclusion:
They both seem ugly to me... Is there a better approach?
Is there any frameworks that can handle this dependecies? Please keep server reconfiguration minimal.
The best approach would be providing an "internal API" requested via AJAX from client side.
Doing this way you can keep your sides separated.
After this, the fastest way is printing in a shared file the values you want to share (as you wrote in your question).
As a last note: if you carry on with the second way I would suggest you
json_encode()
as a really helpful method to pass arrays and objects from php to javascript.
So if you have your php array:
$array = array( "a" => 1, "b" => 2 );
<script language="javascript" type="text/javascript">
var js_array= <?php echo json_encode( $array ) ; ?>;
</script>
It depends on the situation, but in common, predominantly to use first case. But don't forget about quotes if you pass a string:
<script language="javascript" type="text/javascript">
var jsVar = '<?php echo $phpVar?>';
</script>
I am in need of help again, this time with a little Javascript snippet. There's also a little bit of PHP involved. The PHP is as follows (to generate random numbers)
$no1 = rand(0,9);
$no2 = rand(0,9);
$no3 = $no1+$no2;
I'm using this as a captcha method, for a very simple contact form. I'm not a fan of the bulky methods that I find all over google searches.
To validate this code, I use the following Javascript
if(document.forms["feedback"].check.value !== "<?php echo $no3; ?>"){
window.alert("Incorrect security code");
return false;
}
Then inside my form, I just use this:
<?php echo $no1; ?> + <?php echo $no2; ?> = <input name="check" type="text" id="check" size="3">
It all works as it's supposed to, but if there was a way for me to not to just directly echo $no3 in my javascript here:
if(document.forms["feedback"].check.value !== "<?php echo $no3; ?>")
Then that would eliminate any way of viewing the source to find the answer. I'm very much aware that it's a simple math problem, and if anyone knows how to view source, they most definitely can add, but a colleague of mine pretty much insisted on this.
So, does anyone know a way to help me out? I pretty much wrote most of this by hand, including the javascript, so please be nice when telling me that my code is horrible and out dated..
Thanks!
Put the $no1 and $no2 variables inside of span tags (or anything else you can directly target with Javascript) and then get the values from those spans with JS and use that to calculate the sum in JS rather than echoing it into the source with PHP, e.g. give the spans IDs like #captcha-1 and #captcha-2 use getElementByID to retrieve the values, then do the validation entirely in Javascript.
It will still be easy for anyone with a brain to figure out how to bypass but at least the answer will not be present anywhere in the source code other than as a JS variable.
If you want to hide the validation process entirely, you will need to use JS and AJAX to send the sum input by the user to a serverside PHP function which checks to see if it is equal to $no1 + $no2 and then submit the form as part of the response success function.
All that being said, this is still a very weak spam deterrent method.