-2

I am new to PHP. I have a PHP file(form1.php) with an HTML form. I would like to get the value of an input box(type=time), save it to PHP variable and transfer it to another PHP page without clicking the submit button. How can I access the input and do this with just PHP, Can i trigger some event?

Form.php

 <?php

    ?>
<!doctype html>
<html lang="en">
  <head>
    <title>Form</title>

  </head>
  <body>

      <form action="file2.php" method="post">

            <input type="time" name="startTime">

      </form>

</html>
Kem
  • 29
  • 1
  • 9
  • 2
    You can't do it with just PHP. The value you want is client side, so you'll need Javascript, either by using a timer to automatically submit the form, or by calling an AJAX hit. – Alex Howansky Jan 15 '19 at 22:31
  • 1
    "can I trigger some event"...not with "just PHP" no, because any event you might need would need to occur in the browser, and PHP does not run in the browser, it runs only in the server. In order to send data to the server from a browser you need to create and send a HTTP request to the server which contains said data. To do that you can either a) submit a HTML form or b) use some JavaScript to create an AJAX request (a particular type of HTTP request only generated by browser-based script). If you don't want to press the button to submit, then that leaves b), possibly with an automatic timer – ADyson Jan 15 '19 at 22:34
  • 1
    Not wanting to press the button doesn't really make a lot of sense though - an input box is supposed to accept user input. Normally you need to wait for the user to input something before it makes any sense to send things to the server. The conventional way for a user to indicate that they've finished inputting things and are ready to submit the data is to press a "Submit" button. You could also use JavaScript to handle when the user types things (but this can result in one submission per keystroke) or when the focus moves away from the box (but there's a chance this might never happen).... – ADyson Jan 15 '19 at 22:38
  • 1
    ...and use those events as a trigger for your AJAX request. But as I've pointed out, they both have potential downsides. What's your actual underlying reason for not wanting to use a button? What are you trying to achieve exactly? Is it a case where the user has a time limit to complete their input or something? – ADyson Jan 15 '19 at 22:39

2 Answers2

1

If you want to access the value of a variable without submitting the form, then you will likely require javascript. Add a change event handler to the time input on your page. The change event handler can contact your other php page (using fetch or xhr or any number of derivatives).

All of this is very complex if you're just starting out, so for you I really recommend just waiting for the submit button to be clicked and dealing with the input at that moment.

Martin
  • 5,945
  • 7
  • 50
  • 77
-1

you will need to use javascript, otherwise, if you only want to use php, you will need a controller that checks what time is it when it received the petition, and you will need to press the button, remember php is server side, and javascript is client side. You cant pass client-side data via php

Duke
  • 19
  • 7