Function doesnt work with drop down

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lisa007
    New Member
    • Apr 2010
    • 29

    Function doesnt work with drop down

    I'm trying to use a function that will be if A nill then B and C get a value and vice verse if B nill C and A have value and the all function meant to be called right at the top of the drop down list just like the one that says SELECT CATEGORY

    Code:
     <html>
    <body>
    
    <?php
    
    // Create an empty array to hold the error messages.
    $arrErrors = array();
    //Only validate if the Submit button was clicked.
    if (!empty($_POST['Submit'])) {
        // Each time there's an error, add an error message to the error array
        // using the field name as the key.
        	
    		if ($_POST['campustype']=='')
            $arrErrors['campustype'] = 'Please select category.';
    		
    	
    function Name(){
    
       if ($_POST['howt']=='')
        {
              		  
    		  (isset($_POST['hot']) && isset($_POST['ho']));
    		  
    	  }
    		
       
     else
     
    
    if   ($_POST['hot']=='')
        {
              		  
    		  (isset($_POST['howt']) && isset($_POST['ho']));
    } 
    		         
    				  
    	 else 
    
      if ($_POST['ho']=='')
        {
              		  
    		  (isset($_POST['howt']) && isset($_POST['hot']));
    } 			  
    				  
    		else 
    				  
     if ($_POST['ho && howt && hot']=='')
            $arrErrors['Name()'] = 'Please category.';
    }
      if (count($arrErrors) == 0) {
            // If the error array is empty, there were no errors.
            // Insert form processing here.
        } else {
            // The error array had something in it. There was an error.
            // Start adding error text to an error string.
            $strError = '<div class="formerror"><p><img src="/images/triangle_error.gif" width="16" height="16" hspace="5" alt="">Please check the following and try again:</p><ul>';
            // Get each error and add it to the error string
            // as a list item.
            foreach ($arrErrors as $error) {
                $strError .= "<li>$error</li>";
            }
            $strError .= '</ul></div>';
        }
    }
    ?>
    
    <style>
    
    
    .formerror {
      border: 1px solid red;
      background-color : #FFCCCC;
      width: auto;
      padding: 5px 0;
    }
    
    .errortext {
      padding-left: 80px;
      font-size:14px;
      color:red;
     
    }
    
    
    
    </style>
    
    
    <script type="text/javascript">
    function toggleSelect(id)
    {
        if (id == 'off')
        {
              document.getElementById('in-campu').style['display'] = 'none'; //ui
              document.getElementById('1').style['display'] = 'none';
              document.getElementById('off-campus').style['display'] = 'block';
        }
    
        if (id == 'in')
        {
    
              document.getElementById('off-campus').style['display'] = 'none';
              document.getElementById('1').style['display'] = 'none';
              document.getElementById('in-campu').style['display'] = 'block';//ui
    
        }
    
    if (id == '1')
        {
              document.getElementById('off-campus').style['display'] = 'none';
              document.getElementById('in-campu').style['display'] = 'none'; //ui
              document.getElementById('1').style['display'] = 'block';
        }
    
    
    }
    </script>
    
    
    <?php echo $strError; ?>
    <form method="post" action="<?php echo $PHP_SELF; ?>">
    <!--
    For every form field, we do the following...
    
    Check to see if there's an error message for this form field. If there is,
    add the formerror class to the surrounding paragraph block. The formerror
    class contains the highlighted box.
    
    Insert the contents of what the user submitted bak into the form field.
    
    Check again to see if this field has an error message. If it does, show
    the error icon and the error message next to the field.
    -->
    
    <p<?php if (!empty($arrErrors['campustype'])) echo ' class="formerro"'; ?>>
    
        <?php if (!empty($arrErrors['campustype'])) echo '<br /><span class="errortext">'.$arrErrors['campustype'].'</span>'; ?><br/><label for="incampus">Select Category</label>
    <input type="radio" name="campustype" value="in" onclick="toggleSelect('in');" /><label for="campustype">Music</label>
    
    <input type="radio" name="campustype" value="off'" onclick="toggleSelect('off');" /><label for="campustype">Sports</label>
    
    <input type="radio" name="campustype" value="1" onclick="toggleSelect('1');" /><label for="campustype">Art</label>
    
    </p>
    
    
    <p<?php if (!empty($arrErrors['Name()'])) echo ' class="formerro"'; ?>>
    
        <?php if (!empty($arrErrors['Name()'])) echo '<br /><span class="errortext">'.$arrErrors['Name()'].'</span>'; ?><br/>
    	
    <select id="in-campu" name="howt">
     <option name="hot"  value ="">--Select  Music Type--</option>
    <option name="how" value="tuiy">Concerts</option>
    <option name="hot" value="tfyrty" >Clubs</option>
    <option name="hot" value="rtyyt">Festival</option>
    <option name="hot" value="uyity">Opera</option>
    
    </select>
    
    
        <select id="off-campus" class="item" name="hot" style="display: none;">
     <option name="dg" value=""> -- Select Sport Type -- </option>
    <option name="hot" value="dfg">Formula 1</option>
    <option name="hot" value="dfrg">Footbal</option>
    <option name="hot" value="dfgf">Basketball</option>
    <option name="how" value="rugby">Rugby</option>
    <option name="hot" value="cricket">Cricket</option>
    </select>
    
        <select id="1" class="item" name="ho" style="display: none;">
    <option name="hot" value=""> -- Select Art & Theatre Type -- </option>
    <option name="ht" value="hjk">Comedy</option>
    <option name="ho"  value=" gfhftgh">Drama</option>
    <option name="hghjot" value="ioiui">Museus</option>
    </select>
    
    </p>
    
    
    <p>
        <input type="submit" name="Submit" value="Submit">
    </p>
    </form>
    </body>
    </html>
  • guillermobytes
    New Member
    • Jan 2010
    • 77

    #2
    how are represented A,B and C? where do you get them?

    Comment

    • lisa007
      New Member
      • Apr 2010
      • 29

      #3
      sorry A B C meant to be howt, hot and ho those are the names of the drop dwon list if you look at the drop down list

      Comment

      • guillermobytes
        New Member
        • Jan 2010
        • 77

        #4
        what values do you want to give to A B and C?
        also what are you trying to do with the function Name() ?

        Comment

        • lisa007
          New Member
          • Apr 2010
          • 29

          #5
          if look at this array

          Code:
          if ($_POST['campustype']=='')
                  $arrErrors['campustype'] = 'Please select category.';
          
          		which is then called
          
          <p<?php if (!empty($arrErrors['campustype'])) echo ' class="formerro"'; ?>>
          
              <?php if (!empty($arrErrors['campustype'])) echo '<br /><span class="errortext">'.$arrErrors['campustype'].'</span>'; ?><br/><label for="incampus">Select Category</label>
          <input type="radio" name="campustype" value="in" onclick="toggleSelect('in');" /><label for="campustype">Music</label>
          
          <input type="radio" name="campustype" value="off'" onclick="toggleSelect('off');" /><label for="campustype">Sports</label>
          
          <input type="radio" name="campustype" value="1" onclick="toggleSelect('1');" /><label for="campustype">Art</label>
          
          </p>
          now i want to do the same with the drop down but if if all 3 drop downs have the same name the user will have to select the drop down 3 times in order to pass
          is the original work i had

          Code:
          <?php
          // Create an empty array to hold the error messages.
          $arrErrors = array();
          //Only validate if the Submit button was clicked.
          if (!empty($_POST['Submit'])) {
              // Each time there's an error, add an error message to the error array
              // using the field name as the key.
              if ($_POST['name']=='')
                  $arrErrors['name'] = 'Please provide your name.';
              if ($_POST['email']=='')
                  $arrErrors['email'] = 'A valid email address is required.';
              if ($_POST['phone']=='')
                  $arrErrors['phone'] = 'Please provide your phone number.';
          		
          		
          		if ($_POST['campustype']=='')
                  $arrErrors['campustype'] = 'Please select category.';
          		
          		
          		
          		
          		if ($_POST['howt']=='')
                  $arrErrors['howt'] = 'select one field.';
          		
          		
          		
          		
          		
              if (count($arrErrors) == 0) {
                  // If the error array is empty, there were no errors.
                  // Insert form processing here.
              } else {
                  // The error array had something in it. There was an error.
                  // Start adding error text to an error string.
                  $strError = '<div class="formerror"><p><img src="/images/triangle_error.gif" width="16" height="16" hspace="5" alt="">Please check the following and try again:</p><ul>';
                  // Get each error and add it to the error string
                  // as a list item.
                  foreach ($arrErrors as $error) {
                      $strError .= "<li>$error</li>";
                  }
                  $strError .= '</ul></div>';
              }
          }
          ?>
          
          <style>
          
          
          .formerror {
            border: 1px solid red;
            background-color : #FFCCCC;
            width: auto;
            padding: 5px 0;
          }
          
          .errortext {
            padding-left: 80px;
            font-size:14px;
            color:red;
           
          }
          
          
          
          
          
          
          </style>
          
          
          <script type="text/javascript">
          function toggleSelect(id)
          {
              if (id == 'off')
              {
                    document.getElementById('in-campu').style['display'] = 'none'; //ui
                    document.getElementById('1').style['display'] = 'none';
                    document.getElementById('off-campus').style['display'] = 'block';
              }
          
              if (id == 'in')
              {
          
                    document.getElementById('off-campus').style['display'] = 'none';
                    document.getElementById('1').style['display'] = 'none';
                    document.getElementById('in-campu').style['display'] = 'block';//ui
          
              }
          
          if (id == '1')
              {
                    document.getElementById('off-campus').style['display'] = 'none';
                    document.getElementById('in-campu').style['display'] = 'none'; //ui
                    document.getElementById('1').style['display'] = 'block';
              }
          
          }
          </script>
          
          <?php echo $strError; ?>
          <form method="post" action="<?php echo $PHP_SELF; ?>">
          <!--
          For every form field, we do the following...
          
          Check to see if there's an error message for this form field. If there is,
          add the formerror class to the surrounding paragraph block. The formerror
          class contains the highlighted box.
          
          Insert the contents of what the user submitted bak into the form field.
          
          Check again to see if this field has an error message. If it does, show
          the error icon and the error message next to the field.
          -->
          <p<?php if (!empty($arrErrors['name'])) echo ' class="formerro"'; ?>>
          
              <?php if (!empty($arrErrors['name'])) echo '<br /><span class="errortext">'.$arrErrors['name'].'</span>'; ?>
          <br />
              <label for="name">Name:</label>
              <input name="name" type="text" id="name" value="<?php echo $_POST['name'] ?>">
          </p>
          
          
          
          <p<?php if (!empty($arrErrors['campustype'])) echo ' class="formerro"'; ?>>
          
              <?php if (!empty($arrErrors['campustype'])) echo '<br /><span class="errortext">'.$arrErrors['campustype'].'</span>'; ?><br/><label for="incampus">Select Category</label>
          <input type="radio" name="campustype" value="in" onclick="toggleSelect('in');" /><label for="campustype">Music</label>
          
          <input type="radio" name="campustype" value="off'" onclick="toggleSelect('off');" /><label for="campustype">Sports</label>
          
          <input type="radio" name="campustype" value="1" onclick="toggleSelect('1');" /><label for="campustype">Art</label>
          
          </p>
          
          
          
          
          
          
          <p<?php if (!empty($arrErrors['howt'])) echo ' class="formerro"'; ?>>
          
              <?php if (!empty($arrErrors['howt'])) echo '<br /><span class="errortext">'.$arrErrors['howt'].'</span>'; ?><br/>
          	
          <select id="in-campu" name="howt">
           <option name="hot"  value ="">--Select  Music Type--</option>
          <option name="how" value="tuiy">Concerts</option>
          <option name="hot" value="tfyrty" >Clubs</option>
          <option name="hot" value="rtyyt">Festival</option>
          <option name="hot" value="uyity">Opera</option>
          
          </select>
          
          
          
          
              <select id="off-campus" class="item" name="hot" style="display: none;">
           <option name="dg" value=""> -- Select Sport Type -- </option>
          <option name="hot" value="dfg">Formula 1</option>
          <option name="hot" value="dfrg">Footbal</option>
          <option name="hot" value="dfgf">Basketball</option>
          <option name="how" value="rugby">Rugby</option>
          <option name="hot" value="cricket">Cricket</option>
          </select>
          
          
          
              <select id="1" class="item" name="ho" style="display: none;">
          <option name="hot" value=""> -- Select Art & Theatre Type -- </option>
          <option name="ht" value="hjk">Comedy</option>
          <option name="ho"  value=" gfhftgh">Drama</option>
          <option name="hghjot" value="ioiui">Museus</option>
          </select>
          
          
          </p>
          
          <p<?php if (!empty($arrErrors['email'])) echo ' class="formerro"'; ?>>
          
              <?php if (!empty($arrErrors['email'])) echo '<br /><span class="errortext">'.$arrErrors['email'].'</span>'; ?>
          <br />
              <label for="email">Email:</label>
              <input name="email" type="text" id="email" value="<?php echo $_POST['email'] ?>">
          </p>
          
          
          
          <p<?php if (!empty($arrErrors['phone'])) echo ' class="formerro"'; ?>>
          
              <?php if (!empty($arrErrors['phone'])) echo '<br /><span class="errortext">'.$arrErrors['phone'].'</span>'; ?>
          <br />
              <label for="phone">Phone:</label>
              <input name="phone" type="text" id="phone" value="<?php echo $_POST['phone'] ?>">
          </p>
          
          
          
          
          <p>
              <input type="submit" name="Submit" value="Submit">
          </p>
          </form>

          Comment

          • guillermobytes
            New Member
            • Jan 2010
            • 77

            #6
            i'm sorry i don't want you to loose your time nor i want to loose mine, your code is very very messy and I have no time to try to understand it, so please try to explain in words:
            - what you have achieved so far,
            - what you are trying to achieve //output
            - what is the data that you have to treat to achieve what you want//input

            Comment

            • lisa007
              New Member
              • Apr 2010
              • 29

              #7
              i have 3 drop dwon list as long the user select from one of the 3 drop down list pass the validation but if user doenst select from none of them display error message so the user is forced to select at at least form one drop down list and one only if none display error message

              Comment

              • guillermobytes
                New Member
                • Jan 2010
                • 77

                #8
                I will try to help and i hope im not mis answering.

                lets say drop down lists are called 'a', 'b' and 'c':
                first ensure at least one has something selected otherwise log an error message:

                Code:
                //first isolate the 3 drop downs
                $dDwns = array();
                $dDwns['a'] = $_POST['a'];
                $dDwns['b'] = $_POST['b'];
                $dDwns['c'] = $_POST['c'];
                //now perform the checks
                $filledDD = array(); //will contain filled drop downs
                foreach ($dDwns as $name => $value) {
                   //every not empty drop down will be rememebered
                   if ('' !== $value) {
                       $filledDD[$name] = $value;
                   }
                }
                //now ensure at least the minimum of dropDowns have been filled
                if (count($filledDD) === 0) {
                   $errorMessages['dropDown'] = 'You must at least fill one of the 3 drop downs';
                   $noContinueErrorCount++; //every time a "fatal" error is triggered increase count (fatal: the user can't go to next step)
                } else {
                   //do what you want with the data of the filled forms $filledDD
                }
                
                //now present the data as you want
                if ($noContinueErrorCount > 0) {
                   //render the current form
                   formNumber1($validFields, $errorMessages);
                } else {
                   //render the next form
                   formNumber2();
                }

                Comment

                • lisa007
                  New Member
                  • Apr 2010
                  • 29

                  #9
                  before you run away from me like evryone does i am trying to figure wher i should implement your code in my page in the meantime heres my options list if you can implement them before me i would much apreciate
                  i named them A B C i'm just learning php sorry about that


                  Code:
                  <select id="in-campu" name="A">
                   <option name="hot"  value ="">--Select  Music Type--</option>
                  <option name="how" value="tuiy">Concerts</option>
                  <option name="hot" value="tfyrty" >Clubs</option>
                  <option name="hot" value="rtyyt">Festival</option>
                  <option name="hot" value="uyity">Opera</option>
                  
                  </select>
                  
                  
                      <select id="off-campus" class="item" name="B" style="display: none;">
                   <option name="dg" value=""> -- Select Sport Type -- </option>
                  <option name="hot" value="dfg">Formula 1</option>
                  <option name="hot" value="dfrg">Footbal</option>
                  <option name="hot" value="dfgf">Basketball</option>
                  <option name="how" value="rugby">Rugby</option>
                  <option name="hot" value="cricket">Cricket</option>
                  </select>
                  
                      <select id="1" class="item" name="C" style="display: none;">
                  <option name="hot" value=""> -- Select Art & Theatre Type -- </option>
                  <option name="ht" value="hjk">Comedy</option>
                  <option name="ho"  value=" gfhftgh">Drama</option>
                  <option name="hghjot" value="ioiui">Museus</option>
                  </select>
                  
                  </p>

                  Comment

                  • guillermobytes
                    New Member
                    • Jan 2010
                    • 77

                    #10
                    Originally posted by lisa007
                    before you run away from me like evryone does i am trying to figure wher i should implement your code in my page in the meantime heres my options list if you can implement them before me i would much apreciate
                    i named them A B C i'm just learning php sorry about that


                    Code:
                    <select id="in-campu" name="A">
                     <option name="hot"  value ="">--Select  Music Type--</option>
                    <option name="how" value="tuiy">Concerts</option>
                    <option name="hot" value="tfyrty" >Clubs</option>
                    <option name="hot" value="rtyyt">Festival</option>
                    <option name="hot" value="uyity">Opera</option>
                    
                    </select>
                    
                    
                        <select id="off-campus" class="item" name="B" style="display: none;">
                     <option name="dg" value=""> -- Select Sport Type -- </option>
                    <option name="hot" value="dfg">Formula 1</option>
                    <option name="hot" value="dfrg">Footbal</option>
                    <option name="hot" value="dfgf">Basketball</option>
                    <option name="how" value="rugby">Rugby</option>
                    <option name="hot" value="cricket">Cricket</option>
                    </select>
                    
                        <select id="1" class="item" name="C" style="display: none;">
                    <option name="hot" value=""> -- Select Art & Theatre Type -- </option>
                    <option name="ht" value="hjk">Comedy</option>
                    <option name="ho"  value=" gfhftgh">Drama</option>
                    <option name="hghjot" value="ioiui">Museus</option>
                    </select>
                    
                    </p>
                    i don't work for you, try to learn some php then come back when you have spotted questions. i wont code the site for you....

                    Comment

                    Working...