getting php to run ONLY in a javacript call function

Hello All,

I have a webpage on my webhost server. The page is hello.php
This page contains php and javascript codes.
I have 7 javascript codes for linking to my other webpages on the same server.
This is just one of seven:

[code]<?php print "Click here for Seal History
";?>

[/code]

What I need to do is, create a random number between 100-999 in php within the win1() function before the clicked page opens. I do not want the server to create this number unless the win1() function has been called by clicking on the “Click here for Seal History” link.

I have put other php scripts within the win1() function and the server has processed them even when the link was not clicked.

I then need to take the 3 digit random number and make it a filename ie 794.txt
I then need to open this file on my webhost server, write a “1” to the file, then close the file.
This all must be done within the win1() function only when the link is clicked.

Is there any php code that will do what I need.?
Cheers

Well, not clear on what you are asking. Do you mean you want to insert a rand number into the JS?

Let’s explain JS and PHP… First, PHP is SERVER-SIDE only and JS is CLIENT-SIDE only.
Therefore, mixing them is not very complicated.
ALL PHP code is handled on the server before JS even exists!
The output of the PHP is mixed into the data that is sent to the CLIENT (or what most call the browser.)…

So, you can create a variable in your Javascript code and you can place a value in it using PHP.

If you want to, as you said, create a random number and within a JS routine, you would just echo out the value of the PHP variable inside the JS script… Is that what you want?

If so, PHP creates the random number using something like this: $rand_number = rand(100, 999);
And, to insert that into a routine like you posted, guessing that the number is to be passed inside of the
function call. You would do this something like:
[php]

<?php print "Click here for Seal History
";?>

[/php]
or without the added print command:
[php]

Click here for Seal History
[/php] (Why use PHP to handle the HTML?) Hope that helps...

Hi ErnieAlex,

Thanks for your contribution.

The penny just dropped about the differences in js and php.

I will now convert my scripts to php and forget about js, this should solve my problems.

Thanks to all for contributing.

Well, glad to help. I just wasn’t sure what you wanted to do.

Always remember the difference in SERVER-SIDE and CLIENT-SIDE (browser) makes a big difference.
AND, server is more secure than client.

Good luck, I will mark this one solved…

Sponsor our Newsletter | Privacy Policy | Terms of Service