Jump to content

onchange function help


brooksh

Recommended Posts

This script works just fine, but when I submit the data, and it comes back, how do I fool the javascript to getmodels? The script also uses jQuery 1.2.6

 

<script type="text/javascript" charset="utf-8">

/* 
//I need to create this
if($submit == "yes"){
$act = "getmodel";
//I know This needs to be changed $("select#make").change(function(){
}
*/


$(function(){
  $("select#make").change(function(){
    $.getJSON("data.php",{act: 'getmodel', make: $(this).val()}, function(j){
      var options = '';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      } 
      $("select#model").html(options);
    })
  })
})
</script>
<form method="post" name="form">
<input type="hidden" name="submit" value="yes">
<select name="mymake" id="make">
		 <option>Select One</option>
		 <?php do {  ?>
		 <option value="<?= $row_make['make']; ?>"> <?$row_make['make']; ?></option>
		 <?php } while ($row_make = mysql_fetch_assoc($make)); ?>
</select>
       </select>
<select name="mymodel" id="model">
       </select>

Link to comment
Share on other sites

Maybe I didn't explain this well. And for some reason not all of the script was submitted. This script is a dynamic dropdown script. If I choose the make , the script finds the model (getmodel) in my database and displays it in another dropdown. The problem is I want it to work onload (if submitted), not just onchange.

 

 

index.php

<script type="text/javascript" charset="utf-8">

$(function(){
  $("select#make").change(function(){
<?if($mymake != ""){ $act="getmodel"; } ?>
    $.getJSON("data.php",{act: 'getmodel', make: $(this).val()}, function(j){
      var options = '';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      } 
      $("select#model").html(options);
    })
  })
})
</script>

<form name="form1" method="POST" action="demo.php">
<select name="mymake" id="make">
		 <option>Select One</option>
		 <?php do {  ?>
		 <option value="<?= $row_make['make']; ?>" <? if($mymake == $row_make['make']){echo "selected";} ;?>><?= $row_make['make']; ?></option>
		 <?php } while ($row_make = mysql_fetch_assoc($make)); ?>
</select>

       </select>
<select name="mymodel" id="model">
       </select>
<input type="submit" value="submit">
</form>

 

data.php

if ($_GET['act']=='getmodel') { 
$query_model = sprintf("SELECT model,ID FROM car_database WHERE make = '%s' ORDER BY model ASC", $_GET['make']);
$model = mysql_query($query_model, $db) or die(mysql_error() ." " .$query_model);
$row_model = mysql_fetch_assoc($model);
//$totalRows_groups = mysql_num_rows($groups);
do {
	$options[] = "{optionValue:'".$row_model['model']."', optionDisplay: '".htmlentities(trim($row_model['model'])) ."'}";
} while ($row_model = mysql_fetch_assoc($model));

//	print_r($options);
$theOptions = implode(",", $options);
$theOptions = "[".$theOptions."]";

echo $theOptions;

}

Link to comment
Share on other sites

I see you're using jQuery it's better to use the domready functionality then rather then a onload event.

with php you check if the form is send

<script>
<?php
if(isset($_POST['submit'])):
?>
//domready check
$(document).ready(function(){
    //call getmodel
    getmodel();
});
<?php
endif;
?>
</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.