0

I have an error that I can't figure out...

Om my webpage there is a form that the user has the ability to add some new input fields to. If the user is submitting the form, then the optional fields is empty when the php-file is handing them, why?

HTML:

        <form method="post" action="newRequest.php">
            <input type="text" name="title" />
            <input type="hidden" name="fname" value="0" />
            <input type="checkbox" name="fname" value="1"/>
            <input type="hidden" name="ename" value="0" />
            <input type="checkbox" name="ename" value="1" />
            <input type="hidden" name="seat" value="0" />
            <input type="checkbox" name="seat" value="1" />
            <input type="hidden" name="fields" value="0" />
            <input type="text" id="fields" name="fields" />
            <input type="submit" />
        </form>

PHP:

if (strlen($_POST[title]) > 2) {
    $toDb[title] = $_POST[title];
} else {
    error('title');
}

$toDb[fname] = $_POST[fname];
$toDb[ename] = $_POST[ename];
$toDb[seat] = $_POST[seat];

if ($_POST[fields] > 0) {
    $i = 0;
    while ($i < $_POST[fields]) {
        $toDb[optional][$i] = $_POST[optional-$i];
        $i++;
    }
    $toDb[optional] = serialize($toDb[optional]);
} else {
    $toDb[optional] = 0;
}
newEvent($toDb,$dbh);

JQuery that is adding dynamical fields:

$(document).ready(function() {
    $('#fields').focusout(function(){

        var fields = $('#fields').val();
        var i = 0;

        while(i < fields) {
            $('#fields').after("Valfritt fält "+(i+1)+":<input type='text' name='optional"+i+"' />");
            i++;
        }

    })
})
3
  • This is how you use hash arrays: $foo["bar"] = $_POST["bar"];, the string index should be quoted. Commented Mar 20, 2013 at 13:33
  • All your field names have duplicates; the first one gets overwritten by the second one when it gets parsed in PHP. Commented Mar 20, 2013 at 13:34
  • The duplicates is there because the first one is setting a default value that is sent to PHP if the user leaves the field empty. Commented Mar 20, 2013 at 13:37

3 Answers 3

1

You should quote array indexes. It should be

 $toDb['optional'][$i] = $_POST['optional'.$i];
Sign up to request clarification or add additional context in comments.

Comments

1

You are missing commas in $_POST

$toDb['fname'] = $_POST['fname'];
$toDb['ename'] = $_POST['ename'];
$toDb['seat'] = $_POST['seat'];

Here is your modified code

if (strlen($_POST['title']) > 2) {
    $toDb['title'] = $_POST['title'];
} else {
    error('title');
}

$toDb['fname'] = $_POST['fname'];
$toDb['ename'] = $_POST['ename'];
$toDb['seat'] = $_POST['seat'];

if (count($_POST) > 0) {
    $i = 0;
    while ($i < count($_POST)) {
        $toDb['optional'][$i] = $_POST['optional-'.$i];
        $i++;
    }
    $toDb['optional'] = serialize($toDb['optional']);
} else {
    $toDb['optional'] = 0;
}
newEvent($toDb,$dbh);

Also use count() to check if $_POST has values > 0.

Comments

0

I faced the same problem and I solved it using Javascript, like this :

add a new text field every time a button is pressed

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.