gpt4 book ai didi

javascript - 表单字段未存储在 mysql 数据库中

转载 作者:行者123 更新时间:2023-11-28 02:57:00 26 4
gpt4 key购买 nike

我创建了一个由文本区域字段组成的简单表单,因此当用户单击提交按钮时,它会链接到一个包含执行流程和存储数据的 URL 的 jquery 脚本,但问题是每次我点击提交时,ID & created_at 数据已存储,但 textarea 上给出的数据被忽略且未存储,以前从未遇到过这个问题..请帮助我!

HTML

<form id="form" name="form" method="POST" action="profile_1.php" class="wizard-big" autocomplete="off" enctype="multipart/form-data" required="">
<div class="form-group col-sm-12">
<textarea type="text" name="status" id="status" placeholder="What's on your mind.." class="form-control" style="height:100px;"></textarea>
</div>

<div class="col-sm-12 form-group">
<input style="width:100%" type="submit" name="submit" id="submit" value="Post" class="btn btn-success">
</div>

</form>

Jquery

$(document).ready(function() {
$("#submit").click(function(e) {

var status = $('form')[0].checkValidity();
if (status) {
var formData = new FormData($('form')[0]);

$.ajax({
url: "form_post.php",
type: "POST",
data: formData,
processData: false,
contentType: false,
async: false,
dataType: "JSON",

success: function(json) {
if (json.error) {
alert(json.error_msg);
e.preventDefault();
} else {
alert("Post updated successfully!");
}
},

error: function(jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
}

});
});

php

<?php
session_start();
define('HOST','localhost');
define('USER','**');
define('PASS','**');
define('DB','**');

$response = array();

$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

if(!mysqli_connect_errno()){

$error_flag = false;
/*foreach($_POST as $value){
if(empty($value)){
$error_flag = true;
break;
}
}*/

if(!$error_flag){

//receiving post parameters
$status =$_POST['status'];


// create a new user profile
$sql = "INSERT INTO status (via, status, created_at) VALUES ('".$_SESSION['vault_no']."', '$status', NOW())";

if(mysqli_query($con,$sql)){
$response["error"] = false;
$response['via'] = $via;
echo json_encode($response);
}else{
$response["error"] = true;
$response["error_msg"] = "INSERT operation failed";
echo json_encode($response);
}
}else{
$response["error"] = true;
$response["error_msg"] = "Few fields are missing";
echo json_encode($response);
}

}else{
$response["error"] = true;
$response["error_msg"] = "Database connection failed";
echo json_encode($response);
}
?>

最佳答案

注:解答在本题其他读者的评论中

也许这对你有帮助。您需要将其更改为您希望的场外

并保存此功能,将来可能对您有用。

此函数按照应有的方式序列化表单。

<script>
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};

$(function() {
$('form').submit(function() {
var formData = $('form').serializeObject();
$.ajax({
data: formData,
type: 'POST',
url: 'form_post.php',
success: function(result) {
$('#result').html(result);
},
error: function(jqXHR, textStatus, errorThrown) { alert(textStatus); }
});
return false;
});
});
</script>

关于javascript - 表单字段未存储在 mysql 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36614976/

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