Jump to content

submitting with checkboxes


dan_t

Recommended Posts

Choosing these from the checkbox list.

 Art<input type="checkbox" name="interest" value="art" /> Boating<input type="checkbox" name="interest" value="boating" />
    Camping<input type="checkbox" name="interest" value="camping" /> Diving (Scuba)<input type="checkbox" name="interest" value="diveSc" />
    Diving (Sky)<input type="checkbox" name="interest" value="diveSk" /> 
    Equestrian
    <input type="checkbox" name="interest" value="esquest" />
    Field sports<input type="checkbox" name="interest" value="field_sports" /> Fishing<input type="checkbox" name="interest" value="fish" /><br />
   <br /> Golf<input type="checkbox" name="interest" value="golf" /> Hunting<input type="checkbox" name="interest" value="hunting" />
    Jogging<input type="checkbox" name="interest" value="jog" /> Line dancing<input type="checkbox" name="interest" value="line_dance" />
    Motor sports<input type="checkbox" name="interest" value="motor_sports" /> Music<input type="checkbox" name="interest" value="music" />
    Racing<input type="checkbox" name="interest" value="racing" /> 
    Skiing<input type="checkbox" name="interest" value="skiing" /> Tennis<input type="checkbox" name="interest" value="tennis" />

 

When you choose more than one and submit it, it only submits the last choice to the database, how do you enter more than one value?

Link to comment
https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/
Share on other sites

when you name them all the same  ... "interest"

 

you have to put braces beside it or else it just overwrites "interests[]"

Note: this will give you an array in your POST or GET request.

 

$_POST['interest'][0] should have something in it.  If a person checked something.

I've got everything working, but i get these:

 

Notice: Undefined index: interest in C:\wamp\www\bills\show_info.php on line 33

 

Notice: Undefined index: interest2 in C:\wamp\www\bills\show_info.php on line 35

 

Notice: Undefined index: interest3 in C:\wamp\www\bills\show_info.php on line 36

 

Notice: Undefined index: interest4 in C:\wamp\www\bills\show_info.php on line 37

 

Notice: Undefined index: interest5 in C:\wamp\www\bills\show_info.php on line 38

 

Notice: Undefined index: interest6 in C:\wamp\www\bills\show_info.php on line 39

 

Notice: Undefined index: interest7 in C:\wamp\www\bills\show_info.php on line 40

 

Notice: Undefined index: interest8 in C:\wamp\www\bills\show_info.php on line 41

 

Notice: Undefined index: interest9 in C:\wamp\www\bills\show_info.php on line 42

 

Notice: Undefined index: interest11 in C:\wamp\www\bills\show_info.php on line 44

 

Notice: Undefined index: interest12 in C:\wamp\www\bills\show_info.php on line 45

 

Notice: Undefined index: interest13 in C:\wamp\www\bills\show_info.php on line 46

 

Notice: Undefined index: interest14 in C:\wamp\www\bills\show_info.php on line 47

 

Notice: Undefined index: interest15 in C:\wamp\www\bills\show_info.php on line 48

 

with the unused checkboxes.

you had it right the first time... using the array solution.

 

Having 15+ differently named checkboxes is by no means a more efficient way of doing this.  The reason you get the undefined index is because all of those checkboxes aren't checked.. and you're coding it in such a way that all of them would be checked.

 

By using "interest[]" as the name for all of them.. you can then access them like this

$_POST['interest'][0]

and $_POST['interest'][1] for the next (checked) one... and so on and so on.

 

It's also good practice (in PHP) to use the isset() function.  ... example

if(isset($_POST['interest'])) {
    echo $_POST['interest'][0];
}

or ... you can check if it's actually an array too.

if(isset($_POST['interest']) AND is_array($_POST['interest'])) {
    echo $_POST['interest'][0];
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.