I have a form with a select field and a div i wish to update with a value depending on what the user selects.
Example:
Try something like that :
<select id="choose">
<option value="test1">Test1</option>
<option value="test2">Test2</option>
<option value="test3">Test3</option>
</select>
<div id="update"></div>
<script type="text/javascript">
$('#choose').change(function(event) {
$('#update').html('This is ' + $('#choose').val() + ' and other info');
});
</script>
If you want to make it with AJAX, change the javascript function to something like that:
<script type="text/javascript">
$('#choose').change(function(event) {
$.post('info.php', { selected: $('#choose').val() },
function(data) {
$('#update').html(data);
}
);
});
</script>
And in your info.php, you'll have something like:
<?php
$selected = isset($_POST['selected']) ? $_POST['selected'] : 'nothing';
echo("This is $selected and other info");
The general idea:
$(function() {
$("#msel").change(function(){
$("#myresult").html("This is " + $("#msel").val() + " and other info");
});
});
With more specifics I can do better. ;-)
<script type="text/javascript">
$(document).ready(function(){
$('#choose').change(function(event) {
$.post(
'info.php',
$(this).serialize(),
function(data){
$("#update").html(data)
}
);
return false;
});
});
<script type="text/javascript">
$(document).ajaxComplete(function(){
$('#choose').change(function(event) {
$.post(
'info.php',
$(this).serialize(),
function(data){
$("#update").html(data)
}
);
return false;
});
});
This is the simplest way I've found do it (from this handy forum)
<!-- the select -->
<select id="thechoices">
<option value="box1">Box 1</option>
<option value="box2">Box 2</option>
<option value="box3">Box 3</option>
</select>
<!-- the DIVs -->
<div id="boxes">
<div id="box1"><p>Box 1 stuff...</p></div>
<div id="box2"><p>Box 2 stuff...</p></div>
<div id="box3"><p>Box 3 stuff...</p></div>
</div>
<!-- the jQuery -->
<script type="text/javascript" src="path/to/jquery.js"></script>
<script type="text/javascript">
$("#thechoices").change(function(){
$("#" + this.value).show().siblings().hide();
});
$("#thechoices").change();
</script>