www.webdeveloper.com
+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30

    Two submit buttons using switch statementsq

    I have been trying for about a month now to get two submit buttons to work independently in PHP on the same page. I have found examples using text boxes but unfortunately my page has drop downs that use the _$POST array.

    I for some reason cannot figure out how to code the switch case statement properly. Any ideas out there?

    Here is the code for the PHP page:
    (process.php)
    Code:
    <?php
    error_reporting(E_ALL ^ E_NOTICE) ;
    if($_POST['item']) 
      $item = $_POST['item']; 
     // print_r ($_POST['item']); 
    else 
      $item = 2010; 
       print_r ($_POST[item]);
      
      if($_POST['item2']) 
      $item2= $_POST['item2']; 
    else 
      $item2 = 2009; 
      ?>
      
      
      <?php
    
    // branch on the basis of 'Select1' value 
    switch ($_POST['item']) {
          // if Select1 => add
          case 'Submit':
           //     print_r $_POST['Select1']);
    	   $url = "<img src='/WXRECORDS/images/{$item}.png' />"; 
                break;
    
          // if Select1 => subtract
          case 'Submit2':
             //   print_r $_POST['Select1']);
    		 $url = "<img src='/WXRECORDS/images/{$item2}.png' />"; 
                break;
        
    }
    
    ?>
      
      <?php
      
     
    $url = "<img src='/WXRECORDS/images/{$item}.png' />"; 
    
    include_once('portland.html');
    ?>
    And here is the code for the HTML page
    (portland.html)

    Code:
    <!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>
          <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
          <title>Portland Climate Data</title>
          <link href="stylesheets/public.css" rel="stylesheet" type="text/css" >
       </head>
       <body style="height: 53px; color: #FFFFFF; background-color: #000000;">
          <form  action="process.php" method="post">
             <div id="banner" class="banner">Portland Oregon Climate Data </div>
             <div id="header" class="header" style="height: 35px; width: 300px" align="center">
          
    
          <!DROP DOWN #1>
    			<select name="item" style="width: 55px">
                    <?php for($i=2010; $i>=2006; $i--){?>
                      <option value="<?php echo $i?>" <?php if($i==$_POST['item'])
    				  echo 'selected="selected"'?>><?php echo $i?></option>
                    <?php }?>
                </select>   &nbsp 
    			<!BUTTON #1>
                <input name="Submit1" type="submit" value="submit" style="height: 28px" />
    			
    			
    			
    	  <!DROP DOWN #2>
    			<select name="item2" style="width: 65px">
                    <?php for($i=2010; $i>=2006; $i--){?>
                      <option value="<?php echo $i?>" <?php if($i==$_POST['item2'])
    				  echo 'selected="selected"'?>><?php echo $i?></option>
                    <?php }?>
    				<!BUTTON #2>
                <input name="Submit2" type="submit" value="submit" style="height: 28px" />
    			
                </select>   </div>
    		
    		 <br />
    		 
          </form>      <div id="content" align="center" class="content"><?php echo $url ?></div>
       </body>
    </html>

    I must admit I am embarrassed that I still have not figured out this code. I dont know why its so difficult for me. Tons of tutorials have not done the trick.

  2. #2
    Join Date
    Feb 2012
    Posts
    218
    haven't tested the code, but one thing you surely got wrong are the brackets and a condition
    Code:
    	error_reporting(E_ALL ^ E_NOTICE) ;
    	if($_POST['item']){
    		$item = $_POST['item']; 
    		// print_r ($_POST['item']); 
    	}
    	elseif(...){
    		$item = 2010; 
    		print_r ($_POST[item]);
    
    		if($_POST['item2']) 
    		$item2= $_POST['item2']; 
    	}
    	else{
    		$item2 = 2009; 
    	}

  3. #3
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30
    I tried to construct the If-then-else statement to print_r the $_POST array for these two buttons. The braces are matched up but I still get an error on line 17
    Code:
    <?php
    error_reporting(E_ALL ^ E_NOTICE) ;
    //If button 1 is clicked
    if($_POST['item']) {
      $item = $_POST['item']; 
      print_r ($_POST['item']); 
     }
    else if ($_POST['item']){
      $item = 2010; 
       $item = $_POST['item']; 
      print_r ($_POST['item']); 
       }
      //if button 2 is clicked
      if($_POST['item2']) {
      $item2= $_POST['item2']; 
        print_r ($_POST['item2'])
      }
    else {
      $item2 = 2009;
        $item2 = $_POST['item2']; 
      print_r ($_POST['item2']);
    }  
      ?>

  4. #4
    Join Date
    Feb 2012
    Posts
    218
    you forgot a semicolon
    Code:
      //if button 2 is clicked
      if($_POST['item2']) {
      $item2= $_POST['item2']; 
        print_r ($_POST['item2']);
      }
    else {

  5. #5
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30
    Thank you.. I make so many amateur mistakes.
    So now I am trying to use a switch statement. I want to make either button respond independently. One button is called 'sumbit' and the other is called 'submit2'. Obviously my inevitable goal here is to load images from the currently selected drop down. Any ideas?

    Code:
    // branch on the basis of 'Select1' value 
    switch ($_POST['item']) {
          // if Select1 => add
          case 'Submit':
           //     print_r $_POST['Select1']);
    	   $url = "<img src='/WXRECORDS/images/{$item}.png' />"; 
                break;
    
          // if Select1 => subtract
          case 'Submit2':
             //   print_r $_POST['Select1']);
    		 $url = "<img src='/WXRECORDS/images/{$item2}.png' />"; 
                break;
       
    }
    
    ?>

  6. #6
    Join Date
    Feb 2012
    Posts
    218
    Here is what you are looking for
    PHP Code:
    if($_POST['Submit1']){
        
    $url "<img src='/WXRECORDS/images/{$_POST['item']}.png' />"
    }elseif(
    $_POST['Submit2']){
        
    $url "<img src='/WXRECORDS/images/{$_POST['item2']}.png' />"
    }else{
        
    $url "<img src='/WXRECORDS/images/default_image.png' />"


  7. #7
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30
    That worked.. Thank you so much

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



Recent Articles