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

Link to comment
Share on other sites

Why would it report back just array, instead of some type of value?

Should I put a value in each one and then use an if statement?

like:

interest[0]

and then:

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

Or would that just overcomplicate for no reason?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

The missing numbers were to ones that were checked.

 

I read some where you can fix a problem like this by doing something like:

$interest  = $interest$_POST['interest'] = " ";

 

or something like that. Does that make sense?

Link to comment
Share on other sites

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];
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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