0

我有一个表单,它有 2 个下拉列表“Diplomas”和“Fields”、一个提交按钮和一个“添加”按钮,只要文本框的数量低于5 这是dipSecSelection.php包含在表单中的文件的代码:

<!----------- The dipSecSelection File ------------->
<div id="dipSecSelection">
    <table>
        <tr>
            <td><label for="diplomes">Diplome:</label></td>
            <td>
                <select name="Diplomes" id="Diplomes">
                    <option value="na">--Diplome--</option>
                    <option value="Technician">Technician</option>
                    <option value="Master">Master</option>
                    <option value="PhD">PhD</option>    
                </select>    
            </td>
            <td id="separator">|</td><!-- Separator -->
            <td><label for="secteurs">Secteur:</label></td>
            <td>
                <select name="Secteurs" id="Secteurs">
                    <option value="na">--Secteur--</option>
                    <option value="Software Dev">Software Dev</option>
                    <option value="Engineering">Engineering</option>
                    <option value="Physics">Physics</option>
                </select>
            </td>
            <td>
                <a href="#" Onclick="addDipSec()" value="Add" class="button">Add</a>
            </td>
            </tr>
    </table>
    <table>
        <tr>
            <td><div id='DipTextBoxesGroup'></div></td>
            <td><div id='SecTextBoxesGroup'></div></td>
        </tr>
    </table>
</div>

<script type="text/javascript">
    //Diplomas textBoxes!!!
    var counterdip = 1;
    var countersec = 1;
    function addDipSec(){
        if(counter>5){
            alert("Only 5 Diplomas allow");
            return false;
        }           
        var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counterdip);
        newTextBoxDiv.after().html('<label>Diplome #'+ counterdip + ' : </label>' +
            '<input type="text" name="dipBox[]" id="textboxdip' + counterdip + '" value="" >');
        newTextBoxDiv.appendTo("#DipTextBoxesGroup");
        $('#textboxdip'+counterdip).val($('#Diplomes option:selected').html());
        counter++;

        //Secteurs textBoxes
        if(countersec>5){
            alert("Only 5  Setors are allowed");
            return false;
        }  
        var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + countersec);
        newTextBoxDiv.after().html('<label>Secteur #'+ countersec + ' : </label>' +
              '<input type="text" name="secBox[]" id="textboxsec' + countersec + '" value="" size="35">');
        newTextBoxDiv.appendTo("#SecTextBoxesGroup");
        $('#textboxsec'+countersec).val($('#Secteurs option:selected').html());
        countersec++;
        alert(secBox[0].val);
}
</script>

这是php代码:

<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST" enctype="multipart/form-data">
    <?php include'includes/dipSecSelection.php'?>
    <?php
        if(isset($_POST['dipBox']) && isset($_POST['secBox'])){
            if(!empty($_POST['dipBox'])&&!empty($_POST['secBox'])){
                $dip= $_POST['dipBox'];
                $sec= $_POST['secBox'];
                $N = count($dip);
                for($i=0; $i < $N; $i++){
                    $add_AnnDipSec = "INSERT INTO annDipSec VALUES('$dip[$i]','$sec[$i]')";
                    if(mysqli_query($connection, $add_AnnDipSec)){
                        echo'Successfully Added to AnnDipSec';
                    }else{echo'Error while trying to insert into AnnDipSec';}
                }
            }else{echo"Dip and Sec were Empty";}
        }                            
    ?>
    <input type="submit" name="submit" value="Submit" class='button'>
</form>

问题是永远不会被设置dipBox[]并且secBox[]总是什么都不返回,所以它们永远不会被插入到数据库表中。

4

2 回答 2

0

option在您的选择器中丢失$('#Diplomes :selected')

试试这个方法——

$('#textboxdip'+counterdip).val($('#Diplomes option:selected').html());

这也是

$('#textboxsec'+countersec).val($('#Secteurs option:selected').html());
于 2013-04-14T20:49:38.390 回答
0

我刚刚解决了问题!当我把它拉出来的时候,我把表单标签放在第一个表里面,表单工作得很好

于 2013-04-20T12:26:01.710 回答