2

下面是一段 php/mysqli 代码,它在下拉菜单中显示课程列表:

    $courseactive = 1;

    $sql = "SELECT CourseId, CourseNo, CourseName FROM Course WHERE CourseActive = ? ORDER BY CourseNo"; 

    $sqlstmt=$mysqli->prepare($sql);

    $sqlstmt->bind_param("i",$courseactive);

    $sqlstmt->execute(); 

    $sqlstmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName);

    $courses = array(); // easier if you don't use generic names for data 

    $courseHTML = "";  
    $courseHTML .= '<select name="courses" id="coursesDrop">'.PHP_EOL; 
    $courseHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

    $outputcourse = ""; 
    $hiddencourse = ""; 

    while($sqlstmt->fetch()) 
    { 
    $course = $dbCourseId;
    $courseno = $dbCourseNo;
    $coursename = $dbCourseName; 
    $courseHTML .= "<option value='".$course."'>" . $courseno . " - " . $coursename . "</option>".PHP_EOL;  

    if (isset($_POST['courses']) && ($_POST['courses'] == $course)) {
    $outputcourse .= "<p><strong>Course:</strong> " . $courseno .  " - "  . $coursename . "</p>";
    $hiddencourse .= "<p><input type='hidden' id='hiddencourse' value='". $courseno . " - " . $coursename ."'></p>";
}

    } 

    $courseHTML .= '</select>'; 

现在,如果您查看<option>while 循环中的标记,我已经说明每个选项的每个值都是课程的 ID。我这样做的原因是因为我想使用该值来查找CourseId所选课程的值,然后使用下面的代码将其插入到数据库中,该代码位于单独的页面(insertmoduletocourse)上:

$courseid = (isset($_POST['courses'])) ? $_POST['courses'] : '';    

$insertsql = "
INSERT INTO Course_Module
(CourseId)
VALUES
(?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
// Handle errors with prepare operation here
}                                           

$insert->bind_param("i", $courseid);

$insert->execute();

if ($insert->errno) {
// Handle query error here
}

$insert->close();

但问题是,它没有在插入中插入 CourseId 编号,而是仅显示0,这意味着它无法从下拉选项的值中检索课程 ID。我的问题是,为什么它不能撤销它并因此继续插入 0?

我为$coursein做了一个回"<option value='".$course."'>"显,它显示了正确的课程 ID,所以我不知道为什么它无法为插入检索它?

我正在使用下面的这个 ajax 函数导航到insertmoduletocourse

function submitform() {    

    $.ajax({
        type: "POST",
        url: "insertmoduletocourse.php",
        data: $('#detailsForm').serialize(),
        success: function(html){
            $("#targetdiv").html(html);
            $('#targetdiv').show();

        }
     });        
}

下面是包含课程下拉菜单和说明从下拉菜单中选择的课程的字符串的表单:

$moduleForm = "<div class='lt-container'>
<form action='".htmlentities($_SERVER['PHP_SELF'])."' method='post' id='moduleForm'>
<p id='warnings'>{$pHTML}</p>
{$outputcourse}
{$hiddencourse}
<p><strong>Add Module</strong></p>
<p><strong>Module:</strong> {$moduleHTML} </p>   
</form>
</div>";

以下是包含模块详细信息的表单:

$addmodule = "
<div class='lt-container'>
<form id='detailsForm'>

    <p><strong>Module Details</strong></p>
    <table>
    <tr>
    <th></th>
    <td><input type='hidden' id='idmodule' name='moduleid' value='' /> </td>
    </tr>
    <tr>
    <th>Module ID:</th>
    <td><input type='text' id='nomodule' name='moduleno' readonly='readonly' value='' /> </td>
    </tr>
    <tr>
    <th>Module Name:</th>
    <td><input type='text' id='namemodule' name='modulename' readonly='readonly' value='' /> </td>
    </tr>
    <tr>
    <th>Credits:</th>
    <td><input type='text' id='credits' name='creditsname' readonly='readonly' value=''/> </td>
    </tr>
    </table>
    <div id='moduleAlert'></div>

    </form>

    <p id='addbtn'><button id='addSubmit'>Add Module</button></p>

    </div>
";
4

1 回答 1

1

您正在序列化表单$('#detailsForm').serialize(),而不是包含下拉菜单的表单,即moduleForm.

 $.ajax({
    type: "POST",
    url: "insertmoduletocourse.php",
    data: data: { detailsForm: $('#detailsForm').serialize(), moduleForm : $('#moduleForm').serialize() },
    success: function(html){
        $("#targetdiv").html(html);
        $('#targetdiv').show();

    }
 });      
于 2012-12-11T15:07:39.737 回答