gpt4 book ai didi

php - jQuery/PHP POST 不工作

转载 作者:行者123 更新时间:2023-12-01 04:42:27 26 4
gpt4 key购买 nike

一切似乎都是正确的,但空白值继续被插入到我的数据库中。它似乎没有使用 $_POST 获取值。我也尝试过 $_GET 和 $_REQUEST ,但这些值似乎仍然没有被获取。我知道由于插入了静态“视频”文本,空白行被添加到数据库中。

HTML:

<form>
<div class="form-group">
<input type="text" class="form-control" name="firstName" id="firstName"
placeholder="First Name">
</div>
<div class="form-group">
<input type="text" class="form-control" name="lastName" id="lastName"
placeholder="Last Name">
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" id="email"
placeholder="Email">
</div>
<button type="submit" id="view" class="btn bds-button btn-block">View
</button>
</form>

JavaScript:

$(document).ready(function() {

$('form').validate({
rules: {
firstName: {
required: true
},
lastName: {
required: true
},
email: {
required: true,
email: true
}
},
messages: {
firstName: "Please enter your first name",
lastName: "Please enter your last name",
email: {
required: "Please enter your email",
email: "Please enter a valid email",
}
},
highlight: function(element) {
$(element).closest('.form-group').addClass('has-error');
},
unhighlight: function(element) {
$(element).closest('.form-group').removeClass('has-error');
},
errorElement: "span",
errorClass: 'help-block',
errorPlacement: function(error, element) {
if(element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
},
submitHandler: function(form) {

var firstName = $("#firstName").val();
var lastName = $("#lastName").val();
var email = $("#email").val();

$.ajax({
type: "POST",
url: "php/post-viewer.php",
data: {'firstName' : firstName, 'lastName' : lastName, 'email' : email},
dataType: 'json',
success : function(text){

}
});

}
});

});

PHP:

<?php

$servername = "XXXX";
$username = "XXXX";
$password = "XXXX";
$dbname = "XXXX";

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

$first_name = $_POST['firstName'];
$last_name = $_POST['lastName'];
$email = $_POST['email'];

$sql = "INSERT INTO viewers (demo, first_name, last_name, email)
VALUES ('Video', '$first_name', '$last_name', '$email')";

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

$conn->close();

?>

最佳答案

在ajax方法上,试试这个...

var vfirstName = $("#firstName").val();
var vlastName = $("#lastName").val();
var vemail = $("#email").val();

data: JSON.stringify({firstName : vfirstName, lastName : vlastName, email : vemail})

关于php - jQuery/PHP POST 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35040721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com