Solved

Button Click

Posted on 2016-11-09
11
22 Views
Last Modified: 2016-11-09
I have the below code to find the elapsed time between two text boxes. I have two buttons, calculate and submit. When the two text boxes are filled out, I want to be able to click calculate and have the time difference echo'd on the same page without having to leave the page or refreshing. Currently I have to click submit, then calculate, and calculate sends me to another page where I see the results. The results are correct, but the means in displaying them are not.


<form action="JobChangeTimeSheet.php" method="GET">
  Machine Stopped at:
 <input type="time" name="start_date"/>
  

End of TO:
<input type="time" name="since_start"/>  
<input type="submit"> 

Total time

<?PHP
function getTime(){
if (! empty($_GET['start_date'])){

$start_date = new DateTime($_GET['start_date']);
$since_start = $start_date->diff(new DateTime($_GET['since_start']));
echo $since_start->days.' days ';
echo $since_start->h.' hours';
echo $since_start->i.' minutes';

}
}
// delete function getTime()[ above, closing bracket, and button below to echo on page with submit
// insert time and submit then calculate works, but just calculate does not
?>
<input type="button" name="calc" onclick="document.write('<?php getTime() ?>');" value="Calculate">

</form>

 
<input type="time" name="extra1"/>  
<input type="time" name="extra2"/>  
 
 
 





 <br><br>
 
 
 
 <?PHP
if (! empty($_GET['start_date3'])){

$start_date = new DateTime($_GET['start_date3']);
$since_start = $start_date->diff(new DateTime($_GET['since_start3']));
echo $since_start->days.' days ';
echo $since_start->h.' hours';
echo $since_start->i.' minutes';

}
?>




</form>

Open in new window

0
Comment
Question by:peter Ojeda
  • 6
  • 5
11 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
This question appears to be a duplicate.  It has already been answered with a tested-and-working code example here:
https://www.experts-exchange.com/questions/28981779/Time-difference.html#a41879509

You can test the solution here:
https://iconoun.com/demo/temp_peter_ojeda_client.php

It might make more sense if you look at the application design through the understanding of client/server protocols.
https://www.experts-exchange.com/articles/11271/Understanding-Client-Server-Protocols-and-Web-Applications.html
0
 

Author Comment

by:peter Ojeda
Comment Utility
Maybe I am implementing it wrong than.. I have tried testing it in a blank .php page and keep getting an error code for undefined index alpha and omega. It does work in the example though which doesn't make sense to me
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Sigh.  The undefined index message appears because the request is missing alpha and omega arguments.  Try it on this page, like this:
https://iconoun.com/demo/temp_peter_ojeda_client.php

Client-side script:
<?php // demo/temp_peter_ojeda_client.php
/**
 * https://www.experts-exchange.com/questions/28981779/Time-difference.html
 */
error_reporting(E_ALL);


// CREATE OUR WEB PAGE IN HTML5 FORMAT, USING HEREDOC SYNTAX
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<style type="text/css">
/* STYLE SHEET HERE */
</style>

<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
    $("#go").click(function(){
        var alpha = $("input[name=alpha]").val();
        var omega = $("input[name=omega]").val();
        $.get("temp_peter_ojeda_server.php", {alpha:alpha, omega:omega}, function(resp){
            $("input[name=lapse]").val(resp)
        });
    });
});
</script>

<title>HTML5 Page With jQuery in UTF-8 Encoding</title>
</head>
<body>

<noscript>Your browsing experience will be much better with JavaScript enabled!</noscript>

<p>Enter Start and End date/time strings, then <input id="go" type="button" value="click here" />
<br>
Start: <input name="alpha" /> End: <input name="omega" /> Elapsed Minutes: <input name="lapse" readonly />
</p>
</body>
</html>
HTML5;


// RENDER THE WEB PAGE
echo $htm;

Open in new window


Server-side script:
<?php // demo/temp_peter_ojeda_server.php
/**
 * https://www.experts-exchange.com/questions/28981779/Time-difference.html
 *
 * https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
 * Note: UTC does not observe daylight savings time, so the elapsed computations will be correct throughout the year.
 */
error_reporting(E_ALL);


// DEFINE A FUNCTION TO COMPUTE ELAPSED MINUTES
function elapsed_minutes($alpha, $omega, $zone='UTC')
{
    try
    {
        $tzone      = new DateTimeZone($zone);
        $date_alpha = new DateTime($alpha, $tzone);
        $date_omega = new DateTime($omega, $tzone);
    }
    catch(Exception $e)
    {
        return $e->getMessage();
    }
    $elapsed = (int)round( ( $date_omega->format('U') - $date_alpha->format('U') ) / 60);
    return $elapsed;
}


// USE THE FUNCTION
echo elapsed_minutes( $_GET['alpha'], $_GET['omega'] );

Open in new window

If you're new to PHP and want to learn the language, this article can help.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
0
 

Author Comment

by:peter Ojeda
Comment Utility
Yea Ray the demo works fine for me. It is when I try to use the same code on my blank php page where I am having the issue.
ray.PNG
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Have you tried using the server-side script I posted with the example?  

Please show us the calendar.php script that is identified in the error message, thanks.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:peter Ojeda
Comment Utility
<?php // demo/temp_peter_ojeda_client.php
/**
 * https://www.experts-exchange.com/questions/28981779/Time-difference.html
 */
error_reporting(E_ALL);


// CREATE OUR WEB PAGE IN HTML5 FORMAT, USING HEREDOC SYNTAX
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<style type="text/css">
/* STYLE SHEET HERE */
</style>

<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
    $("#go").click(function(){
        var alpha = $("input[name=alpha]").val();
        var omega = $("input[name=omega]").val();
        $.get("temp_peter_ojeda_server.php", {alpha:alpha, omega:omega}, function(resp){
            $("input[name=lapse]").val(resp)
        });
    });
});
</script>

<title>HTML5 Page With jQuery in UTF-8 Encoding</title>
</head>
<body>

<noscript>Your browsing experience will be much better with JavaScript enabled!</noscript>

<p>Enter Start and End date/time strings, then <input id="go" type="button" value="click here" />
<br>
Start: <input name="alpha" /> End: <input name="omega" /> Elapsed Minutes: <input name="lapse" readonly />
</p>
</body>
</html>
HTML5;


// RENDER THE WEB PAGE
echo $htm;
?>
<?php // demo/temp_peter_ojeda_server.php
/**
 * https://www.experts-exchange.com/questions/28981779/Time-difference.html
 *
 * https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
 * Note: UTC does not observe daylight savings time, so the elapsed computations will be correct throughout the year.
 */
error_reporting(E_ALL);


// DEFINE A FUNCTION TO COMPUTE ELAPSED MINUTES
function elapsed_minutes($alpha, $omega, $zone='UTC')
{
    try
    {
        $tzone      = new DateTimeZone($zone);
        $date_alpha = new DateTime($alpha, $tzone);
        $date_omega = new DateTime($omega, $tzone);
    }
    catch(Exception $e)
    {
        return $e->getMessage();
    }
    $elapsed = (int)round( ( $date_omega->format('U') - $date_alpha->format('U') ) / 60);
    return $elapsed;
}


// USE THE FUNCTION
echo elapsed_minutes( $_GET['alpha'], $_GET['omega'] );
?>

Open in new window

0
 

Author Comment

by:peter Ojeda
Comment Utility
I posted the code. I am just trying to use your code so I can understand how it works and hopefully implement it in my project.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
Comment Utility
These should be two separate scripts.  One of them (client) makes a request to the other (server).  But they can't work that way if they are inside the same script file.  Try separating them the way they are shown in the code snippets, install them separately, and give it another go.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
Comment Utility
Also, please remove the "close-PHP" tags.  These are omitted on purpose and for a reason, described in this article.
https://www.experts-exchange.com/articles/12293/AntiPHPatterns-and-AntiPHPractices.html
0
 

Author Comment

by:peter Ojeda
Comment Utility
Ray, thank you very much. That was the issue. I really appreciate you taking your time and being patient with me, I am very new to php and am still learning. It works great. I will be sure to read up more on your articles. Thank you again.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points and thanks for using E-E!  Feel free to post lots of questions here -- we have many experts and several good articles to help you move forward.  Cheers, ~Ray
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

763 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

10 Experts available now in Live!

Get 1:1 Help Now