Hi.
I'm pretty new to php and javascript.
Perhaps I'm going about this all wrong, so any ideas will be appreciated...
I've got a php-generated form that calls an external javascript function (through onclick of button input) to validate an array of checkboxes.
If the user presses ok on the javascript confirm box, it does a submit of the form.
The checkboxes are a javascript, rather than php array:
What I need to do is get the array of checked boxes from my javascript array into a php array in the php script called by the submit action.
Here's the javascript:
function CheckboxTest(list) {
total = "";
outArray = new Array();
outCount = 0;
for (n=0; n < list.length; n++) {
if (list[n].checked) {
total += list[n].value + "\n";
}
}
if (total == "") {
alert("No direct reports have been selected");
}
else {
alertMessage = "You have selected the following users\n";
alertMessage = alertMessage + "for Patrol Console deletion:\n\n";
alertMessage = alertMessage + total + "\n";
if (confirm(alertMessage)) {
document.myForm.submit();
}
}
}
So, it looks like you need help creating the ProcessConsoleAudit.php file.
The first problem is, you're giving every checkbox the same name "uids".
You should change this to uids[] (this will turn it into an array that is submitted to the next page)
Since you are using post as the form method, this will grab the values in ProcessConsoleAudit.php
$chkbx_array = $_POST['uids'];
And this should help you debug the values to see what was checked:
echo "<pre>\n";
print_r($chkbx_array);
echo "</pre>\n";
I haven't tested it, so let me know if you run into any problems
Thanks for the reply irf2k.
I had tried uids[], but then the javascript doesn't know what to do with it. That's the essence of the problem. By using uids (no brackets), it creates an array javascript can handle, but then how do I process it in ProcessConsoleAudit.php?
I really want both javascript and php.
javascript for presenting a confirmation box and php for back-end processing.
I've actually got it nailed now, though,
I'm using toString to convert the javascript array to a (comma-separated) string and passing it back into the submitted form as a hidden input field.
Works great.
Hy,
You can use name="uids[]" for use it with php, and also add a class="uids" for use it with JavaScript.
Then, in JS you need to get and parse the elements with class="uids"
Code:
var get_tags = document.getElementsByTagName('input');
var uidss = new Array();
var u = 0;
// traverse the get_tags array
for (var i=0; i<get_tags.length; i++) {
if (get_tags[i].className=='uids') {
uidss[u] = get_tags[i];
u++;
}
}
// ... then parse the uidss
Bookmarks