What is preventing me from successfully transferring data to a different HTML page?

While I understand that there are numerous questions related to this topic, I believe my situation is unique. I am attempting to transfer form data from one HTML file (test.html) to another HTML file (tut1.html).

Essentially, my goal is to extract the data and display it in a modal on the second page. I encountered an issue where the process worked during a test run, but when implemented in my main project, it did not function as expected. I am certain that I have overlooked a minor detail, but I am struggling to pinpoint it. Any assistance in resolving this issue would be greatly appreciated.

The first HTML file contains the form where the user inputs their information.

<html>
<head>

</head>                 

<body>

                <div  class="feedback-background">
                        <div class="feedback-content">
                            <div class="close">+</div>
                            <img src="E:\IIT\Lectures\WEB\coursework1\Images\feedbackimg1.jpg" alt="Givefeedback" style="width:100px;height:100px;">

                            <form name="FeedbackForm"  method="get">
                                Name:
                                <input id="Name" name="N" type="text" placeholder="Name">
                                E-Mail:
                                <input id="E-mail" name="E-mail" type="email" placeholder="E-mail">
                                What do you think about us?<br>
                                <textarea id="comment" rows="6" cols="33" name="C"></textarea>
                                <br>
                                How would you rate us ?
                                <br>

                            <label><input type ="radio" name="rating" id="rating" value="Excellent" checked>Excellent</label>
                            <label><input type ="radio" name="rating" id="rating" value="Very Good">Very Good</label>
                            <label><input type ="radio" name="rating" id="rating" value="Average">Average</label>
                            <label><input type ="radio" name="rating" id="rating" value="Poor">Poor</label>
                            <label><input type ="radio" name="rating" id="rating" value="Extreamly Poor">Extremely Poor</label>
                            <br>

                                <a href="tut1.html" onClick="testJS()" id="submit" type="submit">SUBMIT</a>


                            </form>
                        </div>
                </div>

                <script>

function testJS() {
    var b = document.getElementById('Name').value,
            document.getElementById('comment').value,
    url = 'http://E:\IIT\Homework\web1t/tut1.html?Name=' + encodeURIComponent(b);

document.location.href = url;
}

</script>



</body>

The second HTML file (tut1.html) contains a modal that I have created (CSS excluded for your convenience).

<body>      
<div class="popup">
                <div class="popuptext" id="myPopup"><p>Thank you <span id="username"></span> ,<br>Your feedback has been recorded.<br> 
                            <br>You commented that"<span id="usercomment"></span>" <br><br>and rated our website "<span id="userrating"></span>".
                            <br><br>Thank you 
                            for taking your time to do so.<br><br>You will now be re-directed automatically</p>
                </div>
                </div>



    <script>
    window.onload = function () {
    var url = document.location.href,
    params = url.split('?')[1].split('&'),
    data = {}, tmp;
    for (var i = 0, l = params.length; i < l; i++) {
     tmp = params[i].split('=');
     data[tmp[0]] = tmp[1];
     }
    document.getElementById('username').innerHTML = data.N;
    document.getElementById('usercomment').innerHTML = data.C;
    }
  </script>


    </body>

PS: I successfully used the exact same codes to populate another form, which worked flawlessly. I am unsure why it is not functioning in this case. Any form of assistance would be greatly appreciated.

Answer №1

It appears that there should be an error output in your code, yet you seem to be neglecting the use of a debugging tool. This could be a valuable learning opportunity for you to improve your skills.

Firstly, consider changing the 'a tags href' to '#tut1.html' and then test if clicking it successfully transitions you to tut1.html. Check if the URL is as expected upon transitioning.

Let's give this a try.

'<a href="tut1.html"~' → '<a href="#tut1.html~'
 (Of course, e.preventDefault is also acceptable.)

Make sure to update the testJS function accordingly.

function testJS() {
    // There was a syntax error, and the value format is usually "?name=value&name=value".
    var b = document.getElementById('Name').value,
        c = document.getElementById('comment').value,
        url = 'http://E:\IIT\Homework\web1t/tut1.html?Name='+ encodeURIComponent(b) + '&Comment=' + encodeURIComponent(c);

    // If you are not using a debugging tool, consider using an alert at least.
    alert(url);
    document.location.href = url;
}

The tut1.html page also needs to be updated.

window.onload = function () {
    var url = document.location.href,
        params = url.split('?')[1].split('&'),
        data = {}, tmp;
    for (var i = 0, l = params.length; i < l; i++) {
        tmp = params[i].split('=');
        data[tmp[0]] = tmp[1];
    }
    // The names are incorrect; you have set names as Name and Comment when creating the URL.
    document.getElementById('username').innerHTML = data.Name;
    document.getElementById('usercomment').innerHTML = data.Comment;
}

Just a heads up. The action on the first page is the same as what you have used simply.

<form action='tut1.html' method='get'>
+
<input type="submit" value="submit">

In this scenario, the parameters are sent using their respective names and values (?N=xxxx&C=yyyy). You do not need to manually create the URL with parameters unless there is a specific reason.

Answer №2

To ensure your testJS function works correctly, make sure to include a preventDefault statement as shown below:

function testJS(e) {
    e.preventDefault();
    var name = document.getElementById('Name').value,
        comment = document.getElementById('comment').value,
        url = 'http://E:\IIT\Homework\web1t/tut1.html?Name=' + encodeURIComponent(name);

    document.location.href = url;
}

Since you are manually defining the URL, it is necessary to prevent the default behavior of the anchor tag.

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

What is the best way to crop an image to focus solely on the center portion?

Using Bootstrap 3, how can I display an image cropped in a .col-sm-6 grid column? The image is 720px wide. Typically, with the .img-responsive class, the image will resize proportionally when the display size changes. However, I want to crop the left and ...

Why is my Angular form submitting twice?

Currently, I am working on a user registration form using AngularJS with ng-submit and ng-model. However, I am facing an issue where the form triggers submission twice when the user submits it. I have checked for common causes such as declaring the contro ...

Using React with TypeScript to ensure that at least one key of a type is not null, even if all keys are optional

Within my Typescript code, I have defined an event type that includes various time parameters: export type EventRecord = { name: string; eta: string | null; assumed_time: string | null; indicated_time: string | null; }; I also have a func ...

Tips for customizing the appearance of date circles and text in Vue V-Calendar.io

On v-calendar.io, there is a demo showcasing the correct display where the date "2018-01-01" has a red background and dark text: However, my display does not match that. I have included Vue.js and v-calendar through CDN, and the page is formatted in HTML ...

The validation for decimal numbers fails to function when considering the length

I've been struggling to come up with a regular expression for validating decimal numbers of a specific length. So far, I've tried using pattern="[0-9]){1,2}(\.){1}([0-9]){2}", but this only works for numbers like 12.12. What I'm aimin ...

Conditional Matching with Javascript Regular Expressions

My strings are formatted like this: #WTK-56491650H #=> want to capture '56491650H' #M123456 #=> want to capture 'M123456' I am trying to extract everything after the # character, unless there is a dash. In that case, I onl ...

How can you reset a variable counter while inside a forEach loop?

I am currently working on resetting a counter that is part of a recursive forEach loop within my code. Essentially, the code snippet provided calculates the length of the innermost key-value pair as follows: length = (key length) + (some strings inserted) ...

Implementing Partial Login and Registration Views using AngularJS in conjunction with MVC5 and ASP.NET Identity

Embarking on the journey of creating a Single Page Application with log-in/register functionality using MVC5, ASP.NET Identity, and Angular feels like diving into a vast ocean of web development technologies. Despite being new to this realm, I delved into ...

The Jquery script is ineffective for newly added elements

After creating an Ajax Form that functions well, I noticed that when the result of the form is another form, my script does not work for the new form generated. Is there a way to make the new form function like the old one? $(document).ready(function() ...

What is the best way to retrieve a string from a URL?

Is there a way to extract only a specific string from a URL provided by an API? For instance: I'm interested in extracting only: photo_xxx-xxx-xxx.png Any suggestions on how to split the URL starting at photo and ending at png? ...

Store the numeric value in JavaScript as a PHP integer

My goal is to obtain the width of the browser and then compare it as a PHP variable. However, the issue I am facing is that it is being saved as a string, and my attempts at parsing it to another variable only result in returning 0. $tam='<script& ...

Limit users to entering either numbers or letters in the input field

How can I enforce a specific sequence for user input, restricting the first two characters to alphabets, the next two to numbers, the following two to characters, and the last four to numbers? I need to maintain the correct format of an Indian vehicle regi ...

You are only able to click the button once per day

I am working on a button that contains numeric values and updates a total number displayed on the page when clicked. I would like this button to only be clickable once per day, so that users cannot click it multiple times within a 24 hour period. Below i ...

Variable unique to the specific function in universal function

Can you explain why the alert displays "AAA" instead of "BBB"? http://jsfiddle.net/Lp4cS/ var z = "AAA"; function xx() { var z = "BBB"; yy(); } function yy() { alert(z); } xx(); ...

Is there a way to update page content without having to refresh the entire page?

My goal is to refresh specific content on a page without having to reload the entire page using JavaScript or jQuery. Since my project is built in PHP and JavaScript, I encountered this issue. Note : I want the page content to refresh when a user performs ...

Styling Angular Material Forms: Customizing Input Backgrounds and Button Heights

I'm currently working on a simple email input form with a submit button. Here are the adjustments I want to make: Set the background of the email input to white Ensure that the submit button matches the height of the input field However, my attempts ...

Donut visualization within a Sankey diagram using D3 JS

Looking to enhance my D3 Js Sankey diagram by adding a donut chart around the company circles. The donut chart will display two values, numsms and nummid, within the company node. Below is an example of what I am trying to achieve: However, I've been ...

Tips for running a JavaScript function that is returned in an AJAX response

I am facing an issue with executing a function after a successful Ajax request in my jQuery code. Here is the snippet of my code: $.ajax({ type: "POST", url: "https://www.example.com/create_chat.php", data: dataString, beforeSe ...

Having difficulty rearranging choices within an optgroup

This is a dropdown https://i.sstatic.net/Rk5yL.png <select id="officer-id" placeholder="Choose an officer"> <option selected="selected" >----</option> <optgroup id="pt1" label="To be reviewed"> ...

The dropdown menu is malfunctioning when accessed within the mobile view of a jQuery

Take a look at this code snippet on Fiddle: https://jsfiddle.net/rizwanali98601/ngofhc24/12/. The table below contains a dropdown inside a jQuery Datatable. When the button is clicked, an option is supposed to be added to the dropdown. However, in mobile ...