Passing a value held within a JavaScript into a php function

I wouldn’t even call myself a beginner with php but I do understand most other programming languages. I see that the JavaScript code will allow me to call the PHP function (I took out the contents of the function to save space), and the PHP function will properly pass a value back to JavaScript.

The question is how to pass the value (a random number) contained within the JavaScript variable ‘currentAd2’ to the PHP variable ‘$currentAd2’ within the function?

PHP Function:
function bannerurl($currentAd2) {

}

JavaScript Code:
var currentAd2 = Math.round(Math.random() * imgCt1 )
var banner2 = document.getElementById(‘adBanner2’);
banner2.src=<?php bannerurl([b]currentAd2[/b]); ?>

This example simply passes the string “currentAd2” into the PHP function. Can PHP read the value of a hidden html input field?

Much Regards!

Yes, hidden fields are used a lot with php to pass data that is needed in other pages.

So:

will put the value of the hidden input into the php variable $id?

It looks as though this would put the value of the php variable $id into the hidden input value.

I need to be able to put the value of the hidden input, or a JavaScript variable, into the php variable. How do I do that?

If you are doing a submit you would use…

Lets say page one has the hidden field and submit button.

Page 2 or second portion of the page would have the submit button check and the code to pull the information from the form…

$id = $_POST[‘id’];

This would make $id = to what ever was put into that field. So this would simply keep the information from being lost. $id would equal the same thing on both pages.

I am sure if this helps you with your situation, but i was just pointing out that yes php can use hidden fields.

I apreciate the help but find it hard to believe that there is so much overhead needed to simply pass a html input value or JavaScript variable value to a php function variable. Passing a value into a function and getting a return is one of the most bsic functions in programming.

Still looking for ideas on how to do this.

i guess u are lookin at it from th wrong side.

PHP is a serverside script outputting html or even JavaScript.

Any variable that will be used in JavaScript later should be known by PHP long before.

go all the steps back. e.g. where does imgCt1 come from and try to get the calculation inside PHP.
[php]function bannerurl($currentAd2) {

}

$currentAd2=rand(0,$imgCt1); [/php]

<script> var banner2 = document.getElementById('adBanner2'); banner2.src=<?php bannerurl($currentAd2); ?> </script>
if imgCt1 was set in JavaScript it should be posible to set it in PHP as well.

maybe if there is no interaction even:
[php]
function bannerurl($currentAd2) {

}

echo ‘’;[/php]
and no JavaScript

if u think this way:
call of a php side -> PHP outputs the file -> On the client the JavaScript is executed (no PHP available anymore) Just use it for interactions the are done before reqesting the next page -> submit a form / click a link -> here it starts all over again

You are correct, I was stuck thinking in client side scripting mode. I moved the random function over to php and let it go at that.

Thx for the help.

Sponsor our Newsletter | Privacy Policy | Terms of Service