Solved

php pass value of html5 button

Posted on 2011-03-22
6
580 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 35196220
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
ID: 35196255
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 83

Expert Comment

by:Dave Baldwin
ID: 35196314
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
Industry Leaders: 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!

 

Author Comment

by:newmie22
ID: 35196432
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
ID: 35196708
I got it to work, thanks for your guidance
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 35200476
Good, glad to help.  Thanks for the points.
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

717 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