从AJAX发送到PHP服务器的未定义变量

时间:2016-03-09 09:10:59

标签: javascript php jquery mysql ajax

我有这种形式,它应该将值发送到AJAX然后发送到PHP并执行一些查询,最后,我们应该从ajax获得另一个响应。

我的HTML表单如下:我们为患者添加有关牙医项目的信息:

        <form>
    <div class="box-body" id="toggleDiv">
      <div class="row">
        <div class="col-md-6">
          <div class="form-group">
            <label style="float:left">Starting date</label>
            <input type="date" class="form-control" id="date_now" name="date_now"/>
            <label style="float:left">Project Description</label>
            <input type="text" class="form-control" id="project_desc" name="project_desc"/>
            <label style="float:left">Project Cost</label>
            <input type="text" class="form-control" id="project_cost" name="project_cost"/>
            <label style="float:left">Payment Type</label>
            <select class="form-control select2" id="pay_type" name="pay_type">
                <option value="0">Choose</option>
                <option value="cash">Cash</option>
                <option value="installment">Instalment</option>
            </select>
            <label style="float:left;">First Payment (In case of installment)</label>
            <input type="text" class="form-control" id="first_pay" name="first_pay"/>
            <label style="float:left">Date of Payment</label>
            <input type="date" class="form-control" id="date_pay" name="date_pay"/>
          </div><!-- /.form-group -->

        </div><!-- /.col -->
        <div class="col-md-6">
          <div class="form-group">
            <label style="float:left">Inssurance Type (Gov, Social, ...)</label>
            <input type="text" class="form-control" id="inssurance" name="inssurance"/>
            <label style="float:left">Prescription</label>
            <input type="text" class="form-control" id="prescription" name="prescription"/>

              <div class="box box-danger">

                <div class="box-body">
                  <!-- Date dd/mm/yyyy -->
                  <div class="form-group">
                    <div class="input-group">
                      <div class="input-group-addon">
                        <i class="fa fa-check"></i>
                      </div>
                      <input type="text" class="form-control" id="ext" name="ext" placeholder="ext"/>
                      <input type="text" class="form-control" id="do" name="do" placeholder="do"/>
                      <input type="text" class="form-control" id="endo" name="endo" placeholder="endo"/>
                      <input type="text" class="form-control" id="fm" name="fm" placeholder="fm"/>
                      <input type="text" class="form-control" id="crown" name="crown" placeholder="crown"/>
                      <input type="text" class="form-control" id="rmvbl" name="rmvbl" placeholder="rmvbl"/>
                      <input type="text" class="form-control" id="paro" name="paro" placeholder="paro"/>
                    </div><!-- /.input group -->
                </div><!-- /.form group -->
            </div>
          </div>
          </div><!-- /.form-group -->
          <div class="form-group">

          </div><!-- /.form-group -->
        </div><!-- /.col -->

      </div><!-- /.row -->
    </div><!-- /.box-body -->

  <div class="row">
    <div class="col-md-6">

      <div class="box box-danger">
        <div class="box-header">
          <h3 class="box-title">Upper Jaw</h3>
        </div>
        <div class="box-body">
          <!-- IP mask -->
          <div class="form-group col-md-6">
            <label>Upper Right Teeth</label>
            <div class="input-group">
                <div class="input-group-addon">
                    <i><img src="../images/molar.png" style="width:15px; height:15px"/>Upper Right</i>
                </div>
                <input type="text" class="form-control" id="one" name="one" placeholder="3rd Molar"/>
                <input type="text" class="form-control" id="two" name="two" placeholder="2nd Molar"/>
                <input type="text" class="form-control" id="three" name="three" placeholder="1st Molar"/>
                <input type="text" class="form-control" id="four" name="four" placeholder="2nd Bicuspid"/>
                <input type="text" class="form-control" id="five" name="five" placeholder="1st Bicuspid"/>
                <input type="text" class="form-control" id="six" name="six" placeholder="cuspid"/>
                <input type="text" class="form-control" id="seven" name="seven" placeholder="lateral"/>
                <input type="text" class="form-control" id="eight" name="eight" placeholder="central"/>
            </div><!-- /.input group -->
          </div><!-- /.form group -->
          <div class="form-group col-md-6">
            <label>Upper Left Teeth</label>
            <div class="input-group">
                <input type="text" class="form-control" id="nine" name="nine" placeholder="3rd Molar"/>
                <input type="text" class="form-control" id="ten" name="ten" placeholder="2nd Molar"/>
                <input type="text" class="form-control" id="eleven" name="eleven" placeholder="1st Molar"/>
                <input type="text" class="form-control" id="twelve" name="twelve" placeholder="2nd Bicuspid"/>
                <input type="text" class="form-control" id="thirteen" name="thirteen" placeholder="1st Bicuspid"/>
                <input type="text" class="form-control" id="fourteen" name="fourteen" placeholder="cuspid"/>
                <input type="text" class="form-control" id="fifteen" name="fifteen" placeholder="lateral"/>
                <input type="text" class="form-control" id="sixteen" name="sixteen" placeholder="central"/>
                <div class="input-group-addon">
                    <i><img src="../images/molar.png" style="width:15px; height:15px"/>Upper Left</i>
                </div>
            </div><!-- /.input group -->
          </div><!-- /.form group -->
        </div><!-- /.box-body -->
      </div><!-- /.box -->

    </div><!-- /.col (left) -->
    <div class="col-md-6">
      <div class="box box-primary">
        <div class="box-header">
          <h3 class="box-title">Lower Jaw</h3>
        </div>
        <div class="box-body">
          <!-- IP mask -->
          <div class="form-group col-md-6">
            <label>Lower Right Teeth</label>
            <div class="input-group">
                <div class="input-group-addon">
                    <i><img src="../images/molar.png" style="width:15px; height:15px"/>Lower Right</i>
                </div>
                <input type="text" class="form-control" id="lone" name="lone" placeholder="3rd Molar"/>
                <input type="text" class="form-control" id="ltwo" name="ltwo" placeholder="2nd Molar"/>
                <input type="text" class="form-control" id="lthree" name="lthree" placeholder="1st Molar"/>
                <input type="text" class="form-control" id="lfour" name="lfour" placeholder="2nd Bicuspid"/>
                <input type="text" class="form-control" id="lfive" name="lfive" placeholder="1st Bicuspid"/>
                <input type="text" class="form-control" id="lsix" name="lsix" placeholder="cuspid"/>
                <input type="text" class="form-control" id="lseven" name="lseven" placeholder="lateral"/>
                <input type="text" class="form-control" id="leight" name="leight" placeholder="central"/>
            </div><!-- /.input group -->
          </div><!-- /.form group -->
          <div class="form-group col-md-6">
            <label>Lower Left Teeth</label>
            <div class="input-group">
                <input type="text" class="form-control" id="lnine" name="lnine" placeholder="3rd Molar"/>
                <input type="text" class="form-control" id="lten" name="lten" placeholder="2nd Molar"/>
                <input type="text" class="form-control" id="leleven" name="leleven" placeholder="1st Molar"/>
                <input type="text" class="form-control" id="ltwelve" name="ltwelve" placeholder="2nd Bicuspid"/>
                <input type="text" class="form-control" id="lthirteen" name="lthirteen" placeholder="1st Bicuspid"/>
                <input type="text" class="form-control" id="lfourteen" name="lfourteen" placeholder="cuspid"/>
                <input type="text" class="form-control" id="lfifteen" name="lfifteen" placeholder="lateral"/>
                <input type="text" class="form-control" id="lsixteen" name="lsixteen" placeholder="central"/>
                <div class="input-group-addon">
                    <i><img src="../images/molar.png" style="width:15px; height:15px"/>Lower Left</i>
                </div>
            </div><!-- /.input group -->
          </div><!-- /.form group -->
        </div><!-- /.box-body -->
      </div><!-- /.box -->
      <button type="button" class="btn btn-danger" id="add_project">Add</button>

      <!-- iCheck -->

    </div><!-- /.col (right) -->
  </div><!-- /.row -->

这是我的AJAX脚本:

$("#add_project").click(function()
{
    //Patient ID
    var pid = '<?php echo $patient_id ?>';
    console.log(pid);
    //variables
    var pay_type = $("#pay_type").val();
    console.log(pay_type);
    var project_cost = $("#project_cost").val();
    var project_desc = $("#project_desc").val();
    var date_now = $("#date_now").val();
    var first_pay = $("#first_pay").val();
    var date_pay = $("#date_pay").val();
    console.log(date_pay);
    var inssurance = $("#inssurance").val();
    var prescription = $("#prescription").val();

    var ext = $("#ext").val();
    var doo = $("#do").val();
    console.log(doo);
    var endo = $("#endo").val();
    var fm = $("#fm").val();
    var crown = $("#crown").val();
    var rmvbl = $("#rmvbl").val();
    var paro = $("#paro").val();

    //teeth
    var ur_t_molar = $("#one").val();
    var ur_s_molar = $("#two").val();
    var ur_f_molar = $("#three").val();
    var ur_s_bicuspid = $("#four").val();
    var ur_f_bicuspid = $("#five").val();
    var ur_bicuspid = $("#six").val();
    var ur_lateral = $("#seven").val();
    var ur_central = $("#eight").val();

    var ul_t_molar = $("#nine").val();
    var ul_s_molar = $("#ten").val();
    var ul_f_molar = $("#eleven").val();
    var ul_s_bicuspid = $("#twelve").val();
    var ul_f_bicuspid = $("#thirteen").val();
    var ul_bicuspid = $("#fourteen").val();
    var ul_lateral = $("#fifteen").val();
    var ul_central = $("#sixteen").val();
    console.log(ul_central);
    var lr_t_molar = $("#lone").val();
    var lr_s_molar = $("#ltwo").val();
    var lr_f_molar = $("#lthree").val();
    var lr_s_bicuspid = $("#lfour").val();
    var lr_f_bicuspid = $("#lfive").val();
    var lr_bicuspid = $("#lsix").val();
    var lr_lateral = $("#lseven").val();
    var lr_central = $("#leight").val();

    var ll_t_molar = $("#lnine").val();
    var ll_s_molar = $("#lten").val();
    var ll_f_molar = $("#leleven").val();
    var ll_s_bicuspid = $("#ltwelve").val();
    var ll_f_bicuspid = $("#lthirteen").val();
    var ll_bicuspid = $("#lfourteen").val();
    var ll_lateral = $("#lfifteen").val();
    var ll_central = $("#lsixteen").val();

    //Conditions before sending data through ajax into php code
    if(project_cost == "" || pay_type == 0 || project_desc == "" || date_pay == "")
    {
        alert("Please add a project description, with cost and type of payment!");
        $("#project_cost").css('border-color', 'red');
        $("#pay_type").css('border-color', 'red');
        $("#date_pay").css('border-color', 'red');
        $("#project_desc").css('border-color', 'red');
    }
    if(project_cost != "" && pay_type != 0 && project_desc != "" && date_pay != "")
    {
        //Ajax script starts here
        $.ajax
        ({
            url: 'add_project.php',
            type: 'post',
            date: {patient_id: pid, payment_type: pay_type, project_c: project_cost, proj_d: project_desc, 
                dn: date_now, fp: first_pay, date_p: date_pay, 
                ins: inssurance, presc: prescription, e: ext, 
                d: doo, en: endo, f: fm, 
                c: crown, r: rmvbl, p: paro,
                one: ur_t_molar, two: ur_s_molar, three: ur_f_molar, 
                four: ur_s_bicuspid, five: ur_f_bicuspid, six: ur_bicuspid, 
                seven: ur_lateral, eight: ur_central, nine: ul_t_molar, ten: ul_s_molar,
                eleven: ul_f_molar, twelve: ul_s_bicuspid, thirteen: ul_f_bicuspid,
                fourteen: ul_bicuspid, fifteen: ul_lateral, sixteen: ul_central,
                lone: lr_t_molar, ltwo: lr_s_molar, lthree: lr_f_molar, 
                lfour: lr_s_bicuspid, lfive: lr_f_bicuspid, lsix: lr_bicuspid, 
                lseven: lr_lateral, leight: lr_central, lnine: ll_t_molar, lten: ll_s_molar,
                leleven: ll_f_molar, ltwelve: ll_s_bicuspid, lthirteen: ll_f_bicuspid,
                lfourteen: ll_bicuspid, lfifteen: ll_lateral, lsixteen: ll_central},
            dataType: 'text',

            success:function(res)
            {
                if(res=="added")
                {
                    alert("information added");
                }
            },
            error:function(res)
            {
                if(res!="added"){
                    alert("Their is some errors in data, please try again!");
                }
            }
        });
    }
});

现在,当我点击添加按钮时,我可以看到一些使用控制台的信息(如pid,ext,...),但是当执行php代码时,我得到每个变量的错误说:

  

注意:未定义的索引:my_variable在...

这是我的控制台输出:

enter image description here

这是我的PHP代码,我使用$_POST['']得到每个值:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

require_once('../include/global.php');
//session_start();
$user = $_SESSION['username'];
$id_logged = $_SESSION['login_id'];
//Getting Patient ID
$patient_id = $_POST['patient_id'];

//Getting other variables
$payment_type = $_POST['payment_type'];
$project_c = $_POST['project_c'];
$proj_d = $_POST['proj_d'];
$dn = $_POST['dn'];
$fp = $_POST['fp'];
$date_p = $_POST['date_p'];
$ins = $_POST['ins'];
$presc = $_POST['presc'];
$e = $_POST['e'];
$d = $_POST['p'];
$en = $_POST['en'];
$f = $_POST['f'];
$c = $_POST['c'];
$r = $_POST['r'];
$p = $_POST['p'];
$one = $_POST['one'];
$two = $_POST['two'];
$three =$_POST['three'];
$four = $_POST['four'];
$five =$_POST['five'];
$six = $_POST['six'];
$seven = $_POST['seven'];
$eight =$_POST['eight'];
$nine =$_POST['nine'];
$ten =$_POST['ten'];
$eleven = $_POST['eleven'];
$twelve = $_POST['twelve'];
$thirteen = $_POST['thirteen'];
$fourteen = $_POST['fourteen'];
$fifteen = $_POST['fifteen'];
$sixteen =$_POST['sixteen'];
$lone = $_POST['lone'];
$ltwo = $_POST['ltwo'];
$lthree =$_POST['lthree'];
$lfour = $_POST['lfour'];
$lfive =$_POST['lfive'];
$lsix = $_POST['lsix'];
$lseven = $_POST['lseven'];
$leight =$_POST['leight'];
$lnine =$_POST['lnine'];
$lten =$_POST['lten'];
$leleven = $_POST['leleven'];
$ltwelve = $_POST['ltwelve'];
$lthirteen = $_POST['lthirteen'];
$lfourteen = $_POST['lfourteen'];
$lfifteen = $_POST['lfifteen'];
$lsixteen =$_POST['lsixteen'];

if($payment_type == "cash")
{
    try
    {
        //Insert into patient Info
        $sqlInfo = "INSERT INTO patient_info(id_logged, patient_id, insurrance_type, project, project_cost,
                    payment_type, prescription, date_now, time_now, ext, do, endo, fm, crown, rmvbl, paro)
                    VALUES(:id_logged, :patient_id, :ins, :proj_d, :project_c, :payment_type, :presc,
                        :date_p, now(), :e, :d, :en, :f, :c, :r, :p)";
        $sqlInfoStmt = $conn->prepare($sqlInfo);
        $sqlInfoStmt->bindValue(":id_logged", $id_logged);
        $sqlInfoStmt->bindValue(":patient_id", $patient_id);
        $sqlInfoStmt->bindValue(":ins", $ins);
        $sqlInfoStmt->bindValue(":proj_d", $proj_d);
        $sqlInfoStmt->bindValue(":project_c", $project_c);
        $sqlInfoStmt->bindValue(":payment_type", $payment_type);
        $sqlInfoStmt->bindValue(":presc", $presc);
        $sqlInfoStmt->bindValue(":date_p", $date_p);
        $sqlInfoStmt->bindValue(":e", $e);
        $sqlInfoStmt->bindValue(":d", $d);
        $sqlInfoStmt->bindValue(":en", $en);
        $sqlInfoStmt->bindValue(":f", $f);
        $sqlInfoStmt->bindValue(":c", $c);
        $sqlInfoStmt->bindValue(":r", $e);
        $sqlInfoStmt->bindValue(":p", $p);

        $sqlInfoStmt->execute();

        //Insert into teeth table
        $sqlTeeth = "INSERT INTO teeth (id_logged, patient_id, one, two, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eightteen, nineteen, twenty, twone, twtwo, twthree, twfour, twfive, twsix, twseven, tweight, twnine, thirty, thone, thtwo, date_now) VALUES (:id_logged, :patient_id, :one, :two, :three, :four, :five, :six, :seven, :eight, :nine, :ten, :eleven, :twelve, :thirteen,
            :fourteen, :fifteen, :sixteen, :lone, :ltwo, :lthree, :lfour, :lfive, :lsix, :lseven, :leight, :lnine, :lten, :leleven, :ltwelve, :lthirteen,
            :lfourteen, :lfifteen, :lsixteen)";
        $sqlTeethStmt = $conn->prepare($sqlTeeth);
        $sqlTeethStmt->bindValue(":id_logged", $id_logged);
        $sqlTeethStmt->bindValue(":patient_id", $patient_id);
        $sqlTeethStmt->bindValue(":one", $one);
        $sqlTeethStmt->bindValue(":two", $two);
        $sqlTeethStmt->bindValue(":three", $three);
        $sqlTeethStmt->bindValue(":four", $four);
        $sqlTeethStmt->bindValue(":five", $five);
        $sqlTeethStmt->bindValue(":six", $six);
        $sqlTeethStmt->bindValue(":seven", $seven);
        $sqlTeethStmt->bindValue(":eight", $eight);
        $sqlTeethStmt->bindValue(":nine", $nine);
        $sqlTeethStmt->bindValue(":ten", $ten);
        $sqlTeethStmt->bindValue(":eleven", $eleven);
        $sqlTeethStmt->bindValue(":twelve", $twelve);
        $sqlTeethStmt->bindValue(":thirteen", $thirteen);
        $sqlTeethStmt->bindValue(":fourteen", $fourteen);
        $sqlTeethStmt->bindValue(":fifteen", $fifteen);
        $sqlTeethStmt->bindValue(":sixteen", $sixteen);

        $sqlTeethStmt->bindValue(":lone", $lone);
        $sqlTeethStmt->bindValue(":ltwo", $ltwo);
        $sqlTeethStmt->bindValue(":lthree", $lthree);
        $sqlTeethStmt->bindValue(":lfour", $lfour);
        $sqlTeethStmt->bindValue(":lfive", $lfive);
        $sqlTeethStmt->bindValue(":lsix", $lsix);
        $sqlTeethStmt->bindValue(":lseven", $lseven);
        $sqlTeethStmt->bindValue(":leight", $leight);
        $sqlTeethStmt->bindValue(":lnine", $lnine);
        $sqlTeethStmt->bindValue(":lten", $lten);
        $sqlTeethStmt->bindValue(":leleven", $leleven);
        $sqlTeethStmt->bindValue(":ltwelve", $ltwelve);
        $sqlTeethStmt->bindValue(":lthirteen", $lthirteen);
        $sqlTeethStmt->bindValue(":lfourteen", $lfourteen);
        $sqlTeethStmt->bindValue(":lfifteen", $lfifteen);
        $sqlTeethStmt->bindValue(":lsixteen", $lsixteen);

        $sqlTeethStmt->execute();

        echo "added";
    }
    catch(PDOException $msg)
    {
        $msg->getMessage();
        echo "not added";
    }
}

if($payment_type == "debt")
{

}
?>

在控制台中查看值意味着问题仅出在服务器端?。我希望我能得到一些帮助。

0 个答案:

没有答案