Solved

echo button

Posted on 2016-10-27
13
47 Views
Last Modified: 2016-11-23
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?
0
Comment
Question by:peter Ojeda
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41862442
Maybe start by showing us the code you're using now.  It might be fixable :-)
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 41862597
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
 

Author Comment

by:peter Ojeda
ID: 41862608
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
 
LVL 9

Expert Comment

by:Brian Tao
ID: 41862654
I don't see your button in the code you posted.  Please add them.
1
 

Author Comment

by:peter Ojeda
ID: 41862662
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
 
LVL 26

Expert Comment

by:skullnobrains
ID: 41862746
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41862786
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
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41862933
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
 
LVL 26

Accepted Solution

by:
skullnobrains earned 500 total points
ID: 41863514
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
 

Author Comment

by:peter Ojeda
ID: 41863985
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
 
LVL 26

Expert Comment

by:skullnobrains
ID: 41871616
post your code
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41871815
Sounds like a new question.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

17 Experts available now in Live!

Get 1:1 Help Now