Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.