Link to home
Start Free TrialLog in
Avatar of peter Ojeda
peter Ojeda

asked on

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?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Maybe start by showing us the code you're using now.  It might be fixable :-)
Avatar of skullnobrains
skullnobrains

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
Avatar of peter Ojeda

ASKER

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.
I don't see your button in the code you posted.  Please add them.
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>
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
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

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
ASKER CERTIFIED SOLUTION
Avatar of skullnobrains
skullnobrains

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
post your code
Sounds like a new question.