How can I dynamically add input fields to a form?

前端 未结 5 855
南旧
南旧 2021-01-05 17:02

I\'m not much of a web programmer, but I\'m creating a simple web app that has a form where the user can enter a series of points (x,y,z) but I don\'t know how many the user

5条回答
  •  萌比男神i
    2021-01-05 17:46

    1: HTML :

      
    +Add More

    2: PHP

        if(isset($_POST['submit']))
        {
    
                $xx= json_encode($_POST["xx"]);
                $xx= json_encode($_POST["xx"]);
                $xx= json_encode($_POST["xx"]);
                $xx= json_encode($_POST["xx"]);
                $xx= json_encode($_POST["xx"]);
                $xx= json_encode($_POST["xx"]);
    
         $query=mysql_query("INSERT INTO `xxx` (`xx`, `xxx`, `xxx`) VALUES ('NULL', '$xx','$xx','$xx')");
        }
    

    3: JS

        var data_fo = $('.add-field').html();
        var sd = '
    Remove
    '; var data_combine = data_fo.concat(sd); var max_fields = 5; //maximum input boxes allowed var wrapper = $(".user"); //Fields wrapper var add_button = $(".add-more"); //Add button ID var x = 1; //initlal text box count $(add_button).click(function(e){ //on add input button click e.preventDefault(); if(x < max_fields){ //max input box allowed x++; //text box increment $(wrapper).append(data_combine); //add input box //$(wrapper).append('
    Remove
    ') } // console.log(data_fo); }); $(wrapper).on("click",".remove-add-more", function(e){ //user click on remove text e.preventDefault(); $(this).prev('.user').remove(); $(this).remove(); x--; })

提交回复
热议问题