?
Solved

php pass value of html5 button

Posted on 2011-03-22
6
Medium Priority
?
589 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 1500 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
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

741 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