echo button

I am writing a form where the user is able to calculate the time between two tasks, echo the results, and then submit those results with the rest of the form. The issues is that every time I click the button to echo results, the form refreshes. Any way to prevent this from happening?
LVL 1
peter OjedaAsked:
Who is Participating?
 
skullnobrainsConnect With a Mentor Commented:
correcting my own code

<?php

print '<form action="?" method="GET">';
	

print_r($_GET);

if($_GET['start'])
  print '<input type="hidden" name="start_date" value="'.time().'" />'
  and print '<input type="submit" value="stop" name="stop" />';
else
  print '<input type="submit" name="start" value="start" />';

$_GET['stop']
and $_GET['start_date']
and print "elapsed time in seconds : ".(time()-$_GET['start_date']);


print '</form>';

Open in new window


fiddled on phptester.net
works
0
 
Ray PaseurCommented:
Maybe start by showing us the code you're using now.  It might be fixable :-)
0
 
skullnobrainsCommented:
2 approaches there.

either you want the page to "remember" the previous anfo and you need to use ajax queries.

or likely more simple in your case, you must keep the information from one query to the next.

without bothering with sessions, why don't you post the information of the first form along with the second.

this can be done quite trivially using something like this
action='?'.http_build_query($_REQUEST)
which will populate whatever the previous form contained in $_GET

or in a slightly more complex way by generating hidden fields with the information you want to pass along
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
peter OjedaAuthor Commented:
Here is my code
<form action="JobChangeTimeSheet.php" method="GET">
  <br>Beginning of T3
<input type="text" name="start_date2"/>
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End of T3
<input type="text" name="since_start2"/>

 
<?PHP
if (! empty($_GET['start_date2'])){

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

}
?>

</form>


I want to find the difference between the two times in the textbox on the click of the button, but i also have more text boxes in the form after this so I do not want to refresh the page after executing the php.
0
 
Brian TaoSenior Business Solutions ConsultantCommented:
I don't see your button in the code you posted.  Please add them.
1
 
peter OjedaAuthor Commented:
Sorry put the wrong code in last time


<form action="JobChangeTimeSheet.php" method="GET">
  Machine Stopped at:
 <input type="text" name="start_date"/>
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
End of TO:
<input type="text" name="since_start"/>  
<input type="submit">

Total time

<?PHP
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';

}
?>
</form>
0
 
skullnobrainsCommented:
something like this should do

<form action="JobChangeTimeSheet.php" method="GET">
<?
if($_GET['start'])
  print '<input type="hidden" name="start_date" value="'.time().'" />'
  and print '<input type="submit" value="stop" name="stop" />';
else
  print '<input type="submit" name="start" value="start" />';

$_GET['stop']
and $_GET['start']
and print "elapsed time in seconds : ".($_GET['stop']-$_GET['start']);

</form> 

Open in new window


you probably want to add better error handling and an option to start again though, and maybe a nice js counter with the elapset time incrementing
0
 
Ray PaseurCommented:
In case you're new to PHP and want to learn the language, this article can help you find dependable learning resources.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

The nature of the HTTP client/server protocol may be in play here.  From the look of things you might need two separate scripts.  One is the HTML document that has the form.  The other is the action= script that responds to the request from the HTML form.  You can combine these two scripts into one script file - just omit the action= attribute from the form tag and the request from the form submit will be sent to the current URL.

Here is how I might get started, if I understand your objectives correctly.
<?php // demo/temp_peter_ojeda.php
/**
 * https://www.experts-exchange.com/questions/28979329/echo-button.html#a41862608
 *
 * https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
 */
error_reporting(E_ALL);


if (!empty($_GET))
{
    $alpha = new DateTime($_GET['a']);
    $omega = new DateTime($_GET['z']);
    $d = $omega->diff($alpha)->format('%d days, %h hours, %i minutes');
    print_r($d);
}

// HEREDOC NOTATION
$form = <<<EOF
<form>
Alpha: <input name="a" /><br>
Omega: <input name="z" /><br>
<input type="submit" />
</form>
EOF;

echo $form;

Open in new window

0
 
Julian HansenCommented:
You can also calculate in the browser using JavaScript and display that on the form before submitting
HTML
    <form class="form-horizontal" action="reflect.php" method="post">
      <label class="col-md-2">Machine stopped at</label>
      <div class="col-md-4">
        <input type="text" name="start_date" id="startdate"/>
      </div>
      <label class="col-md-2">End of TO</label>
      <div class="col-md-4">
        <input type="text" name="since_start" id="sincestart" />
      </div>
      <div class="col-md-6">
        <span id="days"></span> days<br/>
        <span id="hours"></span> hours<br/>
        <span id="minutes"></span> minutes<br/>
      </div>
    </form>

Open in new window

JavaScript / jQuery
<script>
$(function() {
  $('#startdate, #sincestart').blur(function() {
    var startdate = $('#startdate').val();
    var sincedate = $('#sincestart').val();
    if (startdate && sincedate) {
      var diff = (new Date(startdate) - new Date(sincedate)) / 1000;
      var days = Math.floor(diff/86400);
      diff -= (days * 86400);
      var hours = Math.floor(diff/3600);
      diff -= (hours * 3600);
      var minutes = Math.floor(diff/ 60);
      $('#days').html(days);
      $('#hours').html(hours);
      $('#minutes').html(minutes);
    }
  });
});
</script>

Open in new window

You can see a sample of this working here
1
 
peter OjedaAuthor Commented:
Thankyou all very much. I ended up just using submit to submit the results to the database rather than displaying the results on the page. The issue that I am working on now is submitting the result to the sqlserver database.All of my other fields have worked other than the results, so I am guessing that it has something to do with defining the echo'd result.
0
 
skullnobrainsCommented:
post your code
0
 
Julian HansenCommented:
Sounds like a new question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.