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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀