skip to Main Content

I am doing php email sender and i got difficulties. One of the form’s parts is radio inputs where user need to choose one. I need to get the id of element and write it in the email (for example: "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 offering me this code but it leads to result of "Room Type: on". I don’t know PHP myself. How can i correct the code? So Room Type would display chosen input ID or even better, chosen element ‘data-‘ tag?

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

2

Answers


  1. To get the ID or data attribute value of the checked radio input in PHP, you can modify the code as follows:

        if(isset($_POST['roomType'])) {
        $roomType = $_POST['roomType'];
        // If you want to display the ID
        $body .= "Room Type: $roomTypen";
    
        // If you want to display the data attribute value
        $customInfo = $_POST['roomType_customInfo'];
        $body .= "Room Type: $customInfon";
    }
    

    In your HTML code, you can add a data attribute to each radio input containing the custom information. Then, in the PHP code, you can access the custom information using $_POST[‘roomType_customInfo’]. Replace ‘customInfo’ with the appropriate data attribute name you used in your HTML code.

    Here’s an example of how you can modify the HTML code:

    <div id="roomsContainer">
        <input type="radio" name="roomType" id="Room1" data-customInfo="some information about room1"/>    
        <label for="Room1" class="roomTypeLabel"><div class="roomDiv">Room1</div></label>
    
        <input type="radio" name="roomType" id="Room2" data-customInfo="some information about room2"/>    
        <label for="Room2" class="roomTypeLabel"><div class="roomDiv">Room2</div></label>
        <!-- Add data-customInfo attribute with the desired information -->
        <!-- Replace "customInfo" with the appropriate data attribute name -->
    </div>
    

    Make sure that the modified PHP code is executed after the form submission, and the radio input values are sent via the POST method.

    Login or Signup to reply.
  2. The purpose of the id attribute is to uniquely identify an element on the client with the main uses being to link to a specific part of a page, reference the element with the for attribute of a label, and target it with CSS and JavaScript. It is not designed to be sent to the server. You cannot get it with PHP.

    When a form is submitted to a server the data is derived from the names and values of the successful controls.

    Any data you want to send to the server should, therefore, be stored in the value attribute.

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

    and

     $result = $_POST['foo'];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search