天天看点

jquery+ajax+php

index.html

```

league
 
 
 
 
 
    作品类型
 
 
    作品名称
 
 
    参加人员
 
 
    指导老师
 
 
    联系电话
 
 
    学校名称
 
 
    报名
 
 
$('#sign').click(function() {
    alert('sign');
    var prodType = $('#prodType').val();
    var prodName = $("#prodName").val();
    var member = $("#member").val();
    var teacher = $("#teacher").val();
    var telephone = $("#telephone").val();
    var school = $("#school").val();
    if (prodType == '' || prodName == '' || member == '' || teacher == '' || telephone == '' || school == '') {
         alert("所有数据均必填");
     }else{
   if (telephone.length == 11) {
        $.ajax({
            type: "POST",
            url: "testDb.php",
            dataType: "JSON",
            data: {
                "prodType": prodType,
                "prodName": prodName,
                "member": member,
                "teacher": teacher,
                "telephone": telephone,
                "school": school,
            },
            success: function(data) {
                switch (data) {
                    case 1: //用户已存在
                        alert("报名成功")
                        break;
                    case 0: //注册成功
                        alert("报名失败")
                        break;
                }
 
            }
        })
    } else {
        alert('请检查您的联系方式');
    }
  }     
})      

testDB.php

``` prepare("INSERT INTO league VALUES (?, ?, ?, ?,?,?)");

$insert->bind_param('ssssss', $prodType,$prodName,$member,$teacher,$telephone,$school); //防止sql注入 $insert->execute(); echo 1;

} catch (Exception $e) { echo 0; }