php将html输入保存到数据库中

时间:2016-03-21 16:30:57

标签: javascript php html ajax

我已经创建了一个应用程序,用户输入他们的名字和姓氏,然后将其保存到数据库' register'在'用户'表格一旦点击“包含”即可。我完全坚持这一点,似乎无法将数据保存到数据库中。我在下面添加了我的HTML,JS和PHP代码。任何人都可以帮我解决错误吗?

function.js:

$( document ).ready(function() {
    var $server;
    $server = 'http://localhost:81/xampp/';

    $('#include').on('click', function() {
        $name = $('#$name').val();
        $lastname = $('#lastname').val();

        $.ajax ({
            type: "get",
            url: $server-"/connectdb.php",
            data: "name="+$name+"&lastname="+$lastname+"&action=include",
            success: function(data) {
                intel.xdk.notification.alert('user recorded. ok!', 'Message', 'OK');

            }
        });
    });  

});

connectdb.php:

<?php
if(isset($_GET["include"])){
$servername = "localhost";
$username = "xxxx";
$password = "xxxx";
$dbname = "register";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO user (user_name, user_lastname)
VALUES ('".$_GET['name']."','".$_GET['lastname']."')";

if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}

$conn->close();
}
?>

的index.html:

       <link rel ="stylesheet" href="jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="jquery.mobile-1.4.5/jquery-2.1.1.min.js"></script>
    <script src="jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.js"></script>

    <script src="js/function.js"></script>
</head>

<body>

    <!-- page -->
    <div data-role="page" id="page1">

    <!-- header -->
    <div data-role="header">
    <h1>Records</h1>

    <!-- film header -->                        
    </div>

    <!-- main -->
        <div data-role="main" class="ui-content">

            <label>Name</label>
            <input id="name" type="text" name="u_name" /> <br>

            <label>Lastname</label>
            <input id="lastname" type="text" name="u_lastname" /> <br>

            <button id="include">Include</button>

            <HR>
                <a href="#page2" class="ui-btn ui-btn-corner-all"> View Entries</a>
        </div>
            <!-- film main -->    
    </div>

    <!-- film page -->
    <!-- page 2 -->
    <div data-role="page" id="page2">

             <!-- header 2 -->
             <div data-role="header">

                              <a href="#page1" data-rel="back" class="ui-btn-left ui-btn ui-btn-icon-notext ui-corner-all ui-icon-back">Back</a>


            <h1>User List</h1>
   </div>

   <!-- film header 2 -->
   <!-- main 2 -->
             <div data-role="main" class="ui-content">
                 <p id="userlist"></p>
             </div>
        </div>
        <!-- film page 2 -->
</body>
</html>

1 个答案:

答案 0 :(得分:1)

我认为这就是问题所在

if(isset($_GET["include"])){

应该是

if(isset($_GET["action"]) && $_GET["action"]=="include"){

根据ajax请求参数