0

i have written a code for the user to select his area of expertise with respect to fields. i am able to display selected checkboxes for those fields. But when i want tio process further like
when the user clicks on the particular checkbox and then submits i am not able to display the various options he/she has checked on.

the below is the initial html code

<html>
<head></head>
<body>
<h2>Please select your area of expertise </h2>
<p>
<form method="get" action="aoe.php">
<select name="area">
<option value="1">Computer science / Applications
<option value="2">Electronics 
<option value="3">Mechanical
<option value="4">Biotechnology
</select>
<input type="submit" value="Send">
</form>
</body>
</html>

the below is the following php code

<html>
<head>
The choice whic u have selected is </head>

<body>

<?php

$day = $_GET['area'];
echo " Please select the related subjects ";


if($day == 1)
{
?>

<form name="form1" method="post" action="aoe.php">
  <input type="checkbox" name="checkbox" value="checkbox">
  Artificial Intelligence 
<input type="checkbox" name="checkbox2" value="checkbox">
  Operating Systems 
<input type="checkbox" name="checkbox3" value="checkbox">
	Compiler Design
<input type="submit" value="Send">
</form>

	
<?php
		if (isset($_POST['checkbox']) )
		{
?>

	 <p>" U have selected artificaial intelligence " </p>

<?php
}
}


elseif($day == 2)
{
?>

<form name="form2" method="post" action="aoe.php">
  <input type="checkbox" name="checkbox" value="checkbox">
  Microprocessor 
<input type="checkbox" name="checkbox2" value="checkbox">
  Electronic circuits 
<input type="checkbox" name="checkbox3" value="checkbox">
	Chip and dales
<input type="submit" value="Send">
</form>


<?php
	
}
elseif($day == 3)
{
?>

<form name="form3" method="post" action="aoe.php">
  <input type="checkbox" name="checkbox" value="checkbox">
  Machines 
<input type="checkbox" name="checkbox2" value="checkbox">
  Tools  
<input type="checkbox" name="checkbox3" value="checkbox">
	Aeromodelling
<input type="submit" value="Send">
</form>	


<?php
}
else
{
?>

<form name="form4" method="post" action="aoe.php">
  <input type="checkbox" name="checkbox" value="checkbox">
  Genesis 
<input type="checkbox" name="checkbox2" value="checkbox">
  Cloning 
<input type="checkbox" name="checkbox3" value="checkbox">
	Aliens
<input type="submit" value="Send">
</form>
	
<?php
}
?>

</body>
</html>

The part where in i am getting err is beneath

<?php
		if (isset($_POST['checkbox']) )
		{
?>

	 <p>" U have selected artificaial intelligence " </p>

<?php
}
}
3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by Fest3er
0

i have written a code for the user to select his area of expertise with respect to fields. i am able to display selected checkboxes for those fields. But when i want tio process further like
when the user clicks on the particular checkbox and then submits i am not able to display the various options he/she has checked on.

Checkboxes and radio buttons are 'oddities'.

The checkbox variable will arrive at the server set either to 'on' or to something else.

If you wish to 're-select' a checkbox, you must include the 'checked' keyword in the <input> tag.

I usually look at all checkboxes when the PHP script runs and create a companion 'selected' var, setting them to either "" or "checked", as appropriate; I also pre-initialize those vars to "". Then when displaying the checkbox, I always include the 'selected' variable.

It took me a week or two to figure this out and do it proficiently. Checkboxes and radio buttons are one of the few things in HTML that are somewhat less than obvious until one groks what the spec writers meant.

0

take your checkbox as an array. use same name for a group of checkboxes and then retrieve respective value using foreach loop

Edited by BzzBee: n/a

0

take your checkbox as an array. use same name for a group of checkboxes and then retrieve respective value using foreach loop

Honestly i got no idea on how to go about doing that . I would be glad if you could throw more light on it .

0

Because PHP and HTML can be infuriating, I wrote a small demo that shows a functioning checkbox mechanism. I haven't done this in a few years and, yes, it too me an hour to get it to work. :)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  </head>
  <html>
    <body>

<?php
print ("<pre>");
print_r($_POST);
print("</pre>");
// Clear initial values
$expertises[one] = "";
$expertises[two] = "";
$expertises[three] = "";
$expertchks[one] = "";
$expertchks[two] = "";
$expertchks[three] = "";

// Get values returned from form, if any
if (isset($expertises))
{
  $expertises = $_POST['expertises'];
  $expertchks = $_POST['expertises'];
  foreach ($_POST['expertises'] as $idx => $val)
  {
    $expertises[$idx] = "on";
    $expertchks[$idx] = " checked=\"checked\"";
  }
}

print ("<pre>");
print_r($expertises);
print_r($expertchks);
print("</pre>");
// Display form

print<<<END
<form action="{$_SERVER['REQUEST_URI']}"
      method="post">
  <table style="border:none; margin:10pt 4px">
    <tr>
      <td width="120px" rowspan="3"
          style="text-align:right">
        Expertises:&nbsp;&nbsp;
      </td>
      <td>
        <input type="checkbox" name="expertises[one]"{$expertchks[one]}>
        First Expertise
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="expertises[two]"{$expertchks[two]}>
        Second Expertise
      </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="expertises[three]"{$expertchks[three]}>
        Third Expertise
      </td>
    </tr>
  </table>
  <input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

END;
?>
0

firstly , i appreciate your efforts . Kudos for that .. !

These are the errors i get while executing your code

Notice: Use of undefined constant one - assumed 'one' in C:\wamp\www\College Web Project\checkbox.php on line 14

Notice: Use of undefined constant two - assumed 'two' in C:\wamp\www\College Web Project\checkbox.php on line 15

Notice: Use of undefined constant three - assumed 'three' in C:\wamp\www\College Web Project\checkbox.php on line 16

Notice: Use of undefined constant one - assumed 'one' in C:\wamp\www\College Web Project\checkbox.php on line 17

Notice: Use of undefined constant two - assumed 'two' in C:\wamp\www\College Web Project\checkbox.php on line 18

Notice: Use of undefined constant three - assumed 'three' in C:\wamp\www\College Web Project\checkbox.php on line 19

Notice: Undefined index: expertises in C:\wamp\www\College Web Project\checkbox.php on line 24

Notice: Undefined index: expertises in C:\wamp\www\College Web Project\checkbox.php on line 25

Notice: Undefined index: expertises in C:\wamp\www\College Web Project\checkbox.php on line 26

Warning: Invalid argument supplied for foreach() in C:\wamp\www\College Web Project\checkbox.php on line 26


Notice: Use of undefined constant one - assumed 'one' in C:\wamp\www\College Web Project\checkbox.php on line 49

Notice: Use of undefined constant two - assumed 'two' in C:\wamp\www\College Web Project\checkbox.php on line 55

Notice: Use of undefined constant three - assumed 'three' in C:\wamp\www\College Web Project\checkbox.php on line 61

0

One of us has a strange PHP. It worked from from Apache on Linux. I suppose if one is going to post an demo, it ought to work.... Again, I post this because using checkboxes and radio buttons is not exactly obvious, even though the documentation is explicitly clear.

IWFM on Iceweasel, leaving no errors behind on Apache.

Your task is now to understand this so you can write your own PHP and XHTML code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Checkbox Demo</title>
  </head>
    <body>
      <script language="Javascript">
        function myRadio(elem) {
          myElem = document.getElementById(elem);
          myElem.checked = true;
        }
        function myToggle(elem) {
          myElem = document.getElementById(elem);
          myElem.checked = myElem.checked ? false : true;
        }
      </script>

<?php
print ("<pre><br />_POST: ");
print_r($_POST);
print("</pre>");

// Clear initial values
$expertises = array(1=>"", "", "");
$industry = array(1=>"", "", "");

// Get values returned from form, if any
if (isset($_POST['expertises']))
{
  foreach ($_POST['expertises'] as $idx => $val)
  {
    $expertises[$idx] = " checked=\"checked\"";
  }
}
if (isset($_POST['industry']))
{
  $industry[$_POST['industry']] = " checked=\"checked\"";
}

print ("<pre><br />Expertises: ");
print_r($expertises);
print ("<br />Industry: ");
print_r($industry);
print("</pre>");
// Display form

print<<<END
      <form action="{$_SERVER['REQUEST_URI']}"
            method="post">
        <table style="border:none; margin:10pt 4px">
          <tr>
            <td width="120px" rowspan="3"
                style="text-align:right">
              Expertises:&nbsp;&nbsp;
            </td>
            <td>
              <input id="expertise1" type="checkbox" name="expertises[1]"{$expertises[1]} />
              <span onclick='myToggle("expertise1");'>
                First Expertise
              </span>
            </td>
          </tr>
          <tr>
            <td>
              <input id="expertise2" type="checkbox" name="expertises[2]"{$expertises[2]} />
              <span onclick='myToggle("expertise2");'>
                Second Expertise
              </span>
            </td>
          </tr>
          <tr>
            <td>
              <input id="expertise3" type="checkbox" name="expertises[3]"{$expertises[3]} />
              <span onclick='myToggle("expertise3");'>
                Third Expertise
              </span>
            </td>
          </tr>
          <tr>
            <td colspan=2>&nbsp;</td>
          </tr>
          <tr>
            <td width="120px" rowspan="3"
                style="text-align:right">
              Industry:&nbsp;&nbsp;
            </td>
            <td>
              <input id="industry1" type="radio" value="1" name="industry"{$industry[1]} />
              <span onclick='myRadio("industry1");'>
                Industry One
              </span>
            </td>
          </tr>
          <tr>
            <td>
              <input id="industry2" type="radio" value="2" name="industry"{$industry[2]} />
              <span onclick='myRadio("industry2");'>
                Industry Two
              </span>
            </td>
          </tr>
          <tr>
            <td>
              <input id="industry3" type="radio" value="3" name="industry"{$industry[3]} />
              <span onclick='myRadio("industry3");'>
                Industry Three
              </span>
            </td>
          </tr>
        </table>
        <input type="submit" name="submit" value="Submit" />
    </form>
  </body>
</html>

END;
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.