开发者

PHP jQuery AJAX....reload a drop-down once data has been submitted to DB

开发者 https://www.devze.com 2023-04-01 12:26 出处:网络
I am new to PHP and I am trying to learn how to re-populate a dropdown once a new category has been added.

I am new to PHP and I am trying to learn how to re-populate a dropdown once a new category has been added.

Right now the user can create a new category and a message is sent back on success. I am lost on how to re-populate the drop-down on success.

Here is some related code:

//Handles the submit to DB
    $.post("addHourlyScheduleCB.php", {
            schedule: $("#schedule").val()
            },
        function(list){

        $("#message").removeClass().html(list);


        //inject the latest drop down info
        $("#scheduleSelect").load("scheduleSelect.php");


        $("html,body").animate({scrollTop:0},'slow');
        $.unblockUI()
            }
        );

On success I tried to inject the with a PHP page that pulls the updated data from the DB.

Here is the HTML

<select name="scheduleSelect" id="scheduleSelect">
        <?php 

        while ($row = $db->sql_fetchrow($rateScheduleSQLresult)) { 
        echo "<option  value=".$row['Rate_Schedule_ID'].">$row[schedule]</option>\n"; 
        } 
        ?>
        </select>

Here is the page that is called in the success function of the jQuery:

<?php 
require_once("models/config.php");
$rateScheduleSQL = "SELECT * FROM rateschedules ORDER BY schedule";
$rateScheduleSQLresult = $db->sql_query($rateScheduleSQL);

while ($row = $db->sql_fet开发者_运维百科chrow($rateScheduleSQLresult)) { 
echo "<option  value=".$row['Rate_Schedule_ID'].">$row[schedule]</option>\n"; 
} 
?>

*EDIT: The initial dropdown shows the expected results. It is one the success of the post that the dropdown shows no results. I believe this is an issue of how I am trying to update the dropdown. I believe there must be a much better way. *


Within your callback, try seeing what is actually returned by the AJAX call. Add console.log(list) to make sure something is returned (this will show in your developer tools/firebug console).

The .load() function is an event handler, it acts when the called-upon element (#scheduleSelect) is loaded, it doesnt populate it with any info.

You want to make another $.post call to get the data from scheduleSelect.php, and populate the dropdown with the callback variable. (try to do a console.log(variable) with the 2nd post)

0

精彩评论

暂无评论...
验证码 换一张
取 消