douti0467 2018-09-23 18:23
浏览 50

动态字段的问题仅使用jquery php和mysql保存第一个结果

I will try to keep it short and simple. I have a Form that Creates multiple fields via Jquery when a button is pushed.

The problem is that when the form is submitted and the Php saves the data in the MYSQL Database. It only saves the first hardcoded field. The generated fields do not submit.

If I hardcode the fields with name="mytext[]" It works However, the fields generated by the Jquery do not! they are the same code being submitted. I even went as far as adding a counter to each with name="mytext[+x+]" in the js file. Still nothing.

Im curious if it has to do with my enviorment. Its a fresh download of XAMPP on windows

It only saves when the fields are hardcoded in. I'm at a loss here. I have used this method before and it has worked.

********UPDATE************** I did a var dump and it is showing that only the first field is being submited.

array(1) { [1]=> string(5) "TEST1" }

What am i doing wrong?

Here are the 3 parts:

HTML

    <form class="form-horizontal" method="POST" action="">
    <div class="input-group mb-3">
            <div class="input-group-prepend">
                <label class="input-group-text" 
    for="release_name">Name</label>
            </div>
                    <input class="form-control" type="text" name="release_name" value="">
                </div>
            </div>
            <div class="input-group mb-3">
            <div class="input-group-prepend">
                <label class="input-group-text" for="release_desc">Description</label>
            </div>
                    <input class="form-control" type="text" name="release_desc" value="">
                </div>
            </div>
             <script src="assets\js\multifield.js"></script>
             <div class="input_fields_wrap">
     <button class="btn add_fields">Add More Fields</button>
             <div class="wrapper">
                 <div>
                     <div class="input-group mb-3">
                         <div class="input-group-prepend">
                             <label class="input-group-text" for="release_desc">New Variable</label>
                         </div>
                         <input class="form-control" type="text" name="mytext[1]"></input>
                     </div>
                 </div>

             </div>
            </div>
            <div class="input-group mb-3">
            <div class="input-group-prepend">
                <label class="input-group-text" for="release_content">Release</label>
            </div>
                    <textarea name="release_content" rows="15" cols="80"></textarea>
                </div>
            </div>

            <div class="form-group col-md-12 text-center">

                <input type="hidden" name="company_id" value="<?php if(isset($compid)) echo $compid; ?>">

                <input name="updateProfileBtn" type="submit" class="btn btn-primary pull-right bottom-margin" value="Create Release">
            </div>
        </form>

JS

    //Add Input Fields
    $(document).ready(function() {
    var max_fields = 10; //Maximum allowed input fields
    var wrapper    = $(".wrapper"); //Input fields wrapper
    var add_button = $(".add_fields"); //Add button class or ID
    var x = 1; //Initial input field is set to 1

    //When user click on add input button
    $(add_button).click(function(e){
    e.preventDefault();
    //Check maximum allowed input fields
    if(x < max_fields){
        x++; //input field increment
         //add input field
        $(wrapper).append('<div><div class="input-group mb-3"><div 
        class="input-group-prepend"><label class="input-group-text" 
        for="release_desc">New Variable</label></div><input class="form- 
        control" type="text" name="mytext['+x+']"></div><a 
        href="javascript:void(0);" class="remove_field">Remove</a></div>'); 
        //add input box
      }
    });

    //when user click on remove button
    $(wrapper).on("click",".remove_field", function(e){
    e.preventDefault();
    $(this).parent('div').remove(); //remove inout field
    x--; //inout field decrement
    })
   });

PHP

        else if(isset($_POST['updateProfileBtn'])) {
    //Initilaise the array
    $form_errors = array();



    //Collect data from form
    $release_name = $_POST['release_name'];
    $release_desc = $_POST['release_desc'];
    $release_content = $_POST['release_content'];
    $company_id = $_POST['company_id'];

    //Create JSON ARRAY FROM MULTI FEILD ARRAY

$fields="";
if(isset($_POST["mytext"]))
{
 $fields = json_encode($_POST['mytext']);
    }


    $id = "";

    if(empty($form_errors)){
        try{
            //create sql update
            $sqlUpdate = "INSERT into releases SET id =:id, feilds =:feilds, release_name =:release_name, release_desc =:release_desc, release_content =:release_content, company_id =:company_id";

            //Sanitise
            $statement = $db->prepare($sqlUpdate);

            //update
            $statement->execute(array(':id' => $id, ':feilds' => $fields, ':release_name' => $release_name, ':release_desc' => $release_desc, ':release_content' => $release_content, ':company_id' => $company_id));

            //Check if one new row was created
            if($statement->rowCount() == 1){
                $result = '
                    <script type="text/javascript">
                        swal("You have a New Release!", "Your new Release has been created", "success");
                    </script>
                ';
            }
            else {
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题