0

i need to reproduce with the code below something like:

myformdata[languages1]
myformdata[languages2]
myformdata[languages3]

But with this code only reproduce myformdata[languages

    <?php
    $sql=mysql_query("select id_spoken_languages, language, path from spoken_languages");
    $i=0;
    while($row=mysql_fetch_array($sql)) {
    $id=$row['id_spoken_languages'];
    $data=$row['language'];
    $flag=$row['path'];
    echo nl2br ("<input type='checkbox' name='myformdata[languages'.$i++.']' value='$id' class='semLargura'>$data <img style='float:right; margin-top:5px; border:1px solid grey;' src='$flag'></img> \n");
    } 
    ?>

i already tried remove the increment and only access with $form_data_array["languages"][1] but in this case i get:


Notice: Uninitialized string offset: 1 in C:\Users\fel\VertrigoServ\www\login\validation2.php on line 9

but if i use `$form_data_array["languages"][0], no error is showed, seems like all checkbox have the index 0.

what is the best way to solve this problem?

3
  • Get a proper editor, and change the quotes around $i++ from ' to ". Any decent editor could've shown you that. Commented Jun 2, 2011 at 13:45
  • You could get rid of the full stops and single quotes completely as php will display $i++ as a value when placed within speech marks (double quotes)... Commented Jun 2, 2011 at 13:50
  • No, you can't, the ++ operator does not work inside quotes, it will just be regarded as text. Commented Jun 2, 2011 at 17:49

5 Answers 5

1

It looks like your output string may be incorrect. Try this one:

echo nl2br ("<input type='checkbox' name='myformdata[languages".$i++."]' value='$id' class='semLargura'>$data <img style='float:right; margin-top:5px; border:1px solid grey;' src='$flag'></img> \n");

I replaced the two instance of single quotes before and after the increment output with double quotes.

Also note, there is no need to increment in PHP. All you need to add is two empty brackets [] and PHP will take care of the incrementing itself. So if you'd like to access the input with $_POST["languages"][1] you can do so by generating the following: name='languages[]'.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. This is exactly what i pretend: name='myformdata[languages][]'
1
<?php
    $sql=mysql_query("select id_spoken_languages, language, path from spoken_languages");
    $i=0;
    while($row=mysql_fetch_array($sql)) {
    $id=$row['id_spoken_languages'];
    $data=$row['language'];
    $flag=$row['path'];
    echo nl2br ("<input type='checkbox' name='myformdata[languages".$i++."]' value='$id' class='semLargura'>$data <img style='float:right; margin-top:5px; border:1px solid grey;' src='$flag'></img> \n");
    } 
    ?>

Try this.

Comments

0

Looks like quotes are messed up:

echo nl2br ("<input type='checkbox' name='myformdata[languages" . $i++ . "]' value='$id' class='semLargura'>$data <img style='float:right; margin-top:5px; border:1px solid grey;' src='$flag'></img> \n");

Comments

0
    $i++;
echo nl2br ("<input type='checkbox' name='myformdata[languages$i]' value='$id' class='semLargura'>$data <img style='float:right; margin-top:5px; border:1px solid grey;' src='$flag'></img> \n")

Comments

0

Change this line

 echo nl2br ("<input type='checkbox' name='myformdata[languages'.$i++.']' value='$id' class='semLargura'>$data <img style='float:right; margin-top:5px; border:1px solid grey;' src='$flag'></img> \n");

to

 echo nl2br ("<input type='checkbox' name='myformdata[languages" .$i++. "]' value='$id' class='semLargura'>$data <img style='float:right; margin-top:5px; border:1px solid grey;' src='$flag'></img> \n");

Also I'm not 100% sure whether []'s are allowed for names of elements. If not you could consider change the name to be like

myformdata_1 ect...

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.