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 );
          }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘