Jump to content

Storing Values Of List Box To An Array Through For Loop.


chitchirya

Recommended Posts

I have a big problem with this. My combo boxes are looped through do while and I can not store the values of it in my array. These are my current codes: in home.php where its resource values

 function showPoint(spp,sname){if (spp=="")   {   document.getElementById("sp").innerHTML="";   return;   }if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp4=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp4=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp4.onreadystatechange=function()  {  if (xmlhttp4.readyState==4 && xmlhttp4.status==200)	{	document.getElementById("sp").innerHTML=xmlhttp4.responseText;	}  }xmlhttp4.open("GET","showpoints2.php?spp="+spp+"&sname="+sname+"&spa="+spa,true);xmlhttp4.send();}

in showpoints2.php (values comes from home.php)

<?phprequire_once("connections.php");$points[]= $_GET["spp"];$clevel = $_GET["spa"];$sname = $_GET["sname"];$i = 0;for($i=0;$i<count($points);$i++){$total = $points[$i];}echo $total;?>

Once I print the $total, it only displays the current selection i have chosen. Please help this is the only last thing I got to work. God bless everyone! =)

Link to comment
Share on other sites

  • Replies 60
  • Created
  • Last Reply
$points[]= $_GET["spp"];

Your are not adding multiple array keys here so it will only be one key. Which means what you will not need to put it in an array and you don't need to loop it in a for loop either. if you want to send an array through a GET or POST request you need to set the name of the key as an place-holder(what ever is called) for an array like this:
?key1[]=value1&key1[]=value2&key2=value3<?phpprint_r($_GET);?>Will output:<pre>array('key1' => array('0' => 'value1','1' => 'value2'),'key2' => 'value3');</pre>

Link to comment
Share on other sites

Ohh... Well, I tried it and it worked but how will i store the values from the array key1[] being passed to showpoints2.php? I tried xmlhttp4.open("GET","showpoints2.php?spp[]="+spp+"&sname="+sname+"&spa="+spa,true); and it worked somehow, $points = $_GET['key1[]']; does not get the values of the array. how can I add the values in it? Hopefully you can help me =) I have so many questions though...

Link to comment
Share on other sites

First of all, what is spp? Where are you getting it from? The second thing is that, when passing arrays through GET or POST, the square brackets go outside the array key:

// Given the query string key1[]=value1&key1[]= value2 $points = $_GET['key1'];echo $points[0]; // Prints "value1"echo $points[1]; // Prints "value2"

Link to comment
Share on other sites

First of all, what is spp? Where are you getting it from? The second thing is that, when passing arrays through GET or POST, the square brackets go outside the array key:
// Given the query string key1[]=value1&key1[]= value2  $points = $_GET['key1'];echo $points[0]; // Prints "value1"echo $points[1]; // Prints "value2"

This is where I am getting my spp. (Skill Points Passed) showPoint(this.value) is my spp and this.name is my sname
 do{ echo "<p><tr>";echo "<th><select name=".addslashes($result['skill_name'])." onChange='showRlvl(this.value,this.name); showEffect(this.value,this.name,".$result['skill_id']."); showPoint(this.value,this.name)'><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option<option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option><option>16</option><option>17</option><option>18</option><option>19</option><option>20</option> </select></th>";echo "<td>For Image</td>";//$abc = ;//$abc = str_replace("_", " ", $abc);echo "<td>".$result['skill_name']."</td>";echo "<th id=".$result['skill_name'].">".$result['skill_level_req']."</th>";echo "<th id=".$result['skill_id'].">".$result['skill_effect']."</th>";echo "<th>".$result['skill_cd']." second(s)</th>";echo "<th>".$result['skill_ct']." second(s)</th>";echo "<th>".$result['skill_range']." meter(s)</th>";echo "</tr></p>";$result = mysql_fetch_assoc($query);}while($result);

Hmmm I will try the code of yours, thanks! =)

Link to comment
Share on other sites

It does work also and getting the value that I'm clicking but it's not arrayed. When I click 1 it shows 1, and when I click 5 it shows 5 only not 15.

<?php$points = $_GET['spp'];$count = count($points);$i = 0;for($i=0; $i<$count; $i++){echo $points[$i];}?>

The value that gets passed to showPoint is a single value, not an array. Where is the array supposed to come in?
Every click of the user to the combo box. Is there any other method how would I get it without page reloading?
Link to comment
Share on other sites

maybe your after something like this:
spps = [];function storeSPP(spp) {	spps.push(spp);}

storeSPP(spp);var URL = "showpoints2.php?spp[]="+spps.join('&spp[]=')+"&sname="+sname+"&spa="+spa;xmlhttp4.open("GET",URL,true);xmlhttp4.send();

Thanks for this, it works. Now another problem is bothering me. But i'll try if I can do it first, then i'll post again here if I can't. Thanks to all. Your all big help.God bless and more power! Edit: Ahh wait, How could I make my sname's (Skill Name) be the index of the spp's?Because I think that if user click again the same combo box the value should be overwritten.So I think that If I make my sname be the index, I overwrite the same index with its new value.Any help would much be appreciated.
Link to comment
Share on other sites

My script is this, showPoint(this.value,this.name) what I want to do now is to make my sname be the indexes of my spp's. My spp's depends on the combo box name. this.value are my spp's and this.name are my sname's. Because I think that if user click again the same combo box the value should be overwritten.So I think that If I make my sname be the index, I can overwrite the same index with its new value.Say, I clicked the combo box which has a name of abc and I pointed out 3. So the array will have a value of 3 with the index of abc.And I clicked again abc and I pointed out 7. So the array will erase the old value 3 and have the value of 7 with the index of abc. Hmm.. Tell me if you really can't understand it. I'm having hard time explaining it to english though. Lol! =)

Link to comment
Share on other sites

Maybe you want "a" to be an object of which the property sname is an array containing all the elements of spp.var a[sname] = spp; Remember that a is local to the function, so it will be destroyed when the function stops running. If you want it to stay, then declare it in the global scope. By the way, your <option> elements should have a value property. Some browsers will send empty values if there isn't one:

<option value="0">0</option><option value="1">0</option>

Link to comment
Share on other sites

Yes, it's like that but how will I make it pushed every click of the user? like this spps = [];function storeSPP(spp) { spps.push(spp);}My spp is store in an array which is spps. Everytime I click a value, it stores the value. so in this case,say,a[sname] = spp; for example i clicked value 5 in the combo box named abc, will it be stored?then i clicked value 16 in the combo box named abcd, will it be stored? Hmm.... Thanks for this. <option value="0">0</option><option value="1">0</option>

Link to comment
Share on other sites

You can so that. If you click on the same input again, it will overwrite the previous value that was stored. You want something like this:

var inputs = [];function storeValues(value, name) {  inputs[name] = value;}

Link to comment
Share on other sites

Ok, now I understand what you want. You're just storing all the values so you can send them to PHP. First, I need to make a correction: this is an object, not an array, so I use curly braces instead of square brackets:

var inputs = {}; // { } indicates an object. function storeValues(value, name) {   inputs[name] = value;}

When sending the data through AJAX, we just generate the query string from the inputs.

query = "?"; for(var name in inputs) {  if(!(name in Object.prototype) ) {    query += "spp[" + name + "]=" + inputs[name] + "&";  }}var URL = "showpoints2.php" + query;xmlhttp4.open("GET",URL,true);xmlhttp4.send();

Link to comment
Share on other sites

What do you need the array for if you're only changing one value at a time?

function showPoint(value,name){if (value=="")   {   document.getElementById("sp").innerHTML="";   return;   }if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp4=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp4=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp4.onreadystatechange=function()  {  if (xmlhttp4.readyState==4 && xmlhttp4.status==200)		{		document.getElementById("sp").innerHTML=xmlhttp4.responseText;		}  }xmlhttp4.open("GET","showpoints2.php?" + name + "=" + value);xmlhttp4.send();}

This sends one value: the one that you just changed. It should be available in the $_GET array under the name of the input that has been changed.

Link to comment
Share on other sites

It keeps changing, yes. But since there's only one single value, you can just use a foreach() loop on the $_GET array. foreach($_GET as $sname=>$value) { // Do something with $sname and its value.}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...