Support » Developing with WordPress » Option Values not accurate

  • Resolved drockmillar

    (@drockmillar)


    Hi Everyone,

    I am completely dumbfounded here and hope someone may understand what is going on. I have a page on my site that I want the user to input 5 bets, and then the bets will be inputted into a DB table on submit. The PHP snippets will display the available games per bet depending on the criteria of the bet, so there are 5 queries ran to pull the information. In the PHP code, I set the value of each option to the respective game_ID, and it works as intended for the first bet.

    Bets 2-5 however mimic the game_IDs of Bet 1. For example, Bet One has game 1, 2, and 3 with game_ID values of 1, 2, and 3. If Bet Two has game 2, 3, and 4, the value for each option would be 1, 2, and 3 (matching the Bet One values).

    I have confirmed that the queries are correctly getting the game_IDs, but for some reason aren’t correct on page load. I have included the code from this page below.

    I am not sure if it has something to do with how the php code is executed (or in what order) or what. If someone is able to help me figure out what is going on I would greatly appreciate it. I can answer any additional questions if this problem needs additional explaining.

    <script>
    window.onload = function() {
      var game1Sel = document.getElementById("games_1");
      var game2Sel = document.getElementById("games_2");
      var game3Sel = document.getElementById("games_3");
      var game4Sel = document.getElementById("games_4");
      var game5Sel = document.getElementById("games_5");
    
    game1Sel.onchange = function() {
      var x = document.getElementById("games_1").selectedIndex;
      var y = document.getElementsByTagName("option")[x].value;
      document.getElementById("game_1_id").value = y;
    
    }
    game2Sel.onchange = function() {
      var a = document.getElementById("games_2").selectedIndex;
      var b = document.getElementsByTagName("option")[a].value;
      document.getElementById("game_2_id").value = b;
    
    }
    game3Sel.onchange = function() {
      var c = document.getElementById("games_3").selectedIndex;
      var d = document.getElementsByTagName("option")[c].value;
      document.getElementById("game_3_id").value = d;
    alert(d);
    }
    game4Sel.onchange = function() {
      var e = document.getElementById("games_4").selectedIndex;
      var f = document.getElementsByTagName("option")[e].value;
      document.getElementById("game_4_id").value = f;
    alert(f);
    }
    game5Sel.onchange = function() {
      var g = document.getElementById("games_5").selectedIndex;
      var h = document.getElementsByTagName("option")[g].value;
      document.getElementById("game_5_id").value = h;
    alert(h);
     }
    }
    function purseCheck() {
    var money = parseInt([xyz-ips snippet="Display-User-Money"]);
    var amt1 = parseInt(document.getElementById("bet_1_amt").value);
    var amt2 = parseInt(document.getElementById("bet_2_amt").value);
    var amt3 = parseInt(document.getElementById("bet_3_amt").value);
    var amt4 = parseInt(document.getElementById("bet_4_amt").value);
    var amt5 = parseInt(document.getElementById("bet_5_amt").value);
    var total = amt1+amt2+amt3+amt4+amt5;
    if (total > money) {
    alert("You tried to bet " + total + " and only have " + money);
        return false;
      }
    }
    </script>
    <br><br><div style="text-align: center;">
    
    <form name="form1" id="form1" method="post" onsubmit="return purseCheck();" action="">
    Bet 1: Insert Bet Narrative<br>
    <div style="display: inline-block; text-align: left;">
    Game: <select name="games_1" id="games_1" required="">
       <option value="">Select Game</option>
       	[xyz-ips snippet="Bet-1-Display-Games"]
    </select><br>
    Bet Type: <select name="bet_1_type" id="bet_1_type" required="">
       <option value="">Select Bet Type</option>
       <option value="ML">Moneyline</option>
       <option value="PS">Point Spread</option>
       <option value="OU">Over/Under</option>	
    </select><br>
    Team: <select name="team_1" id="team_1" required="">
       <option value="">Select Team</option>
       <option value="home">Home Team</option>
       <option value="away">Away Team</option>	
    </select><br>
    Bet Amount: <input type="number" id="bet_1_amt" name="bet_1_amt" step="1" required="">
    <input type="hidden" id="game_1_id" name="game_1_id" value="">
    </div>
    <br><br>
    Bet 2: Insert Bet Narrative<br>
    <div style="display: inline-block; text-align: left;">
    Game: <select name="games_2" id="games_2" required="">
       <option value="">Select Game</option>
       	[xyz-ips snippet="Bet-2-Display-Games"]
    </select><br>
    Bet Type: <select name="bet_2_type" id="bet_2_type" required="">
       <option value="">Select Bet Type</option>
       <option value="ML">Moneyline</option>
       <option value="PS">Point Spread</option>
       <option value="OU">Over/Under</option>	
    </select><br>
    Team: <select name="team_2" id="team_2" required="">
       <option value="">Select Team</option>
       <option value="home">Home Team</option>
       <option value="away">Away Team</option>	
    </select><br>
    Bet Amount: <input type="number" id="bet_2_amt" name="bet_2_amt" step="1" required="">
    <input type="hidden" id="game_2_id" name="game_2_id" value="">
    </div>
    <br><br>
    Bet 3: Insert Bet Narrative<br>
    <div style="display: inline-block; text-align: left;">
    Game: <select name="games_3" id="games_3" required="">
       <option value="">Select Game</option>
       	[xyz-ips snippet="Bet-3-Display-Games"]
    </select><br>
    Bet Type: <select name="bet_3_type" id="bet_3_type" required="">
       <option value="">Select Bet Type</option>
       <option value="ML">Moneyline</option>
       <option value="PS">Point Spread</option>
       <option value="OU">Over/Under</option>	
    </select><br>
    Team: <select name="team_3" id="team_3" required="">
       <option value="">Select Team</option>
       <option value="home">Home Team</option>
       <option value="away">Away Team</option>	
    </select><br>
    Bet Amount: <input type="number" id="bet_3_amt" name="bet_3_amt" step="1" required="">
    <input type="hidden" id="game_3_id" name="game_3_id" value="">
    </div>
    <br><br>
    Bet 4: Insert Bet Narrative<br>
    <div style="display: inline-block; text-align: left;">
    Game: <select name="games_4" id="games_4" required="">
       <option value="">Select Game</option>
       	[xyz-ips snippet="Bet-4-Display-Games"]
    </select><br>
    Bet Type: <select name="bet_4_type" id="bet_4_type" required="">
       <option value="">Select Bet Type</option>
       <option value="ML">Moneyline</option>
       <option value="PS">Point Spread</option>
       <option value="OU">Over/Under</option>	
    </select><br>
    Team: <select name="team_4" id="team_4" required="">
       <option value="">Select Team</option>
       <option value="home">Home Team</option>
       <option value="away">Away Team</option>	
    </select><br>
    Bet Amount: <input type="number" id="bet_4_amt" name="bet_4_amt" step="1" required="">
    <input type="hidden" id="game_4_id" name="game_4_id" value="">
    </div>
    <br><br>
    Bet 5: Insert Bet Narrative<br>
    <div style="display: inline-block; text-align: left;">
    Game: <select name="game_5" id="games_5" required="">
       <option value="">Select Game</option>
       	[xyz-ips snippet="Bet-5-Display-Games"]
    </select><br>
    Bet Type: <select name="bet_5_type" id="bet_5_type" required="">
       <option value="">Select Bet Type</option>
       <option value="ML">Moneyline</option>
       <option value="PS">Point Spread</option>
       <option value="OU">Over/Under</option>	
    </select><br>
    Team: <select name="team_5" id="team_5" required="">
       <option value="">Select Team</option>
       <option value="home">Home Team</option>
       <option value="away">Away Team</option>	
    </select><br>
    Bet Amount: <input type="number" id="bet_5_amt" name="bet_5_amt" step="1" required="">
    <input type="hidden" id="game_5_id" name="game_5_id" value="">
    </div>
    <br><br>
    <input type="submit" name="Submit" value="Submit">  
    </form>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    If the queries are getting the correct game_IDs, the form code you provided is likely correct as well. If the problem is in the output of results after the first bet, the issue would either be the queries return improper results, or the PHP responsible for output is somehow flawed.

    You should confirm that the bets were properly saved in the DB. You can use the phpMyAdmin app (usually accessed through your hosting account) to look directly at the DB records. Use var_dump() to see the results of a query right after it is made. Verify that these too are correct. If that all checks out, then the issue is in the output code.

    If a problem in output code is indicated, post that code here and maybe someone can spot what the trouble is. If the code is more extensive than a brief snippet, please post somewhere like pastebin.com or gist.github.com and only provide the link here.

    Thread Starter drockmillar

    (@drockmillar)

    Thanks for the reply! After a little more digging I deduced that
    var b = document.getElementsByTagName("option")[a].value;
    for each bet except the first was actually pulling the value at index[a] for the first bet. I thought that grabbing the selectedIndex for each bet would specify I wanted the respective value, but programming doesn’t work that way sometimes!

    After just grabbing the value from getElementByID.value for each bet, I got it working and correctly inserting into the DB.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.