Page 1 of 1

Pass 2 parameters to javascript function from <INPUT onclick

Posted: Tue Jul 15, 2008 10:31 am
by graytest
Hi

Judging by the number of posts I've seen on this, there's a fair bit of confusion as how to code the syntax for passing two parameters from php to a javascript? This includes me :D .

I have a short function to make sure only one checkbox ever gets clicked. It appears in "view source" as follows:-

<script language="JavaScript"><!--
maxBoxes = 1;
boxesChecked = 0;
function checkIt(theBox, my_msg) {
if (boxesChecked+1 > maxBoxes) {
alert('error msg='+my_msg);
return false;
}
boxesChecked++
return true;
}
//-->
</script>

I call it from an input field which gets rendered as below, from "view source":-

<input type='checkbox' onClick='if (this.checked) return checkIt(this,'Only one Selection box can be ticked'); else boxesChecked--' name="del_me[]" value="54">

It's coded in php as:-

$msgline = $my_page['msg20'];
echo "<input type='checkbox' onClick='if (this.checked) return checkIt(this,'".$msgline."'); else boxesChecked--' name=\"del_me[]\" value=\"$row[holiday_id]\">";


When I call this with one parameter i.e. return checkIt(this), the function runs fine but when I add in the extra parameter with the string I want in the pop-up window, the function doesn't appear to run at all?

I've run out of syntax steam now and wondered if someone could help me?

Thanks

Re: Pass 2 parameters to javascript function from <INPUT onclick

Posted: Tue Jul 15, 2008 11:47 am
by Reviresco
Simpler solution? Try using radio buttons instead of checkboxes. That way no more than one can ever be checked, and there's no need for the js and php functions.

Just a thought, although perhaps your form requires checkboxes.