Solved

php pass value of html5 button

Posted on 2011-03-22
6
579 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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