Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts

    Ran into a problem

    I have multiple select boxes which I need to check if all equal 10, and isn't less.

    PHP Code:
     <li>You have <b><span id="mess">10</b></spanstats remaining.</li><br />
                                                            <
    li>
                                                            <
    table id="stats" width="75%">
                                                            <
    tr><td>
                                    <
    b>Focus</b></td><td><select name="focus">
                                    <
    option value="0">0</option>
                                                                    <
    option value="1">1</option>
                                                                    <
    option value="2">2</option>
                                                                    <
    option value="3">3</option>
                                                                    <
    option value="4">4</option>
                                                                    <
    option value="5">5</option>
                                                                    <
    option value="6">6</option>
                                                                    <
    option value="7">7</option>
                                                                    <
    option value="8">8</option>
                                                                    <
    option value="9">9</option>
                                                                    <
    option value="10">10</option>
                                    </
    select></td></tr>
                                                                    <
    tr><td>
                                                                    <
    b>Attack</b></td><td><select name="attack">
                                                                    <
    option value="0">0</option>
                                                                    <
    option value="1">1</option>
                                                                    <
    option value="2">2</option>
                                                                    <
    option value="3">3</option>
                                                                    <
    option value="4">4</option>
                                                                    <
    option value="5">5</option>
                                                                    <
    option value="6">6</option>
                                                                    <
    option value="7">7</option>
                                                                    <
    option value="8">8</option>
                                                                    <
    option value="9">9</option>
                                                                    <
    option value="10">10</option>
                                    </
    select></td></tr>

                                    <
    tr><td>
                                                                    <
    b>Defense</b></td><td><select name="defense">
                                                                    <
    option value="0">0</option>
                                                                    <
    option value="1">1</option>
                                                                    <
    option value="2">2</option>
                                                                    <
    option value="3">3</option>
                                                                    <
    option value="4">4</option>
                                                                    <
    option value="5">5</option>
                                                                    <
    option value="6">6</option>
                                                                    <
    option value="7">7</option>
                                                                    <
    option value="8">8</option>
                                                                    <
    option value="9">9</option>
                                                                    <
    option value="10">10</option>
                                    </
    select></td></tr>

                                    <
    tr><td>
                                                                    <
    b>Dexterity</b></td><td><select name="dexterity">
                                                                    <
    option value="0">0</option>
                                                                    <
    option value="1">1</option>
                                                                    <
    option value="2">2</option>
                                                                    <
    option value="3">3</option>
                                                                    <
    option value="4">4</option>
                                                                    <
    option value="5">5</option>
                                                                    <
    option value="6">6</option>
                                                                    <
    option value="7">7</option>
                                                                    <
    option value="8">8</option>
                                                                    <
    option value="9">9</option>
                                                                    <
    option value="10">10</option>
                                    </
    select></td></tr>

                                    <
    tr><td>
                                                                    <
    b>Magic Defense</b></td><td><select name="magicdefense">
                                                                    <
    option value="0">0</option>
                                                                    <
    option value="1">1</option>
                                                                    <
    option value="2">2</option>
                                                                    <
    option value="3">3</option>
                                                                    <
    option value="4">4</option>
                                                                    <
    option value="5">5</option>
                                                                    <
    option value="6">6</option>
                                                                    <
    option value="7">7</option>
                                                                    <
    option value="8">8</option>
                                                                    <
    option value="9">9</option>
                                                                    <
    option value="10">10</option>
                                    </
    select></td></tr>
                                    </
    table>
    <
    script type="text/javascript">
    var 
    sels=document.getElementById("stats").getElementsByTagName("select");
    var 
    len=sels.length;
    var 
    selval;

    for (var 
    0leni++) {
    sels[i].onclick=function(){selval=this.selectedIndex}
    sels[i].onchange=function(){
    vals=0;
    for (var 
    0lena++) {
    vals+=Number(sels[a].value);
    }
    statsleft=10-vals;
    if (
    this.value>statsleft+Number(this.value)){
    alert("you have exceeded the limit. Please choose a lower number");
    this.selectedIndex=selval;
    return;
    } else{
    if(
    statsleft==0){
    document.getElementById("mess").innerHTML="0";
    document.getElementById("mess").style.color="red"
            
    } else {
    document.getElementById("mess").innerHTML=statsleft;
    document.getElementById("mess").style.color="black"

                
    }
            }
        }
    }

    </script> 
    The JS code makes sure no more than 10 can be selected. But after that I need PHP to check to make sure it's 10 and not 5, 8, 9, etc. How would I process this? Thanks
    Last edited by myfayt; 04-18-2012 at 03:13 AM.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    It looks like you have a <form> that javascript affects, but you don't submit it to a PHP script?
    Is there more script that you are not showing?

    For the PHP part, you could write a cookie using javascripting. Then read the cookie with PHP.

    Here's function to write a cookie (javascripting):
    Code:
    function createCookie(name,value,days) {
    	if (days) {
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}
    	else var expires = "";
    	document.cookie = name+"="+value+expires+"; path=/";
    }
    function eraseCookie(name) {
    	createCookie(name,"",-1);
    }

    You would insert it about here (in your script)

    } else{
    if(statsleft==0){
    Cookies.create('points',statsleft,7);
    document.getElementById("mess").innerHTML="0";

    Now, at any time, you can read that cookie with PHP:

    $points=$_COOKIE['points'];


    .

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Sorry this is the main button.

    PHP Code:
    <br />
                <
    input type="submit" name="process" value="Register">
              </
    form
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    So in the PHP script, you view the form variables and add them up?
    I'm not sure what "10" means.

    $focus=$_POST['focus'];
    $defense=$_POST['defense'];
    $attack=$_POST['attack'];
    .
    .etc.

    $total=$focus+$defense+$attack;


    .

  • #5
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Ah true, man I should take the day off for not remember this stuff.

    Basically it's

    PHP Code:
    $stattotal $attack+$magic+$focus+defense;

    if (
    $stattotal != 10) {
    //Error

    10 means all stats must equal 10 between all fields. Such as 3 focus, 4 attack, 2 defense, 1 magic. It's starter stats when they make a new account, this is so they can customize it.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Alternatively you could use arrays from the selections, and name them along the lines of stats[attack], stats[focus], etc. Then an array_sum would give you the end result of the count of selected.

  • #7
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    I have no clue how to do that Kev, I try not to mess with arrays, know little about them.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #8
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    It's not working, it always gives me an error if the stats equal 10 or not.

    PHP Code:
    $stattotal $focus $attack $defense $dexterity $magicdefense;

    if (
    $stattotal != 10) {
             
    $errors++; $errorlist .= "Your starter stats do not equal 10.<br />";
            } 
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #9
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    24 hour bump
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    What's the result of $stattotal?
    I assume $focus, $attack, etc were extracted from the superglobal?

  • #11
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    The result was 0, even tried to manually assign it to a _POST variable after extract all, which was blank.


    PHP Code:
    if (isset($_POST["process"])) {
         
    $ip $_SERVER['REMOTE_ADDR'];
            
    extract($_POST);



            
    $errors 0$errorlist "";

            
    $stattotal $focus $attack $defense $dexterity $magicdefense;

            echo 
    "Stat Total: $stattotal"
    PHP Code:
     if ($stattotal != 10) {
             
    $errors++; $errorlist .= "Your starter stats do not equal 10.<br />";
            } 
    PHP Code:
    <li>You have <b><span id="mess">10</b></spanstats remaining.</li><br />
                                                            <
    li>
                                                            <
    table id="stats" width="75%">
                                                            <
    tr><td>
                                    <
    b>Focus</b></td><td><select name="focus">
                                    <
    option value="0">0</option>
                                                                    <
    option value="1">1</option>
                                                                    <
    option value="2">2</option>
                                                                    <
    option value="3">3</option>
                                                                    <
    option value="4">4</option>
                                                                    <
    option value="5">5</option>
                                                                    <
    option value="6">6</option>
                                                                    <
    option value="7">7</option>
                                                                    <
    option value="8">8</option>
                                                                    <
    option value="9">9</option>
                                                                    <
    option value="10">10</option>
                                    </
    select></td></tr>
                                                                    <
    tr><td>
                                                                    <
    b>Attack</b></td><td><select name="attack">
                                                                    <
    option value="0">0</option>
                                                                    <
    option value="1">1</option>
                                                                    <
    option value="2">2</option>
                                                                    <
    option value="3">3</option>
                                                                    <
    option value="4">4</option>
                                                                    <
    option value="5">5</option>
                                                                    <
    option value="6">6</option>
                                                                    <
    option value="7">7</option>
                                                                    <
    option value="8">8</option>
                                                                    <
    option value="9">9</option>
                                                                    <
    option value="10">10</option>
                                    </
    select></td></tr>

                                    <
    tr><td>
                                                                    <
    b>Defense</b></td><td><select name="defense">
                                                                    <
    option value="0">0</option>
                                                                    <
    option value="1">1</option>
                                                                    <
    option value="2">2</option>
                                                                    <
    option value="3">3</option>
                                                                    <
    option value="4">4</option>
                                                                    <
    option value="5">5</option>
                                                                    <
    option value="6">6</option>
                                                                    <
    option value="7">7</option>
                                                                    <
    option value="8">8</option>
                                                                    <
    option value="9">9</option>
                                                                    <
    option value="10">10</option>
                                    </
    select></td></tr>

                                    <
    tr><td>
                                                                    <
    b>Dexterity</b></td><td><select name="dexterity">
                                                                    <
    option value="0">0</option>
                                                                    <
    option value="1">1</option>
                                                                    <
    option value="2">2</option>
                                                                    <
    option value="3">3</option>
                                                                    <
    option value="4">4</option>
                                                                    <
    option value="5">5</option>
                                                                    <
    option value="6">6</option>
                                                                    <
    option value="7">7</option>
                                                                    <
    option value="8">8</option>
                                                                    <
    option value="9">9</option>
                                                                    <
    option value="10">10</option>
                                    </
    select></td></tr>

                                    <
    tr><td>
                                                                    <
    b>Magic Defense</b></td><td><select name="magicdefense">
                                                                    <
    option value="0">0</option>
                                                                    <
    option value="1">1</option>
                                                                    <
    option value="2">2</option>
                                                                    <
    option value="3">3</option>
                                                                    <
    option value="4">4</option>
                                                                    <
    option value="5">5</option>
                                                                    <
    option value="6">6</option>
                                                                    <
    option value="7">7</option>
                                                                    <
    option value="8">8</option>
                                                                    <
    option value="9">9</option>
                                                                    <
    option value="10">10</option>
                                    </
    select></td></tr>
                                    </
    table>
    <
    script type="text/javascript">
    var 
    sels=document.getElementById("stats").getElementsByTagName("select");
    var 
    len=sels.length;
    var 
    selval;

    for (var 
    0leni++) {
    sels[i].onclick=function(){selval=this.selectedIndex}
    sels[i].onchange=function(){
    vals=0;
    for (var 
    0lena++) {
    vals+=Number(sels[a].value);
    }
    statsleft=10-vals;
    if (
    this.value>statsleft+Number(this.value)){
    alert("you have exceeded the limit. Please choose a lower number");
    this.selectedIndex=selval;
    return;
    } else{
    if(
    statsleft==0){
    document.getElementById("mess").innerHTML="0";
    document.getElementById("mess").style.color="red"
            
    } else {
    document.getElementById("mess").innerHTML=statsleft;
    document.getElementById("mess").style.color="black"

                
    }
            }
        }
    }

    </script>
                                    </ul>
                            </li> 
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #12
    New Coder
    Join Date
    Oct 2010
    Posts
    56
    Thanks
    2
    Thanked 0 Times in 0 Posts
    You could do it pretty easily by adding an onChange function to your select, something like:

    Code:
    <li>You have <b><span id="mess">10</b></span> stats remaining.</li><br /> 
                                                            <li> 
    // Create a form
    <form name="form1">
                                                            <table id="stats" width="75%"> 
                                                            <tr><td> 
                                    <b>Focus</b></td><td><select name="focus" onChange="focuschg()"> 
                                    <option value="0">0</option> 
                                                                    <option value="1">1</option> 
                                                                    <option value="2">2</option> 
                                                                    <option value="3">3</option> 
                                                                    <option value="4">4</option> 
                                                                    <option value="5">5</option> 
                                                                    <option value="6">6</option> 
                                                                    <option value="7">7</option> 
                                                                    <option value="8">8</option> 
                                                                    <option value="9">9</option> 
                                                                    <option value="10">10</option> 
                                    </select></td></tr> 
                                                                    <tr><td> 
                                                                    <b>Attack</b></td><td><select name="attack" onChange="attchg()"> 
                                                                    <option value="0">0</option> 
                                                                    <option value="1">1</option> 
                                                                    <option value="2">2</option> 
                                                                    <option value="3">3</option> 
                                                                    <option value="4">4</option> 
                                                                    <option value="5">5</option> 
                                                                    <option value="6">6</option> 
                                                                    <option value="7">7</option> 
                                                                    <option value="8">8</option> 
                                                                    <option value="9">9</option> 
                                                                    <option value="10">10</option> 
                                    </select></td></tr> 
    
                                    <tr><td> 
                                                                    <b>Defense</b></td><td><select name="defense" onChange="defchg()"> 
                                                                    <option value="0">0</option> 
                                                                    <option value="1">1</option> 
                                                                    <option value="2">2</option> 
                                                                    <option value="3">3</option> 
                                                                    <option value="4">4</option> 
                                                                    <option value="5">5</option> 
                                                                    <option value="6">6</option> 
                                                                    <option value="7">7</option> 
                                                                    <option value="8">8</option> 
                                                                    <option value="9">9</option> 
                                                                    <option value="10">10</option> 
                                    </select></td></tr> 
    
                                    <tr><td> 
                                                                    <b>Dexterity</b></td><td><select name="dexterity" onChange="dexchg()"> 
                                                                    <option value="0">0</option> 
                                                                    <option value="1">1</option> 
                                                                    <option value="2">2</option> 
                                                                    <option value="3">3</option> 
                                                                    <option value="4">4</option> 
                                                                    <option value="5">5</option> 
                                                                    <option value="6">6</option> 
                                                                    <option value="7">7</option> 
                                                                    <option value="8">8</option> 
                                                                    <option value="9">9</option> 
                                                                    <option value="10">10</option> 
                                    </select></td></tr> 
    
                                    <tr><td> 
                                                                    <b>Magic Defense</b></td><td><select name="magicdefense" onChange="magdefchg()"> 
                                                                    <option value="0">0</option> 
                                                                    <option value="1">1</option> 
                                                                    <option value="2">2</option> 
                                                                    <option value="3">3</option> 
                                                                    <option value="4">4</option> 
                                                                    <option value="5">5</option> 
                                                                    <option value="6">6</option> 
                                                                    <option value="7">7</option> 
                                                                    <option value="8">8</option> 
                                                                    <option value="9">9</option> 
                                                                    <option value="10">10</option> 
                                    </select></td></tr> 
                                    </table>
    								<input type="hidden" name="hinp" id="hinp"></input>
    								
    								</form>
    <script type="text/javascript"> 
    var sels=document.getElementById("stats").getElementsByTagName("select"); 
    var len=sels.length; 
    var selval; 
    
    for (var i = 0; i < len; i++) { 
    sels[i].onclick=function(){selval=this.selectedIndex} 
    sels[i].onchange=function(){ 
    vals=0; 
    for (var a = 0; a < len; a++) { 
    vals+=Number(sels[a].value); 
    } 
    statsleft=10-vals; 
    if (this.value>statsleft+Number(this.value)){ 
    alert("you have exceeded the limit. Please choose a lower number"); 
    this.selectedIndex=selval; 
    return; 
    } else{ 
    if(statsleft==0){ 
    document.getElementById("mess").innerHTML="0"; 
    document.getElementById("mess").style.color="red" 
            } else { 
    document.getElementById("mess").innerHTML=statsleft; 
    document.getElementById("mess").style.color="black" 
    
                } 
            } 
        } 
    } 
    
    focuschg()
    	{
    	var fCh = document.form1.focus.options[document.form1.focus.selectedIndex].value;
    	totalStats();
    	}
    attchg()
    	{
    	var aCh = document.form1.attack.options[document.form1.attack.selectedIndex].value;
    	totalStats();
    	}
    defchg()
    	{
    	var defCh = document.form1.defense.options[document.form1.defense.selectedIndex].value;
    	totalStats();
    	}
    dexchg()
    	{
    	var dexCh = document.form1.dexterity.options[document.form1.dexterity.selectedIndex].value;
    	totalStats();
    	}
    magdechg()
    	{
    	var mCh = document.form1.magicdefense.options[document.form1.magicdefense.selectedIndex].value;
    	totalStats();
    	}
    totalStats()
    	{
    	var tStats = fCh + aCh + defCh + dexCh + mCh;
    	document.form1.hinp.value = tStats;
    	}
    
    </script> 
                                    </ul> 
                            </li>
    I did not test this, but it should basically do what you want. Then just get the value of the hidden input (hinp) with _POST.

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Where's your form tags? Are you sure its passed via post?

  • Users who have thanked Fou-Lu for this post:

    myfayt (04-18-2012)

  • #14
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Quote Originally Posted by Fou-Lu View Post
    Where's your form tags? Are you sure its passed via post?
    Your right! I had the form starter tag in the wrong spot, it didn't start until after the stats. Thanks man
    Been a sign maker for 7 years. My business:
    American Made Signs


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •