0

I created a simple form in html and a button to send that form to a database. I fill the form, start the timer and after stop it I send the information to a database. My problem is that I can't send the actual time to the database.

My HTML form code:

<form action="db.php" method="POST">
                    <div class="form-group">
                        <label>Referência Máquina</label>
                        <input type="text" name="refMaq" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <label>Nome Funcionário</label>
                        <select name="nomeFunc">
                            <option>selecionar funcionário</option>
                            <option value="joao">João</option>
                            <option value="carlos">Carlos</option>
                            <option value="miguel">Miguel</option>
                            <option value="rui">Rui</option>
                        </select>
                    </div>
                        <label>Contador</label>
                        <div name="form-group">
                            <span name="hours" id="hours">00:</span>
                            <span name="mins" id="mins">00:</span>
                            <span name="seconds" id="seconds">00</span> 
                        </div>
                    <input type="submit" class="btn btn-primary" name="submit" value="Submit" required>
</form>

PHP code (I tried this way but it only writes the 00 values, not the actual value of the timer):

$htmlSec = "<span name='seconds' id='seconds'>00</span>";
$domdocSec = new DOMDocument();
$domdocSec->loadHTML($htmlSec);
$contSec = $domdocSec->getElementById('seconds')->nodeValue;

$htmlMins = "<span name='mins' id='mins'>00:</span>";
$domdocMin = new DOMDocument();
$domdocMin->loadHTML($htmlMins);
$contMin = $domdocMin->getElementById('mins')->nodeValue;

$htmlHours = "<span name='hours' id='hours'>00:</span>";
$domdoch = new DOMDocument();
$domdoch->loadHTML($htmlHours);
$contH = $domdoch->getElementById('hours')->nodeValue;

$refMaq = $_POST['refMaq'];
$nomeFunc = $_POST['nomeFunc'];
$contador= $contH. $contMin. $contSec;
$sql = "INSERT INTO db_teste (refMaq, nomeFunc, contador)
VALUES ('$refMaq','$nomeFunc','$contador')";

I also tried something simple like this but it doesn't write anything at all:

$hours = $_REQUEST['hours'];
$mins = $_REQUEST['mins'];
$seconds = $_REQUEST['seconds'];
$refMaq = $_POST['refMaq'];
$nomeFunc = $_POST['nomeFunc'];
$contador=$hours. $mins. $seconds;
$sql = "INSERT INTO db_teste (refMaq, nomeFunc, contador)
VALUES ('$refMaq','$nomeFunc','$contador')";
Turiacus
  • 1
  • 1
  • 1
    Nothing in the form you have shown us, would submit any timer values. Span elements are not form elements, not even if you add a fake `name` attribute to them. – CBroe Jan 24 '22 at 14:10
  • _"I tried this way but it only writes the 00 values"_ - of course it does, because you are reading directly _from_ `00`, which contained that _static_ value. How should that `00` have changed to anything else, in these four lines of code ...? Assuming this timer you are talking about is a client-side implementation, this attempt makes zero sense to begin with. – CBroe Jan 24 '22 at 14:13
  • This actually sounds like you should probably go and have a good, thorough read of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/q/13840429/1427878) first of all ... – CBroe Jan 24 '22 at 14:14
  • Yeah, you are right that thing was meant to fail because I have the value "00" in there. Thanks for your input, I now understand that a span element is not a form element so it's impossible to do it this way. – Turiacus Jan 24 '22 at 14:27
  • You can either replace those spans with text input fields to begin with (will probably require only minor changes in your timer script, setting the field's `value` instead of `innerText`/`innerHTML` of the spans), or you can put your timer values into extra hidden form fields the moment the timer gets stopped/the form gets submitted ... Then you will be able to access them on the server side after submitting the form. – CBroe Jan 24 '22 at 14:39

0 Answers0