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
To get the ID or data attribute value of the checked radio input in PHP, you can modify the code as follows:
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:
Make sure that the modified PHP code is executed after the form submission, and the radio input values are sent via the POST method.
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 thefor
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
name
s andvalue
s of the successful controls.Any data you want to send to the server should, therefore, be stored in the
value
attribute.and