believeinsharing Posted June 14, 2012 Share Posted June 14, 2012 Is there any way to use javascript variable in php code? Quote Link to comment https://forums.phpfreaks.com/topic/264196-pass-javascript-variable-to-php/ Share on other sites More sharing options...
Mahngiel Posted June 14, 2012 Share Posted June 14, 2012 sorta, if you pass it's value to a php function via ajax. Quote Link to comment https://forums.phpfreaks.com/topic/264196-pass-javascript-variable-to-php/#findComment-1353938 Share on other sites More sharing options...
scootstah Posted June 14, 2012 Share Posted June 14, 2012 No. PHP has already been parsed and completed before any Javascript enters the equation. However, you can send data to a PHP script from Javascript with AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/264196-pass-javascript-variable-to-php/#findComment-1353939 Share on other sites More sharing options...
believeinsharing Posted June 14, 2012 Author Share Posted June 14, 2012 I am using select to display combo box with values from 1 to 5... I wanted to use that value for "for loop" to display no of text box... No of text boxes are depends upon what user has selected in select I am not sure is this purely done by php so what i did is I use javascript to get value n then wanted to use that in php for loop. <div id="divSubMenu" style="display:none"> No of submenu: <select id="noofSubmenu" onchange="printMe()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> javascript code is <script type="text/javascript"> function printMe() { var noOfSubmenu =document.getElementById('noofSubmenu').value; //alert ("Selected option is " + noOfSubmenu); } </script> PHP code: <?php echo "No of submenus are:".noOfSubmenu ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/264196-pass-javascript-variable-to-php/#findComment-1353955 Share on other sites More sharing options...
RobertP Posted June 14, 2012 Share Posted June 14, 2012 You can use jQuery.post to send information from javascript to php, and use jQuery.getJSON to send data from php to javascript. Quote Link to comment https://forums.phpfreaks.com/topic/264196-pass-javascript-variable-to-php/#findComment-1353960 Share on other sites More sharing options...
scootstah Posted June 14, 2012 Share Posted June 14, 2012 and use jQuery.getJSON to send data from php to javascript. You can actually throw PHP variables right into the Javascript source. <?php $foo = 'bar'; ?> <script type="text/javascript"> alert("<?php echo $foo; ?>"); </script> Quote Link to comment https://forums.phpfreaks.com/topic/264196-pass-javascript-variable-to-php/#findComment-1353962 Share on other sites More sharing options...
Mahngiel Posted June 14, 2012 Share Posted June 14, 2012 This plugin for jQ can simplify what you're trying to do. Here's something i did with it: // Query info $cats = $this->games->get_cats(); $subs = $this->games->get_subs(); // Create category dropdowns <?php if($cats):?> <select id="cat" name="category_id"> <option value="">Game Genre</option> <?php foreach($cats as $cat):?> <option value="<?php echo $cat->category_id;?>"><?php echo $cat->category_title; ?></option> <?php endforeach; ?> </select> <?php endif; ?> <?php if($subs):?> <select id="sub" name="sub_cat_id"> <option value="">Sub Genre</option> <?php foreach($subs as $sub):?> <option value="<?php echo $sub->sub_cat_id;?>" class="<?php echo $sub->setting_id;?>"><?php echo $sub->sub_cat_name; ?></option> <?php endforeach; ?> </select> <?php endif; ?> // JS code <script type="text/javascript" > $("#sub").chained("#cat"); </script> Quote Link to comment https://forums.phpfreaks.com/topic/264196-pass-javascript-variable-to-php/#findComment-1353969 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.