brooksh Posted January 17, 2009 Share Posted January 17, 2009 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 https://forums.phpfreaks.com/topic/141180-onchange-function-help/ Share on other sites More sharing options...
RichardRotterdam Posted January 17, 2009 Share Posted January 17, 2009 This script works just fine, but when I submit the data, and it comes back, how do I fool the javascript to getmodels? What do you exactly mean with this? is getmodels a function? Link to comment https://forums.phpfreaks.com/topic/141180-onchange-function-help/#findComment-738986 Share on other sites More sharing options...
brooksh Posted January 17, 2009 Author Share Posted January 17, 2009 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 https://forums.phpfreaks.com/topic/141180-onchange-function-help/#findComment-739197 Share on other sites More sharing options...
RichardRotterdam Posted January 17, 2009 Share Posted January 17, 2009 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 https://forums.phpfreaks.com/topic/141180-onchange-function-help/#findComment-739207 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.