Toby
Toby

Reputation: 37

Dynamically generate textboxes for php form, put them into array, implode to string and write string to db

The following Javascript function works and adds 1-10 input fields to a form. The user can choose how many using an option menu.

function run() {
    var i = document.getElementById("radioNumber").value;
    while (document.getElementById("neu").firstChild) {
        neu.removeChild(neu.firstChild);
    }
    for(j=0; j<i; j++){
        var feld = document.createElement("input");
        feld.setAttribute("name", "myNewField[]");
        document.getElementById("neu").appendChild(feld);
    }
}

This is the form:

<div class="radio field" style="display:none">
    <form action="&lt;?php echo htmlspecialchars($_SERVER[" php_self"]);?>
        " method="post"> <label for="title2">Name your textfield</label> 
        <input type="text" name="title2"> </br>
        </br>
        <label for="text">Choose a step</label> 
        <select name="step2">
            <option value="0">Select</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select>
        </br>
        </br>
        <label for="pos2">Choose a position</label> 
        <input type="text" name="pos2"></br>
        </br>
        </br>
        <label for="radio">How many radio fields do you want to add?</label> 
        <select id="radioNumber" onchange="run()">

<!--Call run() function-->
            <option value="0">Select</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
        </select>
        <br> 
        <div id="neu">
        </div>
        </br>
        <input type="submit" name="radio" value="Confirm"> 
    </form>
</div>

This is my PHP code where I try to insert the values in a db. Everything works great except inserting the variable $entries. I get the array myNewField[] and want to implode it in order to insert for example entry1$$entry2$$entry3$$entry4 as a string. The field entries in the db remains empty but print_r($_POST) shows me that the entries are written in the array myNewField[]... Does anyone see the error?

<?php
if(isset($_POST["title2"])){
        $title2 = htmlspecialchars ($_POST["title2"]);
        $req = htmlspecialchars ($_POST["required"]);
        $step2 = htmlspecialchars ($_POST["step2"]);
        $pos2 = htmlspecialchars ($_POST["pos2"]);
        $entries = implode("$$",$_POST["myNewField[]"]);
        echo "test".$entries;
        mysql_query("INSERT INTO field(type_id, title, step, pos, visible, entries, required) VALUES ('2', '".$title2."', '".$step2."', '".$pos2."', '1', '".$entries."', '".$req."');");
        print_r($_POST);
    }
    else{
        $title2 = "";
        $req ="";
        $step2 = "";
        $pos2 = "";
        $entries = "";
    }
?>

Upvotes: 2

Views: 690

Answers (1)

Ganhammar
Ganhammar

Reputation: 1951

Change:

$entries = implode("$$",$_POST["myNewField[]"]);

To:

$entries = implode("$$",$_POST["myNewField"]);

Upvotes: 1

Related Questions