0

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.

5
  • Change the custom value input's name to something else e.g tid-custom, when checking the inputs' value, if the $_POST['tid'] is empty, get the value of $_POST['tid-custom'] Commented Mar 7, 2019 at 23:08
  • Makes sense :) But how do i check if "tid" is empty and if it is get the value of "tid-custom" ? Commented Mar 7, 2019 at 23:11
  • PHP has a function called empty or isset and use some flow control like a if statement.. Also disabling warning/errors/warnings in PHP with @ is considerd bad practice Commented Mar 7, 2019 at 23:15
  • Ideally you should use isset or empty to check a variable and initialise with default value. PHP's ternary operator is the best suited for this $baatnr = isset($_POST["baatnr"]) ? $_POST["baatnr"] : NULL in PHP 7 its even more easy $baatnr = $_POST["baatnr"] ?? NULL see sections Ternary Operator and Null Coalescing Operator Commented Mar 7, 2019 at 23:22
  • Please be aware that the code presented here is vulnerable to SQL injection attacks and should never be used in a website where anyone can submit the form. Commented Mar 7, 2019 at 23:27

2 Answers 2

0

You can add another radio button to represent the entry of a custom value and change the name of the text input as follows:

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>
    <label class="container"><span class="tidtekst">Custom Value</span>
        <input type="radio" name="tid" value="custom">
        <span class="checkmark"></span>
    </label>
    <input type="text" name="tid_custom" id="egentid" placeholder="Tid">
</div>

PHP

if (@$_POST["submit"] != "") {
    $baatnr = @$_POST["baatnr"];
    $fornavn = @$_POST["fornavn"];
    $etternavn = @$_POST["etternavn"];
    $tid = @$_POST["tid"];

    if ($tid == 'custom') {
        $tid = @$_POST["tid_custom"];
    }

    $kr = @$_POST["kr"];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I had to remove the radio button "custom value" and change the $tid == 'custom' to $tid == null to make it work :)
0

Try

$_POST['tid']

without @ & simple quotes ' '

Also check if your method is POST in your form HTML method="POST" & Change the name of the input to another, for example, 'tid_result'

2 Comments

@ disables notices/errors/warnings it does not check annything.
Why double quote is the problem here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.