jquery+ajax+php


index.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>league</title>
    <script src="http://upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.2.min.js"></script>
</head>

<body>
    <form>
        <label>作品类型</label>
        <input id="prodType" type="text" name="" />
        <br>
        <label>作品名称</label>
        <input id="prodName" type="text" name="" />
        <br>
        <label>参加人员</label>
        <input id="member" type="text" name="" />
        <br>
        <label>指导老师</label>
        <input id="teacher" type="text" name="" />
        <br>
        <label>联系电话</label>
        <input id="telephone" type="" name="" />
        <br>
        <label>学校名称</label>
        <input id="school" type="text" name="" />
        <br>
        <button type="button" id="sign">报名</button>
    </form>
    <script>
    $('#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('请检查您的联系方式');
        }
      }     
    })
    </script>
</body>

</html>

testDB.php

<?php
header("charset=UTF-8");

$prodType = $_POST['prodType'];
$prodName=$_POST['prodName'];
$member=$_POST['member'];
$teacher=$_POST['teacher'];
$telephone=$_POST['telephone'];
$school=$_POST['school'];


$servername = "localhost";
$username = "root";
$password = "root";
$db = "league";

// 创建连接
try {
    $conn = new mysqli($servername, $username, $password,$db);
    $insert = $conn->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;
}

声明:coder-syl|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - jquery+ajax+php


Carpe Diem and Do what I like