Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Description: On the form appears radio buttons with textboxes. Their count = count that you entered in textbox. In test.php file I need get all radio buttons and their textbox values, it does not matter - checked, unchecked radio buttons state
Error is that I get only the most recent values of radiobutton and textbox. I do not see values of previous radiobuttons and textboxes.

XML
<!doctype html>
<html lang="en">
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
// serialize() will get all data from form with id "myform" and store it in data variable, that will be sent to test.php page.
function getnewElementID()
{
    var elementID = 0;
    if($(".form-element").length > 0)
        elementID = $(".form-element").length;
    return elementID;
}
$(document).ready(function() {
        $('#ok1').click(function () {
        var n = $('#box1').val();
        var index = 0;
        var newElementID = getnewElementID();

        if($(".RadioDiv").length > 0)
        index = $(".RadioDiv").length;

        var newElement = "<div class='RadioDiv form-element'><input type='hidden' name='RadioElements["+newElementID+"]' value='radiogroup'/>\n<input  type='text' size='50' name='RadioElementsQueations["+newElementID+"]'  placeholder='question...' >";

        for (var i=1;i<=n;i++)
        {
            newElement+="<ol><input type='radio' name='RadioElementsValues["+newElementID+"]' value='radio_"+index+"' for='RadioElementsValuesText["+newElementID+"]'>\n\<input type='text' id='radtext' name='RadioElementsValuesText["+newElementID+"]'  size='30' value='choice.."+index+"' ></ol>";
        index++;
        }

    newElement+="</div>";
    $("#myForm").append(newElement);
});});



    $(document).ready(function() {
        $('#submit').click(function () {

         var data = $("#myForm").serialize();

         if($('input[type="radio"]:checked').length == "0") {
              alert("Select any value");
         } else {
                $.post (
                    "test.php",
                     data,
                     function (response) {
                     $('#message').html(response);
                     }
                );
        }
        return false;
        });

    });

</script>

</head>
<body>
    <form id ="myForm" action="test.php">
        <label>Select your favourite Activities:</label><br/>
        <input type="button" name="ok" value="ok" id="ok1">
        <input type="text" value="" id="box1" name="count" size="1"><br/>
        <input type="submit" id="submit"/> <br/>
    </form>
<div id="message"></div>

</body>
</html>





test.php
XML
<?php
    var_dump($_POST);

    $elements = $_POST['RadioElementsValuesText'];
    if(is_array($elements));
    $astring = implode("<br/>", $elements);
    echo "Choices: ". $astring."<br/>";
?>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900