动态添加<select>元素并插入MySQL </select>

时间:2012-07-10 14:43:53

标签: mysql post select

我有这个jQuery代码来更新我的MySQL数据库:

$('#course_update').click(function() {

//Create the values from the elements as variables to be sent using jQuery POST
    var course_id = $('#course_id').val();
    var plan_id = $('#plan_id').val();
    var price_id = $('#price_id').val();
    var course_name = $('#course_name').val();
    var course_isActive = $('#course_isActive').val();
    var course_city_id = $('#course_city_id').val();

//Ajax loading image...
    $('#update_status').html('<img src="../images/ajax-loader.gif" />');
    $.post('../update.php', {

        course_id: course_id, 
        plan_id: plan_id,
        price_id: price_id,
        course_name: course_name,
        course_city_id: course_city_id,
        course_isActive: course_isActive

    }, function(data) {
        $('#update_status').html(data); //Display HTML result from ../update.php
    return false;
    });
});

plan_id在HTML中看起来像这样:

<select id="plan_id">
  <option value="1">First option</option>
  <option value="2">Second option</option>
  <option value="3">Third option</option>
</select>

(旁注,选项值是根据PHP循环中MySQL的值创建的)。

update.php文件会选择这样的值(我还没有验证输入数据)

<?php       
/* SQL STUFF */

if(isset($_POST['course_id'])) {

    $course_id = $_POST['course_id'];
    $plan_id = $_POST['plan_id'];
    $price_id = $_POST['price_id'];
    $course_name = $_POST['course_name'];
    $course_isActive = $_POST['course_isActive'];
    $course_city_id = $_POST['course_city_id'];

    $update_course = mysql_query("INSERT INTO course_to_plan (course_id, plan_id) VALUES ('$course_id', '$plan_id')"); //This is a linking table
    $update_course_price = mysql_query("UPDATE courses SET course_price_id = '$price_id', course_name = '$course_name', isActive = '$course_isActive', course_city_id = '$course_city_id' WHERE course_id = '$course_id'");

    if ($update_course === true) {
        echo "<img src='../images/action_ok.png' />";
    } else if ($update_course === false){
        echo "Could not update the database...";
    }
}
?>

现在我需要一个简单的添加按钮,以便能够多次添加<select id="plan_id">并将值作为某种数组发送到update.php然后INSERT INTO .. foreach已添加元素......

帮助我找到解决方案的人,我衷心感谢。我已经在网上搜索了几个星期(我是网络编程的新手)。

0 个答案:

没有答案