?
Solved

Button Click

Posted on 2016-11-09
11
Medium Priority
?
41 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
[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
  • 6
  • 5
11 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41880773
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
 
LVL 1

Author Comment

by:peter Ojeda
ID: 41881200
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 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 41881265
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:peter Ojeda
ID: 41881274
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 111

Expert Comment

by:Ray Paseur
ID: 41881283
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
 
LVL 1

Author Comment

by:peter Ojeda
ID: 41881285
<?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
 
LVL 1

Author Comment

by:peter Ojeda
ID: 41881286
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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 2000 total points
ID: 41881298
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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 2000 total points
ID: 41881304
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
 
LVL 1

Author Comment

by:peter Ojeda
ID: 41881308
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 111

Expert Comment

by:Ray Paseur
ID: 41881337
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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.
Suggested Courses

777 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