我一直在尝试解决这个关于jQuery,php和mySQL之间通信的问题。
长话短说,如果你愿意的话,我想通过PHP将信息插入我的数据库。
这是我的表格,在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>
当我按下Register按钮时,我想要触发这个,这是在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();
我可能从无数个小时试图解决这个问题搞砸了,但我真的希望你们中的一个人能够理解它并认识到错误。
感谢。
答案 0 :(得分:2)
通过评论说明变更: -
您的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 :(得分:1)
首先,您必须发布您的变量,以便您的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'])
希望这对你有所帮助,再见!
答案 2 :(得分:0)
现在你没有将任何表单数据传递给php文件。使用$('form')。serialize()并将所有数据传递给php文件。