Solved

Button Click

Posted on 2016-11-09
11
31 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
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 108

Accepted Solution

by:
Ray Paseur earned 500 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
 
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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
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.

912 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

22 Experts available now in Live!

Get 1:1 Help Now