electricmouse
electricmouse

Reputation: 29

HTML PHP array index lenght error

I have an html form where I am grouping inputs using name="array[]" then just looping through the array with PHP when submitted. Well I am using array[] to store the question, but when the question (array index) is longer than 64 characters then It will not pass that array key to my PHP.

HTML

<textarea name="corporate[CAN YOU SHOW US SIMILAR PROJECTS WITH THE SAME TARGET AUDIENCE? COMPETITORS?]"></textarea>

When I do:

var_dump($_POST['array']);

I get array(0) But when I use a shorter index, it works.

Now if I manually create an associative array it works fine:

$array = array("CAN YOU SHOW US SIMILAR PROJECTS WITH THE SAME TARGET AUDIENCE? COMPETITORS?"=>"0");

What am I doing wrong?

I think it has to be a problem with going from the html form to the PHP. I am trying to loop through the inputs with my PHP so that I can loop through and display each question and corresponding answer with:

    foreach ($array as $key=>$value) {
        if ($value != NULL) {
            echo '<strong>' . $key . '</strong><br/>';
            echo $value . '<br/><br/>';
        }
    }

Which gives me:

Question

Answer

Question

Answer

etc.

How else could I do this without giving each input their own name to pass the question, or hard coding the question in my HTML?

Upvotes: 0

Views: 70

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 79024

You can use a hidden input with the question and match up the indexes with the answer:

<input type="hidden" name="question[1]" value="CAN YOU SHOW US BLAH?">
<textarea name="answer[1]"></textarea>

I assume you are dynamically adding the question text? If so you should probably use htmlentities on it to avoid issues.

Then just loop one and access the other:

foreach ($_POST['answer'] as $key => $value) {
    if (!empty($value)) {
        echo '<strong>' . $_POST['question'][$key] . '</strong><br/>';
        echo $value . '<br/><br/>';
    }
}

Upvotes: 0

Blah Argh
Blah Argh

Reputation: 302

It might be because you're doing this

var_dump($_POST['array']);

when you named the textarea "corporate". Try just doing

var_dump($_POST);

Upvotes: 2

Related Questions