Home  >  Q&A  >  body text

How to retrieve input tag corresponding to numeric input type from HTML form using PHP

I have a form that contains input boxes of type number, each input box has a corresponding label. I'd like to add a "review page" after submitting the form where you can see your answers to each input box (although you don't need to fill out all of them), just like you are sorting the tags. For example, if I write 4 in the input box labeled "Mark Twain", I want to display "Choice 4: Mark Twain". I cannot change the format to <select>, where in the form each input box has a unique label.

I set each input box in HTML to:

<label><input type="number" name="num_a"  min="1" max="14" autocomplete="off" oninput="this.value = this.value.replace(/[^0-9.]/g, ''); this. Value = this.value.replace(/(\..*)\./g, ''); this.setCustomValidity('')" oninvalid="this.setCustomValidity('Please enter a number between 1 and 14')" class="unique">  <span>Mark Twain</span></label>

I tried the following code in PHP:

if (isset($_POST["num_a"]))
 {$a= "Mark Twain";
echo "Choice ", $_POST["num_a"], ":", $a, "<br>";}

This method works well when num_a is actually input. However, even if num_a is not filled in, it always appears on the review page, so it looks like this:

"Choice: Mark Twain"

What can I do to only display the input box when it has a value? So that only the input box where numbers are entered will appear?

P粉563446579P粉563446579374 days ago650

reply all(1)I'll reply

  • P粉413704245

    P粉4137042452023-09-15 11:25:11

    If the input is empty, the input will not be printed.

    if (isset($_POST['num_a']) && $_POST['num_a'] != '') {
        $a= "Mark Twain";
        echo "Choice ", $_POST["num_a"], ":", $a, "<br>";
    }

    Since 0 is not a valid input value, you can simplify to:

    if (!empty($_POST['num_a'])) {
        $a= "Mark Twain";
        echo "Choice ", $_POST["num_a"], ":", $a, "<br>";
    }

    reply
    0
  • Cancelreply