简体   繁体   中英

JQUERY to auto-populate a select input from another select input

EDIT 1

I am getting courseID from this code:

    $coursesOutput = '<option value=""></option>';

    while($row = mysql_fetch_array($result2)){
        $courseID = $row['courseID'];
        $courseName = $row['name'];

        $coursesOutput .= '<option value="' . $courseID . '">' . $courseName . '</option>'; 
    }

My php script is as follows (returning an echo statement)

    <?php

    include ("includes/connect.php");

    $courseID = mysql_real_escape_string($_GET['courseID']);
    $sql = "SELECT tee1, tee2, tee3, tee4, tee5 FROM courses WHERE courseID='$courseID' LIMIT 1";
    $result = mysql_query($sql) or die(mysql_error());

    while($row = mysql_fetch_array($result)){
  $tee1 = $row['tee1'];
  $tee2 = $row['tee2'];
  $tee3 = $row['tee3'];
  $tee4 = $row['tee4'];
  $tee5 = $row['tee5'];
    }

    $teesOutput = '<option value="' . $tee1 . '">' . $tee1 . '</option>';

    if($tee2 != ""){
  $teesOutput .= '<option value="' . $tee2 . '">' . $tee2 . '</option>';
    }
    if($tee3 != ""){
   $teesOutput .= '<option value="' . $tee3 . '">' . $tee3 . '</option>';
    }
    if($tee4 != ""){
  $teesOutput .= '<option value="' . $tee4 . '">' . $tee4 . '</option>';
    }
    if($tee5 != ""){
  $teesOutput .= '<option value="' . $tee5 . '">' . $tee5 . '</option>';
    }

    echo '' . $teesOutput . '';
    die();
    ?>

I am not getting any ajax errors but still nothing populating in my tee selector. Hope this helps, once again I am overwhelmed by the support here, unbelieveable!

End EDIT 1

I have been unable to figure this AJAX-JQUERY feature out for awhile now. It should be an easy spot for good jQuery programmers.

I want to be able to auto-populate my tee select input after a user selects their course. This is a golf app and courses have several different tee color schemes so each will be course specific.

So far my broken codes are:

HTML

<form id="formAddScore" action="addscore.php" enctype="multipart/form-data" method="post">
    <p class="profile_label">Select Date:</p>
    <input type="text" id="datepicker" name="datepicker" class="score_input" />

    <p class="profile_label">Select Course:</p>
    <select id="course" name="course" class="score_input" onchange="populateTee(this.value)">
        ' . $coursesOutput . '
    </select>

    <p class="profile_label">Select Tee:</p>
    <select id="tee" name="tee" class="score_input">

    </select>

    <p class="profile_label">Actual Score:</p>
    <input id="score" name="score" class="score_input" type="text" size="10" />

    <p class="profile_label">Score ESC (Equitable Stroke Control):</p>
    <input id="scoreESC" name="scoreESC" class="score_input" type="text" size="10" />
    <br/>

    <input id="btnAddScore" name="btnAddScore" class="btn_score" type="submit" value="Add Score" />

</form>

JQUERY

function populateTee(courseID)
{
    $.ajax(
    {
        url: 'includes/populate_tee.php?courseID=' + courseID,
        success: function(data) {
        $("#tee").html(data);
               }
    });
}

PHP

populate_tee.php script WORKS, so I won't waste your time including it.

I am fairly certain the problem is in the above JQUERY-AJAX script.

Any help would be wonderful.

Thanks in advance.

Where does courseID come from? Try logging it into the console. Does this help?

function populateTee(courseID){
   $.ajax({
           url: 'includes/populate_tee.php?courseID=' + courseID,
           success: function(data) {
                    //Assuming you're returning the tees list as an indexed array
                    var ops = '';
                    for(var i=0; i<data.length; i++){
                        ops += '<option>'+data[i]+'</option>';
                    }
                    $("#tee").html(ops);
           }
   });
 }

Instead of using onChange event on select i suggest use of jquery .change() method. Try following code it should solve the problem.

 $('#course').on('change', function () {
          var courseID = $(this).val();

          $.ajax({
                type: "post",
                url: 'includes/populate_tee.php?courseID=' + courseID,
                success: function(data) {
                $("#tee").html(data);
               }
              });


      });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM