locked
Help with ajax RRS feed

  • Question

  • User-818268817 posted

    Hello, just need some help with this code. The responses in the array doesn't seem to be randomized and will only change when I refresh the page, I posted this on a similar forum and the person told me I can approach this by using ajax but since i'm a complete noob, I have no experience in it. Was wondering if someone could edit my code or give me some advice. Thanks :)

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Untitled Document</title>
    </head>
    <body>
        <p>SoothSayer<br></p>
        <form>
            Name: <input id="Name" name="textfield" type="text"><br>
            Question: <input id="Question" name="textfield" type="text"><br>
            Birthday: <select id="month">
            <option value=0>&nbsp&nbsp--Month--</option>
            <option value=1>January</option>
            <option value=2>February</option>
            <option value=3>March</option>
            <option value=4>April</option>
            <option value=5>May</option>
            <option value=6>June</option>
            <option value=7>July</option>
            <option value=8>August</option>
            <option value=9>September</option>
            <option value=10>October</option>
            <option value=11>November</option>
            <option value=12>December</option>
        </select>
         <select id="date">
            <option value=0>&nbsp&nbsp--Date--</option>
        </select>
    </form>
    <br>
    <span id="sign"></span>
    <span id="Random"></span>
        <script type="text/javascript">
        var dsel = document.getElementById("date");
        var msel = document.getElementById("month");
        var mong = document.getElementById("Name");
        //var schlong = document.getElementById("Question");
    	
        var x = ["I see great calamity up ahead, you should definitley hold off on that", "I see great fortune up ahead, you should definitley do that!", "Your way ahead is cloudy, I suggest you dont do that", "Your way ahead is clear and fortunate, I suggest you attemp that!"];
    	var rand = x[Math.floor(Math.random() * x.length)];
    	
        dsel.options.length = 32;
        for ( var d = 1; d <= 31; ++d ) {
        dsel.options[d] = new Option(d,d);
        }
    
        msel.onchange =  clearbox;
        function clearbox() {
        document.getElementById("sign").innerHTML = " ";
        document.getElementById("date").selectedIndex = 0;
        }
    
        dsel.onchange = getSign;
    
        function getSign( ) {
        var m = Number(msel.value);
        var d = Number(dsel.value);
        var s = "";
        var a = mong.value
        if ((d == 0) || (m == 0)) {
        document.getElementById("sign").innerHTML = " ";
        return;
        }
    	
        var md = m * 100 + d;
    
        if ( m == 1 && d > 0 && d <= 19 ) md = 1299;
    
        if ( md >= 1222 )      { s = "Capricorn"; } 
        else if ( md >= 1122 ) { s = "Sagittarius"; } 
        else if ( md >= 1023 ) { s = "Scorpio"; } 
        else if ( md >= 923 )  { s = "Libra"; } 
        else if ( md >= 823 )  { s = "Virgo"; }
        else if ( md >= 723 )  { s = "Leo";   }
        else if ( md >= 622 )  { s = "Cancer"; }
        else if ( md >= 521 )  { s = "Gemini"; }
        else if ( md >= 420 )  { s = "Taurus"; }
        else if ( md >= 321 )  { s = "Aries"; }
        else if ( md >= 219 )  { s = "Pisces"; }
        else if ( md >  100 )  { s = "Aquarius"; }
    
    
        document.getElementById("sign").innerHTML = "Your Zodiac sign is " + s +", "+ a + " "+ rand;
    	}
        </script>
    </body>
    </html>



    Thursday, September 8, 2016 11:11 AM

Answers

  • User-474980206 posted

    no need for ajax. you just need to move rand lines to inside getSign so it runs every time rather than just once:

    function getSign( ) {
        var m = Number(msel.value);
        var d = Number(dsel.value);
        var s = "";
        var a = mong.value
        if ((d == 0) || (m == 0)) {
          document.getElementById("sign").innerHTML = " ";
          return;
        }
    
        // move 2 line here
        var x = ["I see great calamity up ahead, you should definitley hold off on that", "I see great fortune up ahead, you should definitley do that!", "Your way ahead is cloudy, I suggest you dont do that", "Your way ahead is clear and fortunate, I suggest you attemp that!"];
        var rand = x[Math.floor(Math.random() * x.length)];
    	
        var md = m * 100 + d;
    
        if ( m == 1 && d > 0 && d <= 19 ) md = 1299;
    
        if ( md >= 1222 )      { s = "Capricorn"; } 
        else if ( md >= 1122 ) { s = "Sagittarius"; } 
        else if ( md >= 1023 ) { s = "Scorpio"; } 
        else if ( md >= 923 )  { s = "Libra"; } 
        else if ( md >= 823 )  { s = "Virgo"; }
        else if ( md >= 723 )  { s = "Leo";   }
        else if ( md >= 622 )  { s = "Cancer"; }
        else if ( md >= 521 )  { s = "Gemini"; }
        else if ( md >= 420 )  { s = "Taurus"; }
        else if ( md >= 321 )  { s = "Aries"; }
        else if ( md >= 219 )  { s = "Pisces"; }
        else if ( md >  100 )  { s = "Aquarius"; }
    
    
        document.getElementById("sign").innerHTML = "Your Zodiac sign is " + s +", "+ a + " "+ rand;
    }
       

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 9, 2016 4:05 PM

All replies

  • User-359936451 posted

    A bit unclear on what you are trying to achieve, I don't see question in your post.

    You can look here for AJAX help and tons of videos and tutorials. But I am not sure if what you are doing requires AJAX.

    http://ajaxcontroltoolkit.devexpress.com/Default.aspx

    Thursday, September 8, 2016 2:06 PM
  • User-474980206 posted

    no need for ajax. you just need to move rand lines to inside getSign so it runs every time rather than just once:

    function getSign( ) {
        var m = Number(msel.value);
        var d = Number(dsel.value);
        var s = "";
        var a = mong.value
        if ((d == 0) || (m == 0)) {
          document.getElementById("sign").innerHTML = " ";
          return;
        }
    
        // move 2 line here
        var x = ["I see great calamity up ahead, you should definitley hold off on that", "I see great fortune up ahead, you should definitley do that!", "Your way ahead is cloudy, I suggest you dont do that", "Your way ahead is clear and fortunate, I suggest you attemp that!"];
        var rand = x[Math.floor(Math.random() * x.length)];
    	
        var md = m * 100 + d;
    
        if ( m == 1 && d > 0 && d <= 19 ) md = 1299;
    
        if ( md >= 1222 )      { s = "Capricorn"; } 
        else if ( md >= 1122 ) { s = "Sagittarius"; } 
        else if ( md >= 1023 ) { s = "Scorpio"; } 
        else if ( md >= 923 )  { s = "Libra"; } 
        else if ( md >= 823 )  { s = "Virgo"; }
        else if ( md >= 723 )  { s = "Leo";   }
        else if ( md >= 622 )  { s = "Cancer"; }
        else if ( md >= 521 )  { s = "Gemini"; }
        else if ( md >= 420 )  { s = "Taurus"; }
        else if ( md >= 321 )  { s = "Aries"; }
        else if ( md >= 219 )  { s = "Pisces"; }
        else if ( md >  100 )  { s = "Aquarius"; }
    
    
        document.getElementById("sign").innerHTML = "Your Zodiac sign is " + s +", "+ a + " "+ rand;
    }
       

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 9, 2016 4:05 PM