Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

echo button

Posted on 2016-10-27
13
Medium Priority
?
71 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
12 Comments
 
LVL 111

Expert Comment

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

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
 
LVL 1

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Brian Tao
ID: 41862654
I don't see your button in the code you posted.  Please add them.
1
 
LVL 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 27

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
 
LVL 111

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 60

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 27

Accepted Solution

by:
skullnobrains earned 2000 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
 
LVL 1

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 27

Expert Comment

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

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month20 days, 20 hours left to enroll

810 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