Solved

php pass value of html5 button

Posted on 2011-03-22
6
575 Views
Last Modified: 2012-06-27
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
Comment
Question by:newmie22
  • 3
  • 3
6 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
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
 

Author Comment

by:newmie22
Comment Utility
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
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:newmie22
Comment Utility
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
 

Author Closing Comment

by:newmie22
Comment Utility
I got it to work, thanks for your guidance
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Good, glad to help.  Thanks for the points.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now