SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    validate checkbox with javascript

    I have six checkboxes.
    once u choose checkboxes and click submit, those checkboxes which are checked will be posted and inserted into database.
    How can I write the javascript?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,839
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    With no Javascript at all the checkboxes that are checked will be passed to the server side processing referenced by the action= attribute of the form tag. That server side processing can then test which checkboxes it has been passed to do the database update. Javascript can't assist in doing this because it has no way to access the database other than by calling a server side routine to do the access and since the form will do this anyway the only reason for using Javascript would be to avoid loading a new page. If this is what you want then you need to read up on Ajax.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem I have is nomatter the checkbox is checked or no, they are all passed to insert into the database.
    for example i have ten checkboxes, three of them are checked. so those are inserted into database with their value and others which are not checked is inserted as 0 into the database.
    then what should i do to avoid insert ten times? what should i use then?

  4. #4
    SitePoint Zealot sajjad's Avatar
    Join Date
    May 2005
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all it's not javascript question, but,
    if u r using php as ur server side scripting language then u can check the checkboxes like this,
    if(isset($_POST['checkboxname']))

    i think u got it

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I m totally confused. i posted my question in php forums someone told me i need to use javascript so I posted here.

    I added part of my code. there's a variable in the name of the checkbox. so where should i add the 'if' statement??

    PHP Code:
    for ($i=1; $i<$rows; $i++) {
         $key = mysql_result($q,$i,0);
         $val = mysql_result($q,$i,1);
         $sid = mysql_result($q,$i,2);
    ?>
    <input name="fields[multi_edit][<? echo $i?>][shipID]" type="hidden" value="<? echo $shipID;?>" />
    <tr bgcolor="#FFFFFF">
    <td>
    <input name="fields[multi_edit][<? echo $i?>][destID]" type="checkbox"  value="<? echo $key;?>"
    <? if($sid == $shipID || $sid == -1){
    echo 
    'checked="checked"';
    }
    if(
    $sid == -1){
    echo 
    ' disabled ';
    }
    ?>
    />

    <? if($sid == $shipID || $sid == -){
    $crn 1;
    ?>

    <input name="upd[<? echo $i?>]" type="hidden" value="<? echo $crn;?>" />

    <?}?></td>
    <td><? echo $val;?></td></tr>
    <? }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •