jQuery发布一个序列化的表单然后通过PHP插入到mysql?

Rya*_*yan 1 php mysql forms serialization insert

我想发表连载形式向sumbit.php文件,反过来,会再插入到MySQL数据库; 但是,隐藏的最后一个输入并没有插入到数据库中,尽管其余的都是.

以下是我到目前为止所做的一些不起作用的片段示例:

HTML

            <form method="post" action="" >
                            <label for="name" class="overlay"><span>Name...</span></label>
                            <input class="input-text" type="text" name="name" id="name" />

                            <label for="email" class="overlay"><span>Email...</span></label>
                            <input type="text" class="input-text" name="email" id="email"/>

                            <label for="website" class="overlay"><span>Website...</span></label>
                            <input type="text" class="input-text" name="website" id="website"/>


                            <label id="body-label" for="body" class="overlay"><span>Comment it up...</span></label>
                            <textarea class="input-text" name="body" id="body" cols="20" rows="5"></textarea>

                            <input type="hidden" name="parentid" id="parentid" value="0" />
                            <input type="submit" value="Comment" name="submit" id="comment-submit" />

                </span>
            </form>
Run Code Online (Sandbox Code Playgroud)

使用Javascript

$('form.').submit(function(event) {  

    $.post('submit.php',$(this).serialize(),function(msg){
        // form inputs consist of 5 values total: name, email, website, text, and a hidden input that has the value of an integer
    }
});
Run Code Online (Sandbox Code Playgroud)

PHP(submit.php)

$arr = array();

    mysql_query("   INSERT INTO comments(name,email,website,body,parentid)
                VALUES (
                    '".$arr['name']."',
                    '".$arr['email']."',
                    '".$arr['website']."',
                    '".$arr['body']."',
                    '".$arr['parentid']."'
                )");
Run Code Online (Sandbox Code Playgroud)

Use*_*234 7

好的,这是工作代码.有一些错误(例如,帖子上未关闭的函数括号,未关闭的span标记,PHP中没有POST变量等)

HTML(确保有一个表单ACTION,这样如果人们没有js,你仍然可以使用表格)

<form method="post" id="test" enctype="multipart/form-data" action="post.php">
    <label for="name" class="overlay"><span>Name...</span></label>
    <input class="input-text" type="text" name="name" id="name" />

    <label for="email" class="overlay"><span>Email...</span></label>
    <input type="text" class="input-text" name="email" id="email"/>

    <label for="website" class="overlay"><span>Website...</span></label>
    <input type="text" class="input-text" name="website" id="website"/>


    <label id="body-label" for="body" class="overlay"><span>Comment it up...</span></label>
    <textarea class="input-text" name="body" id="body" cols="20" rows="5"></textarea>

    <input type="hidden" name="parentid" id="parentid" value="0" />
    <input type="submit" value="Comment" name="submit" id="comment-submit" />
</form>
Run Code Online (Sandbox Code Playgroud)

使用Javascript

<script>
$(document).ready(function() {
    $('form').submit(function(msg) {  
        alert($(this).serialize()); // check to show that all form data is being submitted
        $.post("post.php",$(this).serialize(),function(data){
            alert(data); //post check to show that the mysql string is the same as submit                        
        });
        return false; // return false to stop the page submitting. You could have the form action set to the same PHP page so if people dont have JS on they can still use the form
    });
});
</script>
Run Code Online (Sandbox Code Playgroud)

PHP

<?php
$arr = $_POST; //you need to grab the data via the POST (or request) global.

//this is just a check to show that the SQL statement is correct. Replace with your mysql connection/query
echo "INSERT INTO comments(name,email,website,body,parentid)
        VALUES (
            '".$arr['name']."',
            '".$arr['email']."',
            '".$arr['website']."',
            '".$arr['body']."',
            '".$arr['parentid']."'
        )";
?>
Run Code Online (Sandbox Code Playgroud)