Home  >  Q&A  >  body text

How to get the Id of a checked circle input in php?

I'm making a php email sender and I'm having trouble. Part of the form is the radio input and the user needs to select one of them. I need to get the id of the element and write it into the email (eg: "Room Type: Room2").

<div id="roomsContainer">
        <input type="radio" name="roomType" id="Room1" data-customInfo="some information about room"/>    
        <label for="Room1" class="roomTypeLabel"><div class="roomDiv">Room1</div></label>

        <input type="radio" name="roomType" id="Room2" />    
        <label for="Room2" class="roomTypeLabel"><div class="roomDiv">Room2</div></label>
    .....
</div>

GPT provided me with this code, but it resulted in a result of "Room Type: Open". I don't know PHP myself. How can I correct the code? So the room type would show the selected input ID, or even better the selected element "data-" tag?

$roomType = $_POST['roomType'];
    $body .= "Room Type: $roomType\n";

P粉935883292P粉935883292381 days ago598

reply all(1)I'll reply

  • P粉378890106

    P粉3788901062023-09-08 00:33:37

    The purpose of the

    id attribute is to uniquely identify the element on the client. Its main purpose is to link to a specific part of the page, reference the element using the for attribute of the tag, and position it using CSS and JavaScript. It is not designed to be sent to a server. You can't get it using PHP.

    When the form is submitted to the server, the data will be derived from the name and value of the success control.

    Therefore, any data you want to send to the server should be stored in the value attribute.

    <input type="radio" name="foo" value="bar" checked>

    and

    $result = $_POST['foo'];

    reply
    0
  • Cancelreply