...

View Full Version : onClick() add id to array



bubbles19518
02-17-2007, 01:49 AM
Im doing some PHP work for this guy, converting an old script to a new site. Here is a sample checkbox used in the script:


<input type=checkbox name=entries[] value=$rid onclick=Check(this,$rid)>


$rid is the value I want added to the entries[] array when they click the checkbox.

Here is the javascript


<script>
function DeleteAllEntries(url)
{
var msg = 'Delete all of your entries?\n\nYes - Click OK\nNo - Click Cancel\n\n'
+ 'Note that any new entries received from the\ntime this page was loaded will be deleted too.';
if(confirm(msg)) document.location.href = url;
}
function NumberOfChecks()
{
var cbs = document.getElementsByName('entries[]');
var i, n, num_checks;
n = cbs.length;
num_checks = 0;
for(i=0; i<n; i++)
{
if(cbs[i].checked) num_checks++;
}
return num_checks;
}
function EnableDisableDeleteButton()
{
var db = document.getElementById('delete_button');
if(NumberOfChecks()) db.disabled = false;
else db.disabled = true;
}
function SetTdStyle(td_id, bgcolor, bordercolor)
{
var td;
td = document.getElementById(td_id);
td.style.fontFamily = 'Verdana';
td.style.fontSize = '12px';
td.style.width = '90px';
td.style.backgroundColor = bgcolor;
td.style.borderWidth = '1px';
td.style.borderColor = bordercolor;
td.style.borderStyle = 'single';
}
function Check(this_cb, td_id)
{
var td;
td = document.getElementById(td_id);
if(this_cb.checked)
{
SetTdStyle(td_id, '#aa2222', '#000000');
}
else
{
td.setAttribute('bgColor','#FFFFFF');
SetTdStyle(td_id, '#ffffff', '#777777');
}
EnableDisableDeleteButton();
}
function CheckAll(this_cb)
{
var cbs = document.getElementsByName('entries[]');
var i, n, td, td_id;
n = cbs.length;
if(this_cb.checked)
{
for(i=0; i<n; i++)
{
cbs[i].checked = true;
td_id = 'td' + (i+8);
td = document.getElementById(td_id);
td.setAttribute('bgColor','#B0B0B0');
SetTdStyle(td_id, '#B0B0B0', '#000000');
}
}
else
{
for(i=0; i<n; i++)
{
cbs[i].checked = false;
td_id = 'td' + (i+1);
td = document.getElementById(td_id);
td.setAttribute('bgColor','#FFFFFF');
SetTdStyle(td_id, '#000000', '#000000');
}
}
EnableDisableDeleteButton();
}
function ConfirmDelete()
{
if(confirm('Delete the selected entries?'))
{
document.forms.delete_entries_form.submit();
}
}
function findPosX(obj){var curleft = 0;if(obj.offsetParent)while(1) {curleft += obj.offsetLeft;if(!obj.offsetParent)break;obj = obj.offsetParent;}else if(obj.x)curleft += obj.x;return curleft;}
function findPosY(obj){var curtop = 0;if(obj.offsetParent)while(1){curtop += obj.offsetTop;if(!obj.offsetParent)break;obj = obj.offsetParent;}else if(obj.y)curtop += obj.y;return curtop;}
function Details(dtid,dtdvid)
{
var dta=document.getElementById(dtid);
var dtdv=document.getElementById(dtdvid);
if(dtdv.style.display=='none')
{
dtdv.style.display='block';
var w;
if(window.innerWidth) w = window.innerWidth;
else if(document.body.offsetWidth) w = document.body.offsetWidth;
else w = 1024;
var x = (findPosX(dta)-90);
if(x < 10) x = 10;
if(x+90 > w) x = w - 90;
x = Math.floor(x);
dtdv.style.left = x + 'px';
dtdv.style.top = (findPosY(dta)+18) + 'px';
}
else
{
dtdv.style.display='none';
}
}
</script>

I submit the entries array with this code


<form method=POST action=delete.php name=delete_entries_form><input id=delete_button type=submit onclick=javascript:ConfirmDelete(); value=Delete&nbsp;Hit(s) disabled>


This code works on the old site, but wont work on the new one... I just need the value of the checkbox to be added to the entries array when the checkbox is checked

chump2877
02-17-2007, 02:36 AM
As far as I know, that checkbox already has a value of $rid and is in the entries[] POST array, regardless if the checkbox is checked or not --- and you should be able to verify this in the PHP script that receives this form...

In your PHP script, when you add this to your code:


var_dump($_POST['entries']);Do you see the value of the checkbox in the result?

chump2877
02-17-2007, 02:57 AM
If you;re trying to conditionally add the value of the checkbox to the entries[] array depending on whether or not your checkbox is checked, then perhaps you need something like this (I didn't test this code):


<input type="checkbox" name="whatever" value="myValue" onclick="Check(this,document.delete_entries_form);" />

<script type="text/javascript">

function Check(inputObj,formObj)
{
if (inputObj.checked)
{
var newInput = document.createElement("input");
newInput.setAttribute("type","hidden");
newInput.setAttribute("name","entries[]");
newInput.setAttribute("value",inputObj.value);
formObj.appendChild(newInput);
}
}

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum