0

*) when we click addmore and add content for the below code 2 textboxes will appear extra?? how to avoid that??please help

<?php
    include_once 'config.php';
    if (isset($_POST['submit'])) {


        $text1 = $_POST['boxes'];
        $text2 = $_POST['boxes1'];

        foreach($text1 as $a => $b){


           if (!isset($text1[$a])) {
           $text1[$a] = null;
    }
           echo "<input type='text' name='boxes12' id='text1' value=$text1[$a]>";

           if (!isset($text2[$a])) {
           $text2[$a] = null;
    }
          echo "<input type='text' name='boxes11' id='text2' value=$text2[$a]>";
        }
    }
    ?>

    <!DOCTYPE html>
    <html>
    <head>
    <title>Add or Remove text boxes with jQuery</title>
    <script type="text/javascript" src="//code.jquery.com/jquery-latest.js"></script>
    <style type="text/css">
    <!--
    #main {
        max-width: 800px;
        margin: 0 auto;
    }
    -->
    </style>
    <body>
    <div id="main">
        <h1>Add or Remove text boxes with jQuery</h1>
        <div class="my-form">
            <form role="form" method="post">
                <p class="text-box">
                    <label for="box1">Box <span class="box-number">1</span></label>
                    <textarea name="boxes[]" value="" id="box1" ></textarea>
                    <input type="text" name="boxes1[]" value="" id="box2" >
                    <a class="add-box" href="#">Add More</a>
                </p>
                <p><input type="submit" value="Submit" name="submit" /></p>
            </form>
        </div>
    </div>
    <script type="text/javascript">
    jQuery(document).ready(function($){
        $('.my-form .add-box').click(function(){
            var n = $('.text-box').length + 1;
    //        if( 5 < n ) {
    //            alert('Stop it!');
    //            return false;
    //        }
            var box_html = $('<p class="text-box"><label for="box' + n + '">Box <span class="box-number">' + n + '</span></label> <textarea name="boxes[]" value="" id="box1' + n + '" ></textarea> <input type="text" name="boxes[]" value="" id="box2' + n + '" /><a href="#" class="remove-box">Remove</a></p>');

        box_html.hide();
            $('.my-form p.text-box:last').after(box_html);
            box_html.fadeIn('slow');
            return false;
        });
        $('.my-form').on('click', '.remove-box', function(){
            $(this).parent().css( 'background-color', '#FF6C6C' );
            $(this).parent().fadeOut("slow", function() {
                $(this).remove();
                $('.box-number').each(function(index){
                    $(this).text( index + 1 );
                });
            });
            return false;
        });
    });
    </script>
    </body>
    </html>
2
Contributors
1
Reply
11
Views
2 Years
Discussion Span
Last Post by diafol
0

You posted into the PHP forum - so we are to assume that you want hep with the PHP? Sorry if that's a bit obvious.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.