Problem with Global Array in PHP
Posted
by Suezy
on Stack Overflow
See other posts from Stack Overflow
or by Suezy
Published on 2010-03-15T06:25:55Z
Indexed on
2010/03/15
6:29 UTC
Read the original article
Hit count: 224
Hi guys,, need some help pls.. i have a PHP code here that lets a user send a message to multiple recipients using the "phonebook" (from database) as the main list. I used an array to temporarily hold all the desired recipients for the current user. So I have a two box, with each for displaying. The user will clicks the name from the phonebook lisy and clicks "Add" button to add on the temporary list on the other select box. The problem is, whenever I use the "onChange" function in javascript; the array accepts only one recipients, and when I try to add, it replaces the first one. :(
Here is my code:
$recipients = array();
// How do I place the array here?
function reload_page(){
i=document.maillinglist.firstletter.selectedIndex;
this.location='./group_msg.php?firstletter='+document.maillinglist.firstletter.options[i].value;
}
function add(){ }
$conn = pg_connect("host=localhost user=sa dbname=messaging");
if(isset($_GET['firstletter'])){
$letter=$_GET['firstletter'];
} else {
$letter='a';}
print ""; print "
GROUP MESSAGE
";print ""; print ""; print ""; print "";
for($chr_loop=97;$chr_loop<=122;$chr_loop++){
$alphabet = chr($chr_loop);
# Displays list of users arranged by letters - this works fine
if($alphabet==$letter){
print "\t\t<option value='$alphabet' selected>-------------------- $alphabet --------------------</option>\n";
} else {
print "\t\t<option value='$alphabet'>-------------------- $alphabet --------------------</option>\n";}
}
print "";
print "<td><select style='width:210px' name=\"non_members[]\" size=10 width=200>";
$sql = "select name from test_phonebook where name like '$letter%';";
$result = pg_query($conn, $sql);
# Display users from phonebook using the letter chosen
while($row = pg_fetch_assoc($result)){
$my_name = $row['name'];
print "<option width=200 value=\"$my_name\">$my_name</option>";
}
print "</td>";
print "</select>";
print "<td><input type=submit name=add value=\"->>\" onClick=\"add()\"><br><input type=submit name=del value=\"<<-\"></td>";
print "<td><select name=\"members[]\" style='width:210px' size=10>";
# Display temporary recipients - problem: ONLY DISPLAYS ONE VALUE
while (list ($key, $val) = each ($recipients)) {
echo "$key -> $val <br>";
print "<option width=200 value=\"$val\">$val</option>";
}
print "</select></table></form>";
My aim here, is to place all recipients that the user desires to an array, without losing the previous value added.
© Stack Overflow or respective owner