2

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 = "";
    }
?>
1
  • Just a heads up, mysql_* functions are deprecated. I'd advise you to start using PDO or mysqli_* functions. Commented Sep 26, 2014 at 9:14

1 Answer 1

1

Change:

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

To:

$entries = implode("$$",$_POST["myNewField"]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.