...

View Full Version : Resolved Ran into a problem



myfayt
04-16-2012, 12:59 PM
I have multiple select boxes which I need to check if all equal 10, and isn't less.


<li>You have <b><span id="mess">10</b></span> stats 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 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"

}
}
}
}

</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

mlseim
04-16-2012, 01:32 PM
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):


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'];


.

myfayt
04-16-2012, 04:48 PM
Sorry this is the main button.


<br />
<input type="submit" name="process" value="Register">
</form>

mlseim
04-16-2012, 05:08 PM
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;


.

myfayt
04-16-2012, 05:19 PM
Ah true, man I should take the day off for not remember this stuff.

Basically it's



$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.

Fou-Lu
04-16-2012, 06:38 PM
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.

myfayt
04-16-2012, 06:40 PM
I have no clue how to do that Kev, I try not to mess with arrays, know little about them.

myfayt
04-17-2012, 12:56 AM
It's not working, it always gives me an error if the stats equal 10 or not.



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

if ($stattotal != 10) {
$errors++; $errorlist .= "Your starter stats do not equal 10.<br />";
}

myfayt
04-17-2012, 07:33 PM
24 hour bump

Fou-Lu
04-17-2012, 07:36 PM
What's the result of $stattotal?
I assume $focus, $attack, etc were extracted from the superglobal?

myfayt
04-18-2012, 12:55 AM
The result was 0, even tried to manually assign it to a _POST variable after extract all, which was blank.



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



$errors = 0; $errorlist = "";

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

echo "Stat Total: $stattotal";


if ($stattotal != 10) {
$errors++; $errorlist .= "Your starter stats do not equal 10.<br />";
}



<li>You have <b><span id="mess">10</b></span> stats 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 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"

}
}
}
}

</script>
</ul>
</li>

Prime8
04-18-2012, 01:28 AM
You could do it pretty easily by adding an onChange function to your select, something like:



<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.

Fou-Lu
04-18-2012, 02:07 AM
Where's your form tags? Are you sure its passed via post?

myfayt
04-18-2012, 03:12 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum