#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    24
    Rep Power
    0

    Redirect back to previous page with drop down box selection equal to session variabl


    Hi all.

    I have 2 files: FieldSuperDataEntry.php and ProcessFieldSuperDataEntry.php

    When the end user enters data in the data entry php page one of the fields they select is a builder---community from a drop down list. When they click the submit button it then goes to the ProcessFieldSuperDataEntry.php file where it inserts the data into a table, and at the end of that file it does a directs the user to the Menu.php file (header('Location: Menu.php'), at the end of the ProcessFieldSuperDataEntry.php file.

    Instead I want to go back to the FieldSuperDataEntry.php file with the builder---community they had previously selected to be already selected in the drop down list, the rest of the data fields should be blank though.

    How can I do this?

    Thank you in advance.

    FieldSuperDataEntry.php:

    PHP Code:
    <?php 
     
        
    require("common.php");

    if(!
    $_SESSION['user']){
     
    header("Location: index.php");
     exit();
    }

            
          
    $smt1 $db1->prepare('SELECT FieldSuperAssignment.BuilderCommunityID,Builder, Community FROM BuilderCommunity INNER JOIN FieldSuperAssignment ON BuilderCommunity.BuilderCommunityID = FieldSuperAssignment.BuilderCommunityID   WHERE UserID = :user_id AND BuilderCommunity.Status = "Active" ORDER BY Builder, Community');

          
    $smt1->execute(array(':user_id' => $_SESSION['user']['userid']));
          
    $data1 $smt1->fetchAll();
          
          
    $smt $db-> prepare('SELECT jobtypeid, jobtypedescription FROM JobType ORDER BY jobtypeid');
          
    $smt->execute();
          
    $data $smt->fetchAll();


    $_SESSION['action_token'] = generate_secure_token(); 
    ?> 

    <!DOCTYPE html>
    <head>
      <title>MyApp</title>
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
      <link rel="stylesheet" href="style.css" type="text/css" />
      <link href="iphone-icon1.png" rel="apple-touch-icon">
      <script>

    function getLocation()
      {
      var options = {
      enableHighAccuracy: true,
      timeout: 60000,
      maximumAge: 0
    };
      
      if (navigator.geolocation)
        {
            navigator.geolocation.getCurrentPosition(showPosition);
        }
      else{x.innerHTML="Geolocation is not supported by this browser.";}
      }
    function showPosition(position)
      {  
      document.getElementById('Lat').value =position.coords.latitude
      document.getElementById('Long').value =position.coords.longitude
      }
      
      function checkFields()
      {
       var x = document.getElementById('lot').value;
       var y = document.getElementById('block').value;
        
        if((x=="" || x==" ") && (y=="" || y==" "))
        {
            alert("Please enter a lot and block number");
            return false;
        }
        
        else if(x=="" || x==" ")
        {
            alert("Please enter a lot number");
            return false;
        }
        
        else if(y=="" || y==" ")
        {
            alert("Please enter a block number");
            return false;
        }
            
        else return true;
      }
    </script>

    </head>
    <body onload="getLocation()">
    <?php echo "User: " $_SESSION['user']['Name'?>
    <div class="wrapper">
        <div id="logo"></div>
        
       <form class="form4" action="ProcessFieldSuperDataEntry.php" method="post">
    <div class="formtitle4">Data Entry</div>

                
               
                <div class="input2">
                     <div class="inputtext">Builder/Community:</div>           
                         <div class="inputcontent" ><select name="BuilderCommunity" id="BuilderCommunity" style="width: 250px"><option selected="selected"></option>    
                    
                    <?php foreach($data1 as $row) { printf("<option value='%s'>%s --- %s </option>"html_escape($row['BuilderCommunityID']), html_escape($row['Builder']), html_escape($row['Community'])); }?>
                         </select></div>
    <br>
                    <br>
                    <br>
               
                    <div class="inputtext">Lot:</div> 
                    <div class="inputcontent" ><input name="lot" id="lot" input type="text" />
                   
                    </div>
                    
                    <div class="inputtext">Block:</div>
                   <div class="inputcontent" >

                        <input name="block" id="block" input type="text" />

                    </div>
                   
              </div>  
                
                <div class="input2"> 
                    <div class="inputtext">Type:</div>           
                         <div class="inputcontent" ><select name="type" id="type" style="width: 20em;"><option selected="selected"> </option>    
                         <?php foreach($data as $row) { printf("<option value='%s'>%s</option>"html_escape($row['jobtypeid']), html_escape($row['jobtypedescription']) ); }?>

                             </select>
                         </div>
                     
      
                </div>            
                

                <div class="input nobottomborder">
                    <div class="inputtext">Notes: </div>
                    <div class="inputcontent">
                        <textarea name="notes" id = "notes" rows = "3" cols = "80"></textarea>                                
                    </div>
                </div>
            <input type="hidden" name="action_token" value="<?php echo html_escape($_SESSION['action_token']) ?>" />
            <input type = "hidden" input name = "Lat" input id = "Lat"><br>
            <input type = "hidden" input name = "Long" input id = "Long"><br>
         
                <div class="buttons" align = center>

                    <input class="button1"  type="submit" value="Save" onclick="return checkFields()" />
                    <input class="button1"  type="submit" formaction="Menu.php" value="Menu" /> 

            
                </div>


    </form>
    </body>
    </html>
    ProcessFieldSuperDataEntry.php:

    PHP Code:
    <?php 
       
        
    require("common.php");

     
    $date = new DateTime();
                  
    $date->setTimezone(new DateTimeZone('America/New_York'));
                  
    $fdate $date->format('Y-m-d H:i:s');
                  
    $date2 = new DateTime();  // This date and time variable will be used to display it to the end user as 3:50 PM but it will be saved in the table as a 24 hour format in $fdate
                  
    $date2->setTimezone(new DateTimeZone('America/New_York'));
                  
    $fdate2 html_escape($date2->format('m/d/Y h:i:s A')); 
      
    $query 
                SELECT 
                    1 
                FROM FieldSuperDataEntry 
                WHERE 
                    BuilderCommunityID = :buildercommunityid AND Lot = :lot AND Block = :block
            "


      
    $query_params = array( 
                
    ':buildercommunityid' => $_POST['BuilderCommunity'] ,
                
    ':lot' => $_POST['lot'],
                
    ':block' => $_POST['block']
            ); 
            
               
    $stmt $db->prepare($query); 
                
    $result $stmt->execute($query_params); 
                
                
    $row $stmt->fetch();
                
            if(
    $row
            { 
      
    $query 
                SELECT 
                    1 
                FROM FieldSuperDataEntry 
                WHERE 
                    BuilderCommunityID = :buildercommunityid AND Lot = :lot AND Block = :block
            "


      
    $query_params = array( 
                
    ':buildercommunityid' => $_POST['BuilderCommunity'] ,
                
    ':lot' => $_POST['lot'],
                
    ':block' => $_POST['block']
            ); 
            
               
    $stmt $db->prepare($query); 
                
    $result $stmt->execute($query_params); 
                
                
    $row $stmt->fetch();
                
            if(
    $row
            { 
      
    $query 
                SELECT 
                    1 
                FROM FieldSuperDataEntry 
                WHERE 
                    BuilderCommunityID = :buildercommunityid AND Lot = :lot AND Block = :block
            "


      
    $query_params = array( 
                
    ':buildercommunityid' => $_POST['BuilderCommunity'] ,
                
    ':lot' => $_POST['lot'],
                
    ':block' => $_POST['block']
            ); 
            
               
    $stmt $db->prepare($query); 
                
    $result $stmt->execute($query_params); 
                
                
    $row $stmt->fetch();
                
            if(
    $row
            { 
     echo 
    '<script type="text/javascript">alert("That Builder/Community/Lot/Block has already been entered.");</script>'
              echo 
    '<script type="text/javascript">window.location.href = "http://www.myapp.com";</script>';

      exit();
            }

            
            
    $query 
                INSERT INTO FieldSuperDataEntry ( 
                    BuilderCommunityID, 
                    Lot, 
                    Block,
                    Type,
                    Latitude,
                    Longitude,
                    Notes,
                    UserID,
                    CreateDate,
                    ModifiedDate
                ) VALUES ( 
                    :buildercommunityid, 
                    :lot, 
                    :block,
                    :type,
                    :latitude,
                    :longitude,
                    :notes,
                    :userid,
                    :currentdatetime,
                    :modifieddatetime
                 ) 
            "

       
    $query_params = array( 
                
    ':buildercommunityid' => $_POST['BuilderCommunity'], 
                
    ':lot' => $_POST['lot'], 
                
    ':block' => $_POST['block'], 
                
    ':type' =>$_POST['type'],
                
    ':latitude' => $_POST['Lat'],
                
    ':longitude' => $_POST['Long'],
                
    ':notes' => $_POST['notes'],
                
    ':userid' => $_SESSION['user']['userid'] ,
                
    ':currentdatetime' => $fdate,
                
    ':modifieddatetime' => $fdate
              
    ); 
             
         
                
    // Execute the query to insert data into the FieldSuperDataEntry table 
                
    $stmt $db->prepare($query); 
                
    $result $stmt->execute($query_params);
            unset( 
    $_SESSION['action_token'] );
            
    header('Location: Menu.php');
            exit();
    ?>
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    store the selection in the session or a cookie, redirect the user to the page and then restore the selection.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    24
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    store the selection in the session or a cookie, redirect the user to the page and then restore the selection.
    In the ProcessFieldSuperDataEntry.php file I created the session and at the end I redirect:

    PHP Code:
    $_SESSION['buildercommunityid'] = $_POST['BuilderCommunity'];

     
    header('Location: FieldSuperDataEntry.php');
            exit(); 
    Then in the FieldSuperDataEntry.php file I'm having trouble setting the selection in the drop down, it does not select my session variable, it just selects the first entry in the drop down box:

    PHP Code:
           <div class="inputtext">Builder/Community:</div>           
                         <div class="inputcontent" ><select name="BuilderCommunity" id="BuilderCommunity" style="width: 250px">   
                    
                      <?php foreach($data1 as $row) { printf("<option value='%s'>%s --- %s </option>"html_escape($row['BuilderCommunityID']), html_escape($row['Builder']), html_escape($row['Community'])); }?>
                         </select></div>
  6. #4
  7. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,112
    Rep Power
    487
    don't forget to include the line

    session_start();

    at the top of your pages
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    24
    Rep Power
    0
    Got it working:

    PHP Code:
        <option selected="selected"></option>    
    <?php foreach($data1 as $row) : 
        
    $selected = ($row['BuilderCommunityID'] == $buildingcommunity) ? 'selected="selected"' '';
        
    printf("<option value='%s'%s>%s --- %s </option>"html_escape($row['BuilderCommunityID']), $selectedhtml_escape($row['Builder']), html_escape($row['Community'])); 
    endforeach;
    ?>
      </select>

IMN logo majestic logo threadwatch logo seochat tools logo