Setting up the subject line for an email script

I have received an email script from my hosting provider that I need to customize. My goal is to extract the value of radio buttons in a group, and based on their id determine the subject line of the email. If the id is small, the subject should be "företag", or if it's large, then the subject should be "Sök jobb hos oss".

Here is the script for the email form:

<form method="post" action="http://www.something.se/cgi-bin/FormMail.pl"
                            accept-charset="ISO-8859-1" onsubmit="var originalCharset= document.charset;
            document.charset= 'ISO-8859-1';
            window.onbeforeunload= function () {document.charset=originalCharset;};" class="form">
                            <div class="u-margin-bottom-medium">
                                <a name="section-book"></a>
                                <h2 class="heading-secondary">
                                    kontakta oss
                                </h2>
                            </div>

                            <!-- More form input fields here -->

                        </form>

Is there a way to modify this part:

<input type="hidden" name="subject" value="Subject" />

to dynamically set the subject based on the selected radio button using JavaScript?

Answer №1

Manipulating the value of a hidden field in your form can be done using JavaScript. In this case, you can access and modify the value of the hidden "subject" input field with the following code:

document.getElementsByName("subject")[0].value

The method document.getElementsByName(name) returns a NodeList (an array-like object) containing all elements with the specified name attribute. By selecting the first element ([0]), we isolate the desired hidden input.

To determine which radio button is selected, you can utilize the unique ids assigned to them - 'small' and 'large'. Access their checked status using the following code snippet:

var small = document.getElementById("small").checked;
var large = document.getElementById("large").checked;

Based on the boolean values returned by these checks, you can update the subject field accordingly using conditional statements:

if (small) {
  document.getElementsByName("subject")[0].value = "Company";
}
if (large) {
  document.getElementsByName("subject")[0].value = "Job Opportunities";
}

For better organization, encapsulate this logic within a function that can be triggered by the form's onsubmit event. Here’s an example showcasing how this can be implemented:

function updateSubject() {
  var small = document.getElementById("small").checked;
  var large = document.getElementById("large").checked;
  if (small) {
    document.getElementsByName("subject")[0].value = "Company";
  }
  if (large) {
    document.getElementsByName("subject")[0].value = "Job Opportunities";
  }
}

Incorporate this function invocation within your form declaration ensuring it triggers correctly during submission:

<form method="post" action="youraction.php" onsubmit="updateSubject(); return true;">
  <!-- Form fields and other contents here -->
</form>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: Unable to access undefined properties (attempting to read 'getBoundingClientRect')

I keep encountering the issue TypeError: Cannot read properties of undefined (reading 'getBoundingClientRect') while working in React on my localhost with Node.js. I can't figure out what's causing it. import React,{useRef,useState} fro ...

Is there a way to locate the final <li></li> within each <ul></ul>? (Regarding front-end development, specifically dealing with html, css, and jquery)

I'm dealing with a menu that has submenus of its own. Issue: Upon loading the page, I notice that the website is adding an extra letter S at the end of each submenu. How can I eliminate that letter S from the end of every submenu using JQuery or CS ...

Using Jquery Ajax to Remove Post Upon Form Submission

I am trying to submit and delete posts using the form below: <div id="post"> <form method="post" action="javascript:alert('success')" > <input id="postid" type="hidden" name="post" value="9" /> <input id="unique" typ ...

What could be causing my vis.js network's node hover popups to not function properly?

I've encountered an issue where, despite adding the 'title' property to my node objects, the pop up window with the title content doesn't appear when I hover over a node. Here are the options I've chosen and how I've set up m ...

Failure to trigger AJAX Success or Error Events

I'm struggling to understand why this code isn't working or find any relevant resources. When I check the json object in Firebug, it either returns success: false or success: true from the post request, so I'm confused as to why the function ...

Unable to display an image element in fullscreen within a modal that is already fullscreen

I am facing an issue with my modal that is designed to occupy the full width and height of a browser. Inside this modal, there is an image enclosed within a div along with some other elements. My goal is to make the image go full screen in the browser, aki ...

Getting a ThreeJS element, specifically a mesh, by its ID

I am working with a Mesh object in Three.JS. Is there a way to retrieve the Mesh object by its id and adjust its position, similar to using $(#"sample") in jQuery? Alternatively, do you have any suggestions for changing the position of objects in the ...

Choose dropdown menus that are pre-filled by JSON may require a manual refresh to show the content

Occasionally, I encounter an issue with a JSON file used to populate select boxes, where the items in the drop down fail to display until the page is refreshed. Despite no errors in the console or log, the loading is seamless on most occasions, leaving me ...

Constant variable class

I need to create a unique ID for each object in a class using node. For instance, I want each instance of a Person class to have an ID starting from 1, 2, and so on. In Java, this problem can be solved with the following code: public class Person { st ...

Discover the hidden truth: Unveiling the enigma of React

I'm currently learning React and I've been working on some apps to enhance my skills and deepen my understanding. Right now, I am facing a challenge where I need to incorporate the logged user information into the Redux state. However, whenever I ...

Managing callback functions within a for loop in Node.js: strategies and best practices

My mongoose schema looks like this: skills : [ { name : {type : String}, count : {type :Number, default:0} } ] I am currently executing an update query using a for loop in order to update an array of documents. ...

Tips for showcasing content by hovering over buttons with the help of Bootstrap3 and Jquery

My code and fiddle are currently set up to display buttons when hovered over, but I want to modify it so that only the relevant button is displayed when a specific text is hovered over. For example, if "Water" is hovered over, only the button for Water sho ...

Tips for updating the content within an HTML tag with jQuery

I am looking to update the parameter value of an applet tag based on the selection from a dropdown menu. As I am new to jQuery, I would appreciate any guidance on how to achieve this using jQuery. This is my current applet code: <applet id="decisiontr ...

Is it possible to insert an image directly into the <img> tag without having to first send it to the server?

I've created an upload form that allows users to submit images: <form> <input accept="image/jpeg, image/gif, image/png" type="file" name="image" id="image" class="UploadInput" onchange="submitImageUploaderForm()"/> </form> Once t ...

Adjust the sorting key for the displayed mapped array in a React component

Seeking client-side sorting functionality that allows users to sort by different integer keys. Currently implementing a sort function right before the map function: .sort(function(a, b) { return b.sortKey - a.sortKey; } When a button is pr ...

What steps should be taken to configure Multer so that it delays saving an image until after a successful database entry using Mongoose has been made?

In the typical Multer setup, files are saved immediately, as illustrated in the following basic example: const multer = require('multer'); const upload = multer({ dest: 'uploads/' }); const app = express(); app.post('/profile&apo ...

Utilizing a mutual RxJS subject for seamless two-way data binding in Angular 2

I have a unique service dedicated to managing app configurations class Configuration { get setting() { return dataStore.fetchSetting(); } set setting(value) { dataStore.saveSetting(value); } } This configuration is linked to components t ...

Comparing the positions of elements in Selenium WebDriver with PHP

One of the elements on my webpage serves as a button that reveals a drop-down menu. Due to various factors affecting the page layout, there were some alignment issues between the button and the menu until a few bugs were fixed. I am now looking for a way t ...

Printing a webpage through an FGL printer (Boca System) step-by-step guide

I am currently developing a ticketing system website that requires admins to print tickets using a Boca printer. The issue I am facing is that the Boca printer utilizes FGL (Friendly Ghost Language) as a standard for ticket printing. Is there a "secret" m ...

Changing color based on AngularJS condition using CSS

My HTML code looks like this: <div> <i class="glyphicon glyphicon-envelope" style="margin-top: -50px; cursor:pointer; color: #1F45FC" ng-click="createComment(set_id)"> </i> Route <center> ...