dtl19910708 2014-01-31 15:00
浏览 71
已采纳

将序列化表单发送到数据库

I'm trying to send a serialized form to my database. I'm doing this because each form can have a different number of user-created input fields and wanted a simple way to store all the forms the same way. My question is that when I serialize the form, use JQuery to attach it to a hidden input field, and then send the the form and some other information to the database, the rest of the information is reaching the database but I still have a blank in where the serialized form is. If anyone could point out where I went wrong and/or explain how this should be done I'd be very grateful! Thank you very much!

Here is how I'm sending the data to the database:

$orderDate = mysql_prep($_POST["orderDate"]);
        $orderName = mysql_prep($_POST["orderName"]);
        $formSerialized = mysql_prep($_POST["formSerialized"]);


        $query = "INSERT INTO test (orderDate, orderName, orderSerialized) VALUES ('{$orderDate}', '{$orderName}', '{$orderSerialized}')";
        $result = mysqli_query($connection, $query);    

Here is the hidden field I am trying to attach the serialized form to:

<input type="hidden" id="phpVar" name="phpVar" value="<?php echo $var; ?>">

And here is the JQuery:

var formSerialized = $("form").serialize();
$("#phpval").val(formSerialized);
  • 写回答

1条回答 默认 最新

  • douduan5753 2014-01-31 16:04
    关注

    It looks like you are serializing your form and then insert it into an element with id of phpval.

    You should use the jQuery ajax function to send the serialized form and then post it to your php page. ref. http://api.jquery.com/jquery.ajax/

    $('#submit').click(function(){
        var form = $('#my_awesome_form');
    
        $.ajax( {
          type: "POST",
          url: form.attr( 'action' ),
          data: form.serialize(),
          success: function( response ) {
            console.log( response );
          }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行