Jump to content

PHP variables to Javascript


optikalefx

Recommended Posts

Iv seen a lot of topics about javascript to php but not the other way around

 

I run a sql query to get the number of rows in a table

and i want that variable to be able to be used in javascript

 

the javascript is this

<script language="text/javascript">

function formSubmit() {

var a;

a = <? echo $nrows; ?>;

alert(a);

document.frmupload.hdnupload.value=true;

document.frmupload.submit();

return true;

}

 

the reason it has to be in javascript is because an html form uses onClick="return formSubmit();"  so its not php anymore

well the function is in javascript but i want to bring in variable $nrows which has a number.

 

ill do the if statement after im able to see that javascript has the number.  any ideas on how to get javascript the php variable?

 

oviously the example i posted doesnt work.

 

and yes $nrows does exist because Iv echoed it and its there.

Link to comment
https://forums.phpfreaks.com/topic/63381-php-variables-to-javascript/
Share on other sites

that should work fine. I have been doing things exactly that way for a long time. Also, if you are already using short tags you could do away with the echo.

 

Like this:

<?=$a; ?>

 

for more info see alternative php syntax or http://codeigniter.com/user_guide/general/alternative_php.html

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.