Click to See Complete Forum and Search --> : [RESOLVED] Creating a checkbox that will fire an event
j_w_Bruce
11-21-2006, 09:48 AM
I am generating text boxes and check boxes as I move through a recordset.
echo "<td>".$i."</td><td><input name=UserName".$i." type=text onKeyPress=return noenter() size=20
value=".$row['UserName']."></td>";
echo "<td><input name=Password".$i." type=text onKeyPress=return noenter() size=20
value=".$row['Password']."></td>";
echo "<td><input name=InputDate".$i." type=text onKeyPress=return noenter() size=20
value=".$row['InputDate']."></td>";
echo "<td><Input name=deleteBox".$i." type=checkbox value=Delete>Delete</td>";
echo "<td><Input name=editBox".$i." type=checkbox value=Edit>Edit</td>";
This should be super simple, but I don't get it yet. I need to make the edit and delete checkboxes fire some code (preferably "do you want to edit / delete...")
I've not had any luck to this point with it.
Thanks
Jay
U should try posting on ClientSide Technologies (http://phpbuilder.com/board/forumdisplay.php?f=27), this is javascript problem; any way u should check for checkbox (checked) or validate input text field on "onsubmit" event, or if the user click on a checkbox, u should have onclick event (onclick="return confirm('Are u sure?');"), any way your question is so general (u should be more specific on what u want) and the best section of this forum for your question is the one I told u above ...
MarkR
11-21-2006, 10:44 AM
Make sure you quote all attribute values in HTML correctly.
A HTML checkbox should fire onclick when it is clicked (or activated by some other method, e.g. keyboard or clicked on its label), and will fire onchange when it loses focus following a change of its value.
Mark
j_w_Bruce
11-21-2006, 07:00 PM
I've looked through the Client Side Technologies, but didn't find anything that has helped me so far, and I'm getting pretty stumped on this one. Here's what I'm doing so far:
while ($row = mysql_fetch_assoc($AppRS))
{
echo "<td>".$i."</td><td><input name=UserName".$i." type=text onKeyPress=return noenter() size=20
value=".$row['UserName']."></td>";
echo "<td><input name=Password".$i." type=text onKeyPress=return noenter() size=20
value=".$row['Password']."></td>";
echo "<td><input name=InputDate".$i." type=text onKeyPress=return noenter() size=20
value=".$row['InputDate']."></td>";
echo "<td><Input name=deleteBox".$i." type=checkbox onclick=return DelFunction(UserName$i,Password$i) value=Delete>Delete</td>";
echo "<td><Input name=editBox".$i." type=checkbox value=Edit>Edit</td>";
echo "</tr>\n";
$i=$i+1;
}
I'm trying to call this function:
function DelFunction($User,$Pass)
{
mysql_select_db($database_app, $app);
$query_App = "DELETE * FROM users where UserName=".$User." and Password=".$Pass." ORDER BY UserName";
$AppRS = mysql_query($query_App, $app) or die(mysql_error());
if ($AppRS=True)
{
$Reloadable ="DisplayUsers.php";
header(sprintf("Location: %s", $insertGoTo));
}
else
{
$Reloadable ="DisplayUsers.php";
header(sprintf("Location: %s", $insertGoTo));
}
}
I can't see that it is even trying to do anything when I click one of my delete checkboxes...
Thanks
Rodney H.
11-22-2006, 12:05 AM
Have another look at MarkR's advice...
bretticus
11-22-2006, 12:52 AM
You cannot call a server-side function from a client-side call. At least not directly. You need to use either AJAX (http://http://en.wikipedia.org/wiki/AJAX) or just submit the form and depending on POST (or GET) values, determine the server-side code to call (which you need to do.) In fact, what you want to do is pretty simple. On your checkbox onclick events just submit the form. Then, in the form handler, look for the checkbox names under the $_POST array using the isset() (http://us2.php.net/manual/en/function.isset.php) function. Of course you'll need to loop through the entire $_POST array to match the numbers represented by $i (I'll leave that to another thread.)
bradgrafelman
11-22-2006, 01:26 AM
Moved to ClientSide Technologies forum, as your current problem and the likely solution will both deal with client-side scripting.
As Rodney suggested, Mark has already told you part of your problem: Make sure you quote all attribute values in HTML correctly. In your HTML INPUT entities, you don't quote any of the values.
Though this will fix your current issue, it will not achieve the desired result. As is mentioned above, you can't directly call a function written in a server-side script from the client.
j_w_Bruce
11-22-2006, 09:19 AM
I wish I was understanding better...
I'm using echo " " to make these lines, so I can't quote the line like what I've seen in some other examples without getting parse errors.
Do I need to do something like this?
onclick=<script text/javascript>some scripty code here...</script>
bretticus
11-22-2006, 01:01 PM
I wish I was understanding better...
I'm using echo " " to make these lines, so I can't quote the line like what I've seen in some other examples without getting parse errors.
Do I need to do something like this?
onclick=<script text/javascript>some scripty code here...</script>
ummmm...you might wanna start learning the fundamentals of HTML and clientside scripting before jumping into the serverside stuff. Just a thought.
bradgrafelman
11-22-2006, 01:18 PM
I'm using echo " " to make these lines, so I can't quote the line like what I've seen in some other examples without getting parse errors. Why not? echo "Here are some \"quotes\"!"; // Here are some "quotes"!
Besides, HTML also accepts single quotes as string delimiters.
j_w_Bruce
11-23-2006, 12:48 PM
I've tried the single quotes and the double quotes:
echo "<td><Input name=\"deleteBox".$i."\" onclick=\"return CheckButtons($i)\" type=\"checkbox\" value=\"Delete\">Delete</td>";
My code on the finished page is looking like this:
<td>1</td><td><input name=UserName1 type=text onKeyPress=return noenter() size=20
value=12345></td><td><input name=Password1 type=text onKeyPress=return noenter() size=20
value=2345></td><td><input name=InputDate1 type=text onKeyPress=return noenter() size=20
<td><Input name="deleteBox1" onclick="return CheckButtons(1)" type="checkbox" value="Delete">Delete</td>
But when I click the button, this is what I'm getting:
error 0, object required
I've tried calling a php function, but the general consensus was that I needed to be calling java script so here is what I have lower in the page...
<script type='text/javascript' language='JavaScript'>
function CheckButtons()
{
alert "Warning, you've clicked a box";
}
</script>
Thanks for all the help so far.
bradgrafelman
11-23-2006, 09:09 PM
First of all, you need to surround your HTML values in quotes, such as the onKeyPress=return noenter() part SHOULD be onKeyPress="return noenter()".
And yes, PHP is a server-side scripting language, so you can't call PHP functions from the client (the rendered page). In JavaScript, alert isn't a language construct like echo in PHP - it's a function. So, you need to use it like so: alert("Warning, you've clicked a box");
Also, just for some HTML cleanup, the "language" attribute of the SCRIPT tag is not only superfluous, but not even a valid attribute by W3C standards; I would remove it and stick with type='text/javascript'.
j_w_Bruce
11-23-2006, 09:43 PM
Thanks for the replies. I finally started getting the code down to the Script.
I've gotten the check box fixed like so:
echo "<td><Input name=\"deleteBox".$i."\" onclick=\"return CheckButtons($i)\" type=\"checkbox\" value=\"Delete\">Delete</td>";
Then I have my script doing this:
<script type='text/javascript'>
<!--
function CheckButtons(SelectedItems)
{
var Selection=SelectedItems;
var YesNo=confirm ("You are working with Item Number " + Selection + " Continue?");
if (YesNo)
{
var UserBox=document.frmForm.UserName+Selection.value;
var PassBox=document.frmForm.Password+Selection.value;
alert (UserBox + " " + PassBox);
}
}
-->
</script>
I'm getting my Alert that says "you are working with 10". That is working.
But I'm having a problem with this line now.
var UserBox=document.frmForm.UserName+Selection.value;
var PassBox=document.frmForm.Password+Selection.value;
alert (UserBox + " " + PassBox);
My second Alert is giving me this "NaN Nan"
Each answer seem to lead to more questions... But I do appreciate the help.
Thanks
bradgrafelman
11-23-2006, 10:17 PM
Erm, the "Selection" variable contains a number, not a reference to an HTML entity. So... why are you trying to do "Selection.value" ?
j_w_Bruce
11-23-2006, 10:36 PM
Well, I've numbered my boxes as I created them ie. UserName1, UserName2, UserName3..., UserName34. Selection tells me what row number was clicked on. So now I'm trying to put it back together to pull the text that was in UserName(x) and Password(x)
Maybe there is a way better way to do it.
Thanks
bradgrafelman
11-23-2006, 10:54 PM
Ahh, I see. So technically, you want to use variable variable names. Sort of like doing ${'Username' . $i} in PHP. In that case, I'd say do something like this:
eval('var UserBox=document.frmForm.UserName'+Selection+'.value');
eval('var PassBox=document.frmForm.Password'+Selection+'.value');
j_w_Bruce
11-24-2006, 10:43 AM
Excellent, exactly what I was looking for. Thanks!!
bradgrafelman
11-24-2006, 03:04 PM
No problem. Don't forget to mark this thread resolved.
PHP Builder
Copyright Internet.com Inc. All Rights Reserved.