【问题标题】:jQuery post a serialized form then inserting into mysql via php?jQuery发布一个序列化的表单然后通过php插入到mysql中?
【发布时间】:2010-07-25 06:58:48
【问题描述】:

我正在尝试将序列化的表单发布到 sumbit.php 文件,然后将插入 MySQL 数据库;然而,最后一个隐藏的输入并没有被插入到数据库中,尽管其余的都是。

这里有一些 sn-p 示例,说明到目前为止我所得到的不起作用:

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>

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
    }
});

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']."'
                )");

【问题讨论】:

标签: php mysql forms insert serialization


【解决方案1】:

好的,这是工作代码。有一些错误(例如,帖子上未闭合的函数大括号、未闭合的跨度标签、您的 PHP 中没有 POST var 等)

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>

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>

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']."'
        )";
?>

【讨论】:

  • $arr = $_POST; 做到了。我没有在正确的位置抓住它。谢谢乔希!
  • 没问题。您应该“接受”这个答案,以便其他人知道哪个有效。干杯
【解决方案2】:

尝试手动添加隐藏字段。正如 Amber 所指出的,这似乎是隐藏字段的问题.. 可能是旧 jquery 版本中的错误,也许请确保您首先拥有最新版本。

否则,也许尝试添加隐藏字段...?

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

    $.post('submit.php',$(this).serialize()+'&parentid='+$('#parentid').val(),function(msg){
    }
});

【讨论】:

  • 当我 alert($(this).serialize()) 它正确显示所有内容。 name=test&email=test%40test.com&website=http%3A%2F%2Fillustratut.com&body=Test+msg&parentid=7
【解决方案3】:

您的脚本应该可以正常工作。请在后端检查 parentid 是 null 还是 0 ??你可以使用

var_dump($arr); 

然后检查表 cmets 的 parentid 字段是否接受 0 或 null 值,并检查您的外键是否以您应得的当前方式定义。

谢谢

【讨论】:

  • 我认为 parentid 字段不接受 null 或 0,因为它是外键。
  • 它只转储了前四个数组元素,而不是 parentid;但是,当我 alert() 序列化表单时,parentid 会正确显示。 parentid int(11) NOT NULL DEFAULT '0'
  • 那么我认为您使用的是较旧的 Jquery :) 您可以按照 Ryan 所说的方式尝试 serialize()+'&parentid='+$('#parentid').val(),它必须有效。谢谢
  • @Ryan:也许你的 PHP 代码有一些缺陷。 print_r($_POST) 带给你什么?
  • @Muhit:我正在使用 jQuery 1.4.2 @Felix Kling:print_r($_POST) 结果正确。数组 ( [name] => test [email] => test@adsa.com [website] => test.com [body] => test text [parentid] => 8)
【解决方案4】:

当你序列化一个表单时,数据用 & 分隔

在您的 PHP 代码中,您必须在 & 符号上爆炸,然后在 = 符号上的新数组中再次爆炸。

例子

$thedata = explode("&", $_POST['data']);
foreach($thedata as $new) {
    list($x, $y) = explode("=", $new);
    ${$x} = $y;
}

然后,您将拥有 PHP 变量,例如 $name、$email、$address 等,无论您在表单上输入的任何其他名称。

【讨论】:

  • 这完全是错误的。 $.post('submit.php',$(this).serialize()) 不会创建数据发布变量。
  • 不,jQuery 会将数据作为普通的 POST 参数发送。您似乎认为 jQuery 只会发送一个带有序列化数据的参数 (data)。这是不正确的。并且 OP 已经说过它适用于除隐藏字段之外的所有字段。
猜你喜欢
  • 2020-02-19
  • 2019-06-30
  • 1970-01-01
  • 2014-12-06
  • 2017-06-28
  • 1970-01-01
  • 2016-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多