Jump to content

pass javascript variable to php


Recommended Posts

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 ;

?>

 

 

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>

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.