从表单中获取用户名、密码和电子邮件,并用它创建数据库行

Get username, password and email from a form and create database row with it

我一直在努力解决这个关于 jQuery、php 和 mySQL 之间通信的问题。

长话短说,如果您愿意,我想通过 PHP.

将我的 "registration form" 中的信息插入到我的数据库中

这是我的表格,在 login.php:

<form action="login.php" method="post">
    <input type="text" name="username" id="username">
<br><br>
    <input type="password" name="password">
<br><br>
    <input type="text" name="email">
<br><br>
    <input type="submit" value="Register" class="submitRegistration"/>
</form>

我希望在我按下“注册”按钮时触发它,它位于 head.php。

$(document).ready(function(){
    $(".submitRegistration").click(function(){
    var clickBtnValue = $(this).val();
    var username = $("#username").val();
    var password = $("#password").val();
    var email = $("#email").val();
    var ajaxurl = 'ajaxdisk.php';
    data = {'action': clickBtnValue};
    $.post(ajaxurl, data, function (response) {
        alert(response);
        });
    });

});

这是ajax.php中的数据库连接和我想要的功能运行

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "swag";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//I have suspicions that this is where things go wrong, because it checks if 
//$_POST['action'] != NULL before it is defined, I want it to 
//do this WHEN the button is hit, not when the page loads.
if (isset($_POST['action'])) {
    switch ($_POST['action']) {
        case 'Submit':
            submit();
            break;
        case 'Register':
            register($conn);
            break;
    }
}
else {
    print("Error");
}

function submit() {
    echo "The submit function is called.(unfinished)";
    exit;
}

function register($cpnn) {
    echo "The register function is called.";
    $sql = "INSERT INTO MyGuests (username, password, email)
    -- Here i want the values from the textboxes to be, so it gets put     into the database
    VALUES (username, password, email)";
    if ($cpnn->query($sql) === TRUE) {
        echo "New record created successfully";
    }   
    else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}


   $conn->close();

我可能花了无数个小时试图解决这个问题,但不知何故把它搞砸了,但我真的希望你们中的任何一个人都能理解它并认识到错误。

谢谢。

现在您没有将任何表单数据传递到 php 文件。使用 $('form').serialize() 并将所有数据传递到 php 文件。

更改由评论说明:-

您的 html 文件代码将如下所示:-

<form method="post">
    <input type="text" name="username" id="username">
<br><br>
    <input type="password" name="password" id="password"> <!-- id required -->
<br><br>
    <input type="text" name="email" id="email"> <!-- id required -->
<br><br>
    <input type="submit" value="Register" class="submitRegistration"/>
</form>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script type = "text/javascript">
$(document).ready(function(){
    $(".submitRegistration").click(function(e){// e for event
    e.preventDefault(); // to stop form submission
    var clickBtnValue = $(this).val();
    var username = $("#username").val();
    var password = $("#password").val();
    var email = $("#email").val();
    $.post('ajaxdisk.php', {'action': clickBtnValue,"username":username,"password":password,"email":email}, function (response) {
        alert(response);
        });
    });
});
</script>

您的 php 文件 (ajaxdisk.php) 代码将如下所示:-

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "swag";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['action'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];
    echo "The register function is called.";
    $sql = 'INSERT INTO `MyGuests` (username, password, email) VALUES ("'.(string)$username.'","'.(string)$password.'","'.(string)$email.'")';
    if ($conn->query($sql)) {
        echo "New record created successfully";
    }   
    else {
        echo "Error: ". $conn->error;
    }
}
?>

注意:- 问题出在您的 switch 代码中(我无法找出原因)。此外,您的值以字符串形式出现,但在查询中它不被视为字符串(这对我来说也是无法理解的,为什么会发生这种情况)

此代码肯定对您有用。检查一次。

我的实际代码:-

  1. http://prntscr.com/atqnpv

  2. http://prntscr.com/atqnye

  3. http://prntscr.com/atqob0

  4. http://prntscr.com/atqomw

首先你必须post你的变量所以你的api调用应该是这样的:

data = {'action': clickBtnValue, 'username': username, 'password': password, 'email': email};
$.post(ajaxurl, data, function (response) {
    alert(response);
    });
});

然后在您的 PHP 页面中,您应该能够从 $_POST 对象中检索数据。

您的 SQL 字符串应如下所示:

INSERT INTO MyGuests (username, password, email) VALUES ($_POST['username'], $_POST['password'], $_POST['email'])

希望对你有帮助,再见!