PDA

View Full Version : Need help with manipulating an array



jamefarm
Mar 23rd, 2007, 10:37 PM
I am working on a program using PHP and JavaScript that can access a database table, output the correct information and allow you to delete/add entries. The code I have currently finds all the relevant db entries and posts them dynamically to page after a refresh, with a checkbox next to each so that a user (me) can select any entries and delete them from the database. I have all the PHP code working, but I'm having a problem working with an array that tallies all the checked check boxes and places the variable for each into an array to hand off to PHP. Here is the relevant code:

this is the code that generates the checkboxes

<? if( ($listcon==1 ) && $ok==1) {
$k = 0;
echo "<table cellpadding=2 cellspacing=1 align=center>";
if( trim($stksym) == "") {
$sql = "SELECT * FROM testopt5 ORDER BY totsym asc";
} else {
$sql = "SELECT * FROM testopt5,RawTotSym WHERE RawTotSym.stksym='$stksym' and RawTotSym.totsym = testopt5.totsym";
}
$Split_out = @mysql_query($sql,$connection) or die(" Option Symbols database error");
while ($row = mysql_fetch_array($Split_out)) {
$totsym = trim($row['totsym']);
echo "<tr><td><input type=checkbox name=\"Chkbx[$k]\" onClick=\"chkdel[$k]\" value=\"$totsym\"></td><td> $totsym </td></tr>";
$SymOkay[$totsym] = 1;
$k++;
}
echo "</table>";
echo "<center><a href=\"JavaScript:DoDel()\"><b class=greenlinkbig>Do It</b></a></center>";
}
?>

<? if($listcon==6) {
echo $DelAray;
}
?>


Here is the code that I need to manipulate an array for those checkboxes:



function chkdel(a) {
xx = "var zz = document.form0.Chkbox"+a+".checked";
eval(xx);
if( zz==true ) {
xx = "document.form0.del"+a+".value=1";
} else {
xx = "document.form0.del"+a+".value=0";
}
eval(xx);
}

function DoDel() {
var DelAray = new Array();
for( del"a" == 1){
DelAray[] = $totsym;
}
document.form0.listcon.value=6;
document.form0.action="http://<? echo "$server"; ?>jftestopt5.php";
document.form0.submit();
}

brandonH
Mar 24th, 2007, 06:14 AM
several errors here.

first you are setting a var while stting a var.....

xx = "var zz = document.form0.Chkbox"+a+".checked";

what are you trying to sett xx to? it looks like you are trying to set xx to true or false, but you the way you are trying wont work.

should do as so:


function chkdel(a) {
xx=document.form0["Chkbox"+a].checked?1:0;
//sets xx to true if the checkbox is checked, else sets xx to false
if(xx) {//<-if xx is true
document.form0["del"+a].value="1";
} else {
document.form0["del"+a].value="0";
}
}


what are you doing here? i assume you are trying to loop through the form elements with names starting with del and using the variable a. unfortunately the variable no longer exsists, it is not a global variable, and you only used it in the first function, as soon as the first function ends, the variable no longer exsists. make sense?



function DoDel() {
var DelAray = new Array();
for( del"a" == 1){
DelAray[] = $totsym;
}
document.form0.listcon.value=6;
document.form0.action="http://<? echo "$server"; ?>jftestopt5.php";
document.form0.submit();
}


pay close attention to the changes, you have similar errors in your second function

jamefarm
Mar 25th, 2007, 08:52 PM
brandonH, thank you for the reply. So in order to make the variables global, I just need to declare them outside of a funtion, correct? what I need it to do is cycle through all the checkboxes, names Chkbx[$k] and determine which are checked, then I need the value $totsym for each checked checkbox to be put into an array that I can use in a sql command to delete those values. Maybe that can be done in one funtion? I have implemented your changes on the first function, but I'm still at a loss on how to get this to work.

glenngv
Mar 26th, 2007, 07:02 AM
When the form gets submitted, the values of the checked checkboxes get submitted to the server. From there, you can access those in PHP and delete the associated records in the database. You don't have to put it in an array.

And as you noticed, brandonH didn't use eval and instead used square bracket notation. You can see more info on this in my sig.