简体   繁体   English

PHP将HTML输入保存到数据库

[英]php saving html input to database

I have made an application where the user inputs their name and lastname which is then saved to the database 'register' in the 'user' table once they click on 'include'. 我制作了一个应用程序,用户在其中输入他们的姓名和姓氏,然后单击“包括”,然后将其保存到“用户”表中的数据库“注册”中。 I am completely stuck on this and can't seem to get the data saved to the database. 我完全陷入困境,似乎无法将数据保存到数据库中。 I have added my HTML, JS and PHP code below. 我在下面添加了我的HTML,JS和PHP代码。 Can anyone help me solve what's wrong? 谁能帮我解决问题?

function.js: 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: 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: 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>

I think this is where the issue lies 我认为这就是问题所在

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

it should be 它应该是

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

according to the ajax request parameters 根据ajax请求参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM