#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    56
    Rep Power
    15
    I'm having a problem determining how to pass a value from a form with a checkbox on a html page to a mysql database. I've preloaded some data and can populate a box if the data is already present in the db. But I would like to be able to check the box or uncheck the box and pass that value to the db (being able to change the value depending on whether it's checked or not). I'm a newbie to php so please bare with me.

    <pre>
    <html>

    <body>

    <?php

    $db = mysql_connect("intserver");

    mysql_select_db("mydb",$db);

    $count = mysql_query("SELECT COUNT(*) FROM members")
    or exit();

    if ($row = mysql_fetch_array ($count))
    echo "<P>The list currently has " . $row[0] . " members.<BR>&nbsp;<br>";

    if ($submit) {

    // here if no member_id then adding else we're editing

    if ($member_id) {

    $sql = "UPDATE members SET first_name='$first_name',last_name='$last_name',address='$address',company='$company',faxlist='$faxl ist',city='$city' WHERE member_id=$member_id";

    } else {

    $sql = "INSERT INTO members (first_name,last_name,address,company,faxlist,city) VALUES ('$first_name','$last_name','$address','$company','$faxlist','$city')";

    }

    // run SQL against the DB

    $result = mysql_query($sql);

    echo "Record updated/edited!<p>";

    } elseif ($delete) {

    // delete a record

    $sql = "DELETE FROM members WHERE member_id=$member_id";

    $result = mysql_query($sql);

    echo "$sql Record deleted!<p>";

    } else {

    // this part happens if we don't press submit

    if (!$member_id) {

    // print the list if there is not editing

    $result = mysql_query("SELECT * FROM members ORDER BY last_name",$db);

    while ($myrow = mysql_fetch_array($result)) {

    printf("<a href="%s?member_id=%s">%s %s</a> n", $PHP_SELF, $myrow["member_id"], $myrow["first_name"], $myrow["last_name"]);

    printf("<a href="%s?member_id=%s&delete=yes">(DELETE)</a><br>", $PHP_SELF, $myrow["member_id"]);

    }

    }

    ?>

    <P>

    <a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>

    <P>


    <form method="post" action="<?php echo $PHP_SELF?>">

    <?php

    if ($member_id) {

    // editing so select a record

    $sql = "SELECT * FROM members WHERE member_id=$member_id";

    $result = mysql_query($sql);

    $myrow = mysql_fetch_array($result);

    $member_id = $myrow["member_id"];

    $first_name = $myrow["first_name"];

    $last_name = $myrow["last_name"];

    $address = $myrow["address"];

    $city = $myrow["city"];

    $company = $myrow["company"];

    $faxlist = $myrow["faxlist"];

    // print the member_id for editing

    ?>

    <input type=hidden name="member_id" value="<?php echo $member_id ?>">

    <?php

    }

    ?>

    First:<input type="Text" name="first_name" value="<?php echo $first_name ?>"><br>

    Last:<input type="Text" name="last_name" value="<?php echo $last_name ?>"><br>

    Address:<input type="Text" name="address" value="<?php echo $address ?>"><br>

    City:<input type="Text" name="city" value="<?php echo $city ?>"><br>

    company:<input type="Text" name="company" value="<?php echo $company ?>"><br>

    Faxlist:<input type="checkbox" name="faxlist[]" value=<?php echo $faxlist ?> <?php if ($faxlist="YES") { echo "CHECKED"; } ?>>

    <input type="Submit" name="submit" value="Enter information">

    </form>

    <?php

    }

    ?>

    </body>

    </html>

    </pre>

    Thank you

  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    <<
    Faxlist:<input type="checkbox" name="faxlist[]" value=<?php echo $faxlist ?> <?php if ($faxlist="YES") { echo "CHECKED"; } ?>>
    >>


    Actually i'm not able understand your script..It is shapeless...But i am giving a hint how to pass the check box value to your script..

    In the above line you have the given name faxlist[] for variable..That means your variable is an array...Here you don't have to create an array variable it could be simply the follwoing..

    [b]
    Faxlist:<input type="checkbox" name="faxlist" value="YES" <?php if ($faxlist="YES") { echo "CHECKED"; } ?>>
    [b]

    When you check the value of your check box in your script..you can simply say..

    if(isset($faxlist)){
    //user has checked your check box..
    //do something here


    }else{
    //check box is unchecked..

    }

    yea,,That's it!!

    I would suggest you to write the code very clearly..try to see some script of others and follow a standard for writing your script(this is very important becoz other programmers should know what you are doing)..




    ------------------
    SR -
    webshiju.com
    www.jobxyz.com-IT Career Portal
    ezipindia.com--WebStudio


    "The fear of the LORD is the beginning of knowledge..."
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    56
    Rep Power
    15
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Shiju Rajan:
    [b]
    <<
    Faxlist:<input type="checkbox" name="faxlist[]" value=<?php echo $faxlist ?> <?php if ($faxlist="YES") { echo "CHECKED"; } ?>>
    >>


    Actually i'm not able understand your script..It is shapeless...But i am giving a hint how to pass the check box value to your script..

    In the above line you have the given name faxlist[] for variable..That means your variable is an array...Here you don't have to create an array variable it could be simply the follwoing..

    [b]
    Faxlist:<input type="checkbox" name="faxlist" value="YES" <?php if ($faxlist="YES") { echo "CHECKED"; } ?>>


    When you check the value of your check box in your script..you can simply say..

    if(isset($faxlist)){
    //user has checked your check box..
    //do something here


    }else{
    //check box is unchecked..

    }

    yea,,That's it!!

    I would suggest you to write the code very clearly..try to see some script of others and follow a standard for writing your script(this is very important becoz other programmers should know what you are doing)..


    [/quote]

    Thank you for the quick reply. I understand the format is poor. I started with a sample for hotwired and do need to clean it up. I do have a question. You code:

    Faxlist:<input type="checkbox" name="faxlist" value="YES" <?php if ($faxlist="YES") { echo "CHECKED"; } ?>>

    somewhat confuses me. If you put a value of "YES" in the box always appears checked. I'm trying to get it to look at the db and if the value says YES, then place a checkbox in the box otherwise leave it blank. Then if I want to change the value, do so by checking or unchecking the box.

    Thanks

  6. #4
  7. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    <<
    Faxlist:<input type="checkbox" name="faxlist" value="YES" <?php if ($faxlist="YES") { echo "CHECKED"; } ?>>

    >>


    Leave the value="YES" for a moment..

    take the the following condition first...

    <?php if ($faxlist="YES") { echo "CHECKED"; } ?>

    if the checkbox value is "YES" then you make the checkbox checked ,otherwise it is not checked...Right!!

    yea,,

    now value="YES" portion...

    if your checkbox is checked then only the "YES" value will be passed to the script..other wise it won't pass any value...

    that means,,

    let us say,i am clicking your check box then
    the program will pass check box value "Yes" to your script.if i'm not selecting that check box then it won't pass any value to your script...

    i hope you got the point!!!

    that is why i'm checking the check box value like following..

    if(isset($faxlist)){

    //this means that the user has selected the check box...

    //the check box value here is "YES".


    }else{
    //no he has not selected the check box so the value is "NO"

    }





    ------------------
    SR -
    webshiju.com
    www.jobxyz.com-IT Career Portal
    ezipindia.com--WebStudio


    "The fear of the LORD is the beginning of knowledge..."
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    56
    Rep Power
    15
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Shiju Rajan:

    <<
    Faxlist:<input type="checkbox" name="faxlist" value="YES" <?php if ($faxlist="YES") { echo "CHECKED"; } ?>>

    >>


    Leave the value="YES" for a moment..

    take the the following condition first...

    <?php if ($faxlist="YES") { echo "CHECKED"; } ?>

    if the checkbox value is "YES" then you make the checkbox checked ,otherwise it is not checked...Right!!

    yea,,

    now value="YES" portion...

    if your checkbox is checked then only the "YES" value will be passed to the script..other wise it won't pass any value...

    that means,,

    let us say,i am clicking your check box then
    the program will pass check box value "Yes" to your script.if i'm not selecting that check box then it won't pass any value to your script...

    i hope you got the point!!!

    that is why i'm checking the check box value like following..

    if(isset($faxlist)){

    //this means that the user has selected the check box...

    //the check box value here is "YES".


    }else{
    //no he has not selected the check box so the value is "NO"

    }



    [/quote]


    Thank you very much for your help. I've finally got the faxlist to work like this:

    <pre>
    <input type="checkbox" name="faxlist" value=<?php echo $faxlist ?> <?php if ($faxlist="YES") { echo "CHECKED"; } ?>>
    </pre>

    The only thing that's still puzzling is that if I put an additional
    <pre>
    <input type="checkbox" name="press" value=<?php echo $press?> <?php if ($press="YES") { echo "CHECKED"; } ?>></pre>

    I doesn't work on the second checkbox.
    I apologize for being a bother to you.

    Thank you,

    Chad
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    56
    Rep Power
    15
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Shiju Rajan:

    <<
    Faxlist:<input type="checkbox" name="faxlist" value="YES" <?php if ($faxlist="YES") { echo "CHECKED"; } ?>>

    >>


    Leave the value="YES" for a moment..

    take the the following condition first...

    <?php if ($faxlist="YES") { echo "CHECKED"; } ?>

    if the checkbox value is "YES" then you make the checkbox checked ,otherwise it is not checked...Right!!

    yea,,

    now value="YES" portion...

    if your checkbox is checked then only the "YES" value will be passed to the script..other wise it won't pass any value...

    that means,,

    let us say,i am clicking your check box then
    the program will pass check box value "Yes" to your script.if i'm not selecting that check box then it won't pass any value to your script...

    i hope you got the point!!!

    that is why i'm checking the check box value like following..

    if(isset($faxlist)){

    //this means that the user has selected the check box...

    //the check box value here is "YES".


    }else{
    //no he has not selected the check box so the value is "NO"

    }



    [/quote]

    Thanks again for your help. I've finally solved the puzzle and the script is functioning properly.

    Thanks again.
    chad
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    if ($faxlist="YES") => if ($faxlist=="YES")

    could clear some confusion! - easy mistake to make though!

    ------------------
    Simon Wheeler
    FirePages -DHTML/PHP/MySQL

Similar Threads

  1. mysql, checkbox and search?
    By horneusz in forum PHP Development
    Replies: 0
    Last Post: February 13th, 2004, 04:32 PM
  2. Inserting checkbox values from an array into MySQL
    By outlyr242 in forum PHP Development
    Replies: 1
    Last Post: February 9th, 2004, 05:28 PM
  3. Replies: 1
    Last Post: February 6th, 2004, 06:53 AM
  4. Replies: 4
    Last Post: January 14th, 2004, 06:22 AM
  5. Adding numbers from mysql
    By vastslug in forum PHP Development
    Replies: 1
    Last Post: January 8th, 2004, 08:13 AM

IMN logo majestic logo threadwatch logo seochat tools logo