未定义的索引:ajax POST出错

时间:2017-08-31 10:05:12

标签: javascript php ajax

我是php编程的新手,当我尝试使用ajax POST在我的桌面上添加记录时,我收到此错误 :未定义的索引:第6行的C:\ xampp \ htdocs \ app \ server.php 和其他变量一样

这是我的server.php页面:

 <?php
$dbh = new PDO('mysql:host=localhost;dbname=dbgestion', 'root', '');
$page = isset($_GET['p'])? $_GET['p'] : '';
if($page=='add'){
    try{
        $jour = $_POST['jour'];
        $ventes = $_POST['ventes'];
        $soldeinitial = $_POST['soldeinitial'];
        $stmt = $dbh->prepare("INSERT INTO jours VALUES(?,?,?)");
        $stmt->bindParam(1,$jour);
        $stmt->bindParam(2,$ventes);
        $stmt->bindParam(3,$soldeinitial);
        if($stmt->execute()){
            print "<div class='alert alert-success' role='alert'>Data has been added</div>";
        } else{
            print "<div class='alert alert-danger' role='alert'>Failed to add data</div>";
        }
    } catch(PDOException $e){
        print "Error!: " . $e->getMessage() . "<br/>";
    } 
}

在这里我的html页面:

<div class="row">
            <div class="col-md-9">
                <table class="table table-bordered table-striped table-hover">
                    <thead>
                        <tr>
                            <th width="40">Jour</th>
                            <th>Ventes</th>
                            <th>Solde Initial</th>
                            <th width="100">Action</th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>
            <div class="col-md-3">
                <form id="sidebar">
                    <div class="form-group">
                        <label for="jour">Jour</label>
                        <input type="date" id="jour" name="jour" class="form-control" placeholder="jour"/>
                    </div>
                    <div class="form-group">
                        <label for="ventes">Ventes</label>
                        <input type="text" id="ventes" name="ventes" class="form-control" placeholder="ventes"/>
                    </div>
                    <div class="form-group">
                        <label for="soldeinitial">Solde initial</label>
                        <input type="text" id="soldeinitial" name="soldeinitial" class="form-control" placeholder="soldeinitial"/>
                    </div>
                    <button type="button" id="save" class="btn btn-primary" onclick="saveData()">Save</button>
                    <button type="button" id="update" class="btn btn-warning" onclick="updateData()">Update</button>

                </form>
            </div>
        </div>

最后这是我的script.js文件

function saveData(){
    var jour = $('#jour').val()
    var ventes = $('#ventes').val()
    var soldeinitial = $('#soldeinitial').val()
       $.post('server.php?p=add', {jour:jour, ventes:ventes, soldeinitial:soldeinitial}, function(data){
            viewData()
            $('#jour').val(' ')
            $('#ventes').val(' ')
            $('#soldeinitial').val(' ')
        })
    }

0 个答案:

没有答案