I have made a form where you can either select "30" or "60" or type in your custom value.
When I submit the form after I chose for example "30", it will send an empty value to the database cause the input field was empty.
How do I make it so it will take the input value of the radio buttons if the input is empty but take the custom input value if something is written in it
html:
<div class="tid">
<label class="container"><span class="tidtekst">30m</span>
<input type="radio" class="checked" name="tid" id="tid" value="30">
<span class="checkmark"></span>
</label>
<label class="container"><span class="tidtekst">60m</span>
<input type="radio" name="tid" value="60">
<span class="checkmark"></span>
</label>
<input type="text" name="tid" id="egentid" placeholder="Tid">
</div>
Php:
if (@$_POST["submit"] != "") {
$baatnr = @$_POST["baatnr"];
$fornavn = @$_POST["fornavn"];
$etternavn = @$_POST["etternavn"];
$tid = @$_POST["tid"];
$kr = @$_POST["kr"];
}
$sql = "INSERT INTO utleie (utleid, baatnr, fornavn, etternavn, tid, kr)
VALUES ('$utleidtid', '$baatnr', '$fornavn', '$etternavn', '$tid', '$kr')";
Here it will take the input value of "tid" but if I don't type anything in the input field it will send nothing to the database.
tid-custom, when checking the inputs' value, if the$_POST['tid']is empty, get the value of$_POST['tid-custom']@is considerd bad practice$baatnr = isset($_POST["baatnr"]) ? $_POST["baatnr"] : NULLin PHP 7 its even more easy$baatnr = $_POST["baatnr"] ?? NULLsee sections Ternary Operator and Null Coalescing Operator