• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 608
  • Last Modified:

php pass value of html5 button

I have a form that is keeping time of how long the user spends on training pages and updates the database each time.  However, i'm having troubles getting it to then direct them to the link they wanted to go to after the time is entered into the database.

I got the elapsedTime through a hidden field on each page and also used that to find out where they had been, but i tried to use the value of the button field and after entering time into the database i get brought to the blank pass through page instead of whatever link was clicked on.  
<?php
session_start();
?>

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
        <meta name="viewport" content="width=device-width; height=device-height; initial-scale=1;">
        
        <title>Digital Natives Testing Module</title>
        
        <link rel="stylesheet" type="text/css" href="styles.css" />
               
        <!--[if IE]>
        
        <style type="text/css">
        .clear {
          zoom: 1;
          display: block;
        }
        </style>

        
        <![endif]-->
        
    </head>
    
    <script>
	  var currentTime = new Date();
	  var startTime = currentTime.getTime();
	  
	  function getElapsedTime()
	  {
		  var endTime = new Date();
		  var elapsedTime = ((endTime.getTime() - startTime) / 1000);
		  document.getElementById('elapsedTime').value = elapsedTime;
		  document.forms['form1'].submit();
	  }
	</script>
    
    <body>
    
		<form name="form1" id="training" method="post" action="doTrainingInsert.php">
		   <input type="hidden" name="elapsedTime" id="elapsedTime" />
           <input type="hidden" name="lastpage" value= "overheat"/>
           
    	<section id="page"> <!-- Defining the #page section with the section tag -->
    
            <div class="header"> <!-- Defining the header section of the page with the appropriate tag -->

                <h1>Test A</h1>
                <p>&nbsp;</p>
                <nav> <!-- The nav link semantically marks your main site navigation -->
              		<ul>
                        <li>
                          <table border="0" cellpadding="0" cellspacing="0"> 
                          <tr> 
                            <td>                                                          
                              <button name="nextpage" value="submitAoverheat" onclick="getElapsedTime()">OVERHEAT</button>
                            </td> 
                            <td> 
                              <button name="nextpage" value="submitAjammed" onclick="getElapsedTime()">JAMMED</button>
                            </td>
                            <td>
                              <button name="nextpage" value="submitAcontinue" onclick="getElapsedTime()">CONTINUE</button>
                            </td>
                            <td>
                              <button name="nextpage" value="submitAstop" onclick="getElapsedTime()">STOP</button>
                            </td>
                            <td>
                              <button name="nextpage" value="submitAfinished" onclick="getElapsedTime()">FINISHED</button>
                            </td> 
                          </tr>
                        </table>
                        </li>

                    </ul>
                </nav>
            
            </div>
            
            <section id="articles"> <!-- A new section with the articles -->

				<!-- Article 1 start -->

                <div class="line"></div>  <!-- Dividing line -->
                
                <div class="article" id="article1"> <!-- The new article tag. The id is supplied so it can be scrolled into view. -->
                    <h2>overheat</h2>
                    
                    <div class="line"></div>
                    
                    <div class="articleBody clear">
                    
                    <div class="figure"> <!-- The figure tag marks data (usually an image) that is part of the article -->
	                    	<img src="img/a_overheat.png" alt="overheat" />
                    </div>
                    
                      <p>If the machine is producing too rapidly, it may result in overheating. When the machine displays a square, this indicates that it is overheating and needs to be slowed down.</p>
                      
                      
                      
                  </div>
                </div>
                
				<!-- Article 1 end -->


            </section>

        <footer> <!-- Marking the footer section -->

          <div class="line"></div>
        
        </footer>
            
		</section> <!-- Closing the #page section -->
     </form>   
    </body>
</html>

Open in new window

<?php
	
	// start the session
	session_start();
	
	// connect to the databse
	include("includes/openDbConn.php");
	
	$trainingTime = $_POST["elapsedTime"];
	$lastPage = $_POST["lastpage"];
	$nextPage = $_POST["nextpage"];
	
	
	
	if (($lastPage) == "overheat")
	{
		$sql = "UPDATE Users SET trainOverheat = trainOverheat + '".$trainingTime."' WHERE userID='".$_SESSION["username"]."'";
		//echo $sql; // to check 
		$result = mysql_query($sql);
		exit;
	}else
	
	if (($lastPage) == "jammed")
	{
		$sql = "UPDATE Users SET trainJammed = trainJammed + '".$trainingTime."' WHERE userID='".$_SESSION["username"]."'";
		//echo $sql; // to check 
		$result = mysql_query($sql);
		exit;
	}else
	
	if (($lastPage) == "continue")
	{
		$sql = "UPDATE Users SET trainContinue = trainContinue + '".$trainingTime."' WHERE userID='".$_SESSION["username"]."'";
		//echo $sql; // to check 
		$result = mysql_query($sql);
		exit;
	}else
	
	if (($lastPage) == "stop")
	{
		$sql = "UPDATE Users SET trainStop = trainStop + '".$trainingTime."' WHERE userID='".$_SESSION["username"]."'";
		//echo $sql; // to check 
		$result = mysql_query($sql);
		exit;
	}
	
	
	
	
	if (($nextPage) == "submitAoverheat")
	{
			header("Location: testA-overheat.php");
			exit;
	}else
	
	if (($nextPage) == "submitAjammed")
	{
			header("Location: testA-jammed.php");
			exit;
	}else
	
	if (($nextPage) == "submitAcontinue")
	{
			header("Location: testA-continue.php");
			exit;
	}else
	
	if (($nextPage) == "submitAstop")
	{
			header("Location: testA-stop.php");
			exit;
	}
	
	// close database connection
	include("includes/closeDbConn.php");

?>

Open in new window

0
newmie22
Asked:
newmie22
  • 3
  • 3
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
From this page http://us2.php.net/manual/en/function.header.php :

"HTTP/1.1 requires an absolute URI as argument to ยป Location: including the scheme, hostname and absolute path, but some clients accept relative URIs."

That means that "testA-overheat.php" may need to be "http://www.yoursite.com/testA-overheat.php".

Also, all of your MySQL statements are followed by 'exit;'.  That stops the script right there, nothing more happens after you reach that statement.
0
 
newmie22Author Commented:
I thought using exit stopped that part of the code? I've used it before to validate and then exited and continued on to the other if statements.

Is there a more efficient way to accomplish what i need?  I want it to check where they've been and update the time depending on what page they were on and then redirect based on what link they clicked.  I only know the basics so i'm sure i'm trying to accomplish things in an elementary way.
0
 
Dave BaldwinFixer of ProblemsCommented:
No, 'exit;' stops the current script.  Completely.  http://us.php.net/manual/en/function.exit.php  They are recommended to be used after the "header("Location: xxxxxrrrrr.php");" statements.

Also, your 'else if's are not formatted correctly.  http://us.php.net/manual/en/control-structures.elseif.php  I don't think you need any of the 'else's anyway.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
newmie22Author Commented:
I've reorganized and realized through echoing variables that the buttons are not passing their value and the page doesn't know where to go because of that, it was before and i have no idea why its not.
0
 
newmie22Author Commented:
I got it to work, thanks for your guidance
0
 
Dave BaldwinFixer of ProblemsCommented:
Good, glad to help.  Thanks for the points.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now