Checkboxes and Radio Buttons


Neue Antwort erstellen
Erstellt
Jan. '07
letzte Antwort
Noch keine
3,4 T.
Aufrufe
1
„Gefällt mir“
0
Abos
phpinfo
Handyfan
Handyfan


Anm. Datum: 18.10.2006
Beiträge: 41

Handy: SE P900
BeitragMo 08. Januar, 2007 19:01
Antworten mit Zitat


Checkboxes and Radio Buttons

Having trouble with you checkboxes and radio buttons. Here is you one stop answer shop for everything you ever wanted to know about them and how to use them in conjunction with PHP (obviously)

Difference between the two
Just so that there is no confusion, i will quickly tell you what the difference is and when to use each.

Checkboxes are mainly use when presenting the user with a number of options, of which, they can select more than one option. For example, a form asking a user what topics they are interested in, obvioulsy the user may be interested in more than one topic.

Radio buttons are generally used when presenting the user with an "either/or" option. The user is presented with two or more options, of which, the user can only select one option. For example, a form asking the user which method of payment they want to use. The user can only select one.


Checkboxes

Okay, lets construct a simple form asking the user what sports they like. There will be multiple options of which the user can select none, some or all the options.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="results.php">
<table width="300"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><input type="checkbox" name="sports[]" value="Football"></td>
    <td>Football</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sports[]" value="Rugby"></td>
    <td>Rugby</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sports[]" value="Cricket"></td>
    <td>Cricket</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sports[]" value="Formula 1"></td>
    <td>Formula 1 </td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sports[]" value="Tennis"></td>
    <td>Tennis</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sports[]" value="Golf"></td>
    <td>Golf</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sports[]" value="Darts"></td>
    <td>Darts</td>
  </tr>
</table>
<p>
  <input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>


Notice that each checkbox has been given exactly the same name? The reason for which will become apparent in a moment.

Now, we need some PHP to process the form. We are just going to output the options the users selected, but this should give you the concept of how this works so you an adapt it to insert the values in a Database, or anything else you want to do with them.

PHP:


<?
if (!isset($_POST['sports'])) { 
        echo 
"You did not select any sports, you really should do some exercise!"
} else { 
        echo 
"You selected the following sport(s):<br>" 
        
foreach ($_POST['sports'] as $selected_sport) { 
                echo 
$selected_sport."<br>"
        } 

?>




Can you see why we named the checkboxes as we did yet? Naming all the checkboxes sports[] instructs PHP to construct an array called $sports containing all selected values.

Easy eh?

So lets me just explain the PHP code a little.

A common mistake i see people making when using checkboxes is that they do not allow for someone not selecting anything. Then, when they run their script they get an error saying Undefined index.

This is because, if the user decides not to select any of the options, the array $sports will not be created at all - not even empty.

This is why, in the PHP snippet above, we have used

PHP:

if (!isset($_POST['sports'])) { 



This checks to see if the user actually selected anything. If they didn't, we can output the message "You did not select any sports, you really should do some exercise!".

If the user did make some selections, we jump to the next part of the code, and display all the options they selected. We do this using the foreach function. We itterate through the $sports array, outputting each value to the broswer.

Warum willst Du den Post von phpinfo melden?






PHP:

<?php 
echo 'Php Rules!'
?>






C&M distanziert sich konkret und ausdrücklich vom Inhalt dieses Postings.
Der Ersteller des Postings haftet für seine Äußerungen.
Inhalte, die nicht den Forumsregeln entsprechen sind bitte vom Leser zu melden ...

Benutzer-Profile anzeigen Private Nachricht senden

 Post #1

Werbung
World4You Webhosting

Beiträge der letzten Zeit anzeigen:      

Neue Antwort erstellen

Ähnliche Themen:
Das Radio und der Katastrophenfall
Das Radio und der Katastrophenfall
Das ist die FM4 Radio Session mit Danger Dan und dem RSO Wien
Slowakei: Proteste gegen Umbaupläne für Radio und TV
Get ready: die FM4 Radio Session mit Danger Dan und dem RSO Wien kommt am Freitag!
Get ready: die FM4 Radio Session mit Danger Dan und dem RSO Wien kommt am Freitag!
Get ready: die FM4 Radio Session mit Danger Dan und dem RSO Wien kommt am Freitag!
FM4 Radio Session mit Danger Dan und Orchester
Moskau verbietet Radio Free Europe Aktivitäten in Russland
Am Radio hantiert: Alkolenker prallt in Leitschiene

Kurze URL:

Du hast bereits für diesen Post angestimmt...

;-)




Alle Zeiten sind GMT + 1 Stunde

Top