1

This is my first post on SO. I'm currently working on a Uni project - we are creating a Momondo/Pricerunner type of website for flight tickets from RyanAir. We are scraping RyanAirs site for all the info we need - and that works perfectly fine. So on our site, the user will enter their route (e.g. Copenhagen to London Stanstead) a departure and return date and an amount of adults. All these input fields are in a form. Then the user will click 'Search Flights' which submit all the input fields in the form and sent them to the server via POST (and querystring from NPM) which starts the different JS scripts with the inputs the user has given on the site. When the scraper starts, it will parse all scraped data to a JSON file - the user can also see chart data on how the price moves.

Picture: Main site

We want to add a notification option, where the user can input their phone number and the desired price for their ticket. So if the ticket gets to lower or equal to the user's desired price, an SMS will get sent directly to their phone with the right info. This notification thingy is a button on the main page, that when pressed will activate a pop-up/modal, where the user then inputs their phone number and the price as stated above. However, it seems like the input fields in the pop-up/modal won't get submitted correctly to the server. All the other user-input like route, departure, and return date, the amount of adults gets correctly sent to the server. But the inputs in the pop-up/modal does not. I've tried to put the pop-up/modal code in the form tag of the other user-input, but that still doesn't help. When submitting the user phone number and the desired price, the value of them is undefined, which I do not understand. The pop-up/modal has display: none as default since it should only pop up when user presses the notification button.

Picture: Notification pop-up/modal

I hope that I can get some help with it here because I spent a lot of time making that pop-up/modal :'). Im going to paste the html, js and css for the pop-up/modal here below.

HTML (keep in mind i have already tried putting the pop-up/modal in the form where the other user-input from main page is):

                <div class="getNotified" id="myPopUp">
                    <div id="PopUpContent" class="PopUp">
                        <div class="PopUpContentHeader">
                            <h1 id="PopUpH1">Get notified <img id="notify_icon" src="images/notify_icon.png" height="42" width="42"> </h1>
                        </div>
                        <div class="PopUpBody">
                            <p>Want to get notified about future price changes for this route? </p>
                            <br>
                              <p>Leave your phone number and your desired total price for the ticket(s) below. <br> Now sit back and relax - we'll take care of the rest!</p>
                            <br>
                            <br>
                            <input type="tel" id="UserTel" placeholder="Phone number.." value=""> 
                            <br>
                            <div class="priceMover">
                                <br>
                                <input type="number" id="userPrice" placeholder="Price.." value="">
                                <button type="button" class="closePopUp" id="OKbutton"> OK </button>
                            </div>
                        </div>
                    </div>
                </div> 

CSS:

#PopUpButton{
    display: inline-block;
    height: 37px;
    margin-bottom: -1px;
    margin-top: 1px;
    margin-left: 10px;
    margin-right: 100px;
    background-color: black;
}
#PopUpButton:hover{
    background-color: #ffc107
}
#PopUpH1{
    font-size: 25px;
    padding: 10px;
    color:#fefefe;
}
img[id="notify_icon"]{
    margin-top: -40px;
    margin-bottom: -12.5px;
}
.getNotified {
    display: none; /* Hidden by default */
    position: fixed;  /* Stay in place*/
    z-index: 1; /* Sit on top */
    padding-top: 210px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
  }

  /* Modal Content */
  .PopUp {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    border: 1px solid rgb(14, 1, 1);
    width: 37%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
  }
  .priceMover{
    width: 23%;
    height: 10px;
    margin-left: 180px;
    margin-top: -66.5px; /* 56.5 */
}
  /* this is the input field for desired user price */
  #userPrice{ 
    width: 37%;
    height: 15px;
    background: transparent;
    background-image: url(../images/scaled1_tag.png);
    background-position: 5px 5px;
    background-repeat: no-repeat;
    padding-left: 35px;
    border: 1.5px solid #ccc;
    border-radius: 4px;
    font-size: 15px;
  }
  /* this is the input field for user phone number*/
  #UserTel{
    width: 23%;
    height: 32.8px;
    border-radius: 4px;
    background-image: url(../images/scaled1_phn.png);
    background-position: 5px 5px;
    background-repeat: no-repeat;
    padding-left: 35px;
    margin-top: -10px;
    margin-bottom: 10px;
    border: 1.5px solid #ccc;
    border-radius: 4px;
    font-size: 15px;
  }
  /* Add Animation */
  @-webkit-keyframes animatetop {
    from {top:-300px; opacity:0} 
    to {top:0; opacity:1}
  }
  @keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
  }
  /* The OK Button */
  #OKbutton{
      margin-top: 1px;
      margin-right: -242px;
  }
  .closePopUp {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
  }
  .closePopUp:hover, .closePopUp:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
  }
  .PopUpContentHeader {
    padding: -1px 16px;
    background-color: #ffc107;
    color: #f6f4f4;
    margin-bottom: -45px;
    margin-top: -25px;
  }
  .PopUpBody {padding: 10px 10px 56.5px;}

JS:

<script>

    var modal = document.getElementById("myPopUp");
    var btn = document.getElementById("PopUpButton");
    var btn2 = document.getElementsByClassName("closePopUp")[0];

    // When the user clicks the button, open the pop-up
    btn.onclick = function() {
        modal.style.display = "block";
    }

    // When the user clicks on OK button, close the pop-up
    btn2.onclick = function() {
        modal.style.display = "none";
    }

    // When the user clicks anywhere outside of the pop-up, close it
    window.onclick = function(event) {
        if (event.target == modal) {
            modal.style.display = "none";
        }
    }
    </script>

Im trying to get the input values with POST method via our localhost server, as said, it works for the other input fields, but not for pop-up/modal ones. As u can see below, im currently just console.log the pop-up/modal input value to test on line 4 below, and it doesnt work. Im putting the code under here for good measure.

req.on('end', function () {
            var post = qs.parse(body);
            if(req.url == "/website.html"){  
                console.log({UserPhone: post.UserTel, UserPrice: post.userPrice});
                if(post.datepicker3 == ""){
                    startJob(post.datepicker2, undefined, post.myInput3, post.myInput4, post.adltsQ1);
                    console.log({departDate: post.datepicker2, fromCity: post.myInput3, toCity: post.myInput4, AmountAdults: post.adltsQ1});
                    res.writeHead(200);
                }else{
                    startJob(post.datepicker, post.datepicker1, post.myInput1, post.myInput2, post.adltsQ);
                    console.log({departDate: post.datepicker, returnDate: post.datepicker1, fromCity: post.myInput1, toCity: post.myInput2, AmountAdults: post.adltsQ});
                    res.writeHead(200);
                }
            }

        });

Here is the CMD when submitting, as u can see all other info is correct (ignore the 'Checking if given file exists...'):

CMDs output

Ive rambled on, i dont know if i shoudk provide any other code than this - please let me know. And thank you very much for using your time to potentially help me! Have a nice day.

EDIT

Ive now tried to create dummy input fields in the form, the user phone number and desired price input field value will then get = to the dummy input fields values, i can successfully console log this (which i could not do before), but still cant submit it successfully - still says undefined. Which is very weird, since i can litterally console log the dummy input fields (Which are in the form tag) and will log the correct info, then i submit it, and it says undefined.

KPsanz
  • 27
  • 1
  • 6

2 Answers2

0

It may be possible you are not getting replies because for people working with best practices this is a mess. I did a few minutes of research and it may be possible that the modal input is being ignored due to display:none. I would attempt parsing it without display:none to see the result. Here is a link to this information: When will a browser parse elements which are hidden?

AJAX & jQuery example

        $.ajax({
            url: "https://api.exchangeratesapi.io/latest?base=EUR",
            method: "GET"
        }).done(function(res) {
            return (global.rate = res.rates.USD);
        });

The url returns a currency rate object. I retrieve the wanted data and return it to the main function. Using AJAX you can also do POST, PUT etc, but this is for you to lookup if you're interested.

Ilia
  • 112
  • 5
  • 1
    llia - hello! Thank you for answering. Ive been doing HTML for under 2 months so yeah, might be a mess... ill try it out and let you know how it goes. – KPsanz May 02 '20 at 15:57
  • I see you've made an edit about using form tags. I am uncertain, given that you say >"in the form tag" but your HTML above contains no form tag. Example of what should be done and the lack thereof might be causing the issue: `
    `. Better yet, you should learn about ajax, the simpler version is in jQuery.
    – Ilia May 03 '20 at 09:58
  • I am aware of that the HTML i posted does not contain the form tag, but i have indeed tried inserting it in the form, which did not help... and yes i also include the 'method='post'' in the form tag. However, i do not have the action tag in the form. Can i include a javascript file in this action tag? The same js file that the other input gets sent to (the file containing the localserver and the scripts that run with the posted input from the forms). – KPsanz May 03 '20 at 10:08
  • https://stackoverflow.com/questions/10520899/form-action-with-javascript/34467977 Yes in theory, using google will help you a lot :D – Ilia May 03 '20 at 10:12
  • Thank you - i will look in to it :) – KPsanz May 03 '20 at 10:41
0

Since the elements of a DOM object with CSS display style 'none' can not be rendered and will not be part of the render tree, this means that the elements on the modal cannot be accessible as long as the display of the modal still remain none. I will advice that you keep the value of the phone text in variable so that when the modal not visible, the value of the variable can still be used.

  • This was exactly what i have done now. When the phone and price has been input, and user presses ''OK'' button in the modal, the phone number and price values will get transferred to two invisible input fields (that are in the form) - which i can successfully console.log the value of when the modal has been closed! BUT i still cant post it to my server.... i really do not understand this... so weird and frustrating. – KPsanz May 03 '20 at 09:43
  • Forgot to say thanks for your answer Matthew! Also, is there another way i could do this and what you say i should do? – KPsanz May 03 '20 at 09:44
  • Should I perhaps instead of creating invisible (dummy) input fields that is gonna hold the value of phone number and price (which still doesn't work in terms of posting those values to the server), should I then instead make the modal a form in itself, so that the ''OK'' button submits the phone number and price to server, and then in the javascript for the server, create two variables that is gonna hold the values of phone number and price when posted? Gonna test this later^^ – KPsanz May 03 '20 at 09:54