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 ;

?>

 

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.