简体   繁体   English

如何从 jquery 和 html 字段中获取 mysql 数据库中的多个字段条目

[英]How to get multiple field entry in mysql database from jquery and html fields from php

I have created a field in Html and jquery that can expand basically I made it for customer care centers so that they can put on the pin codes where they want to work on.我在 Html 和 jquery 中创建了一个基本上可以扩展的字段,我为客户服务中心制作了它,以便他们可以在他们想要工作的地方输入密码。 Now I have created a field in Html and jquery that can expand by clicking a plus button now I want to insert individual data for individual field in MySQL database using PHP how can I do that. Now I have created a field in Html and jquery that can expand by clicking a plus button now I want to insert individual data for individual field in MySQL database using PHP how can I do that.

<div class="input_fields_container">
      <div><input type="text" name="product_name[]">
           <button class="btn btn-sm btn-primary add_more_button">Add More Fields</button>
      </div>
    </div>

this is my jquery code这是我的 jquery 代码

 <script>
    $(document).ready(function() {
    var max_fields_limit      = 10; //set limit for maximum input fields
    var x = 1; //initialize counter for text box
    $('.add_more_button').click(function(e){ //click event on add more fields button having class add_more_button
        e.preventDefault();
        if(x < max_fields_limit){ //check conditions
            x++; //counter increment
            $('.input_fields_container').append('<div><input type="text" name="product_name[]"/><a href="#" class="remove_field" style="margin-left:10px;">Remove</a></div>'); //add input field
        }
    });  
    $('.input_fields_container').on("click",".remove_field", function(e){ //user click on remove text links
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script>  

this is my output if you want to get a better idea of what I am trying to say my page这是我的 output 如果你想更好地了解我想说的话我的页面

It is just a traditional way... $_POST['product_name'];这只是一种传统方式... $_POST['product_name'];

First enclose all your inputs under a form, along with a submit button.首先将您的所有输入连同一个提交按钮附在一个表单下。

Then, have an ajax call as follows:然后,有一个 ajax 调用如下:

$(myform).submit(function(){
    $.ajax({
        type: 'post',
        url: "your/url",
        data: new FormData(myform),
        contentType: false,
        processData: false,
        success: function(){.
             //your code
        }
    });
});

In php, you can access the request inputs with $_POST['product_name'] .在 php 中,您可以使用$_POST['product_name']访问请求输入。

Even if you choose to do it without AJAX, the inputs can be accessed in the same way.即使您选择不使用 AJAX,也可以以相同的方式访问输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM