表单字段不存储在mysql数据库中

时间:2016-04-14 06:06:29

标签: javascript php jquery html mysql

我创建了一个由textarea字段组成的简单表单,因此当用户点击提交按钮时,它链接到一个jquery脚本,其中包含一个执行该进程并存储数据的url,但问题是每次我点击提交,ID&amp ;存储了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);
    }
?>

1 个答案:

答案 0 :(得分:0)

注意:解决方案在此问题的其他读者的评论中

也许这会帮助你。您需要将其更改为您的希望之处

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

此函数将表单序列化为应如何完成。

<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>