anvd
anvd

Reputation: 4047

checkboxes in php - problem with access

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?

Upvotes: 0

Views: 84

Answers (5)

jeremysawesome
jeremysawesome

Reputation: 7254

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[]'.

Upvotes: 1

Asteriskk
Asteriskk

Reputation: 387

<?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.

Upvotes: 1

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25465

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...

Upvotes: 0

Anze Jarni
Anze Jarni

Reputation: 1157

    $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")

Upvotes: 0

James
James

Reputation: 22247

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");

Upvotes: 0

Related Questions