Pass 2 parameters to javascript function from <INPUT onclick

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
graytest
Forum Newbie
Posts: 2
Joined: Mon Jul 07, 2008 6:56 am

Pass 2 parameters to javascript function from <INPUT onclick

Post 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
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

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

Post 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.
Post Reply