www.webdeveloper.com
Results 1 to 10 of 10

Thread: Need Some Help

  1. #1
    Join Date
    Aug 2012
    Location
    Malaysia
    Posts
    9

    Lightbulb Need Some Help

    Helo there.
    I just started with building a program using PHP language.
    I created the database & also a form.

    But my problem is, I have checkboxes & radio buttons.
    How do i store their input?
    Do i have to create

    checkbox1
    checkbox2
    checkbox3
    ...

    Thank you for reading.
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    You would want to make a table of possible things that can be checked off.

    This is very basic sql, you need to read more about sql and relational databases. It is very important. Hint: primary/foreign keys
    I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;

  3. #3
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    Oooh I know how to do this! I try to help people on this forum but most questions are too complicated for me. I'm only intermediate at PHP.

    Anyhooooooooo. Here goes. Okay, say you have a checkbox question that asks "What colors do you like?" And then you have a checkbox for blue, one for red, one for yellow, and one for green, right? Well, you're going to give all those checkboxes the same name and put a [] after it. Watch:

    PHP Code:
    <strong>What colors do you like?</strong>
    <
    br /><br />
    <
    input type='checkbox' name='color[]' value='red' /> Red
    <br /><input type='checkbox' name='color[]' value='blue' /> Blue
    <br /><input type='checkbox' name='color[]' value='yellow' /> Yellow
    <br /><input type='checkbox' name='color[]' value='green' /> Green 
    Now, lets turn our attention to how the form will be submitted. Do you recognize the following tag?

    PHP Code:
    <form action='somepage.php' method='get'
    That goes at the beginning of a form. See method='get'? That means the information entered in the form will be submitted through the URL. If you don't want to do this, use "post" in place of get, like this: method='post'

    Now turn your eyes to action='somepage.php'. Replace somepage.php with the page you want to process the form. Let's just call it formprocessor.php for now.

    Okay, when you submit the form, the information will go to formprocessor.php If you used "get" as your method the information will be put inside a variable called "$_GET", if you used "post", the info will be in "$_POST".

    To access something in $_GET or $_POST you use what you named the field. So, like, for our "color" field, if we submitted it for $_GET it's in $_GET['color'] and likewise if it was with post then $_POST['color'].

    Because we used the [] things, if somebody checks multiple boxes and submits the form, PHP is going to put those multiple answers into an array. Like if they picked blue, green, and red, $_GET['color'] is going to have an array like this in it:

    Array
    (
    [0] => red
    [1] => blue
    [2] => green
    )
    If you want to see this for yourself, go to formprocessor.php and do this:

    PHP Code:
    print '<pre>';
    print_r($_GET['color']);
    print 
    '</pre>'
    (If you used post then replace GET with POST.) Go to your page with the form, check multiple answers, and submit it.

    Erase the code I just showed you because you're not going to actually use that code to process your script. This is just a nice way of looking at what is in that array.

    Anyhoo, now, having multiple answers combined into an array is well and good, but how do we store it in the database? Well, it kind of depends on what you want. Do you want to store them in one field or multiple fields? Storing them in one field is easiest for me to explain so I'll show you that.

    You can use the function implode() to compress an array into a string. I don't know how to explain what a string is except you are going to hear that word a lot and it, is like, just a collection of characters like letters, number, spaces, etc. It's nothing fancy like an array. Anyway, do this with $_GET['color']:

    PHP Code:
    $colorCompressed implode(','$_GET['color']); 
    $colorCompressed was an arbitrary name I chose. $_GET['color'] contains the original array and I wanted somewhere convenient to stick the imploded one. The process is the same with $_POST.

    So the array, now turned into a string, looks like this:

    red,blue,green
    It's ready to stick in the database.

    Later on, if you get it back out of the database, and want to turn it back into an array, you can use explode() So, okay, let's say you got it out of the database and ended up sticking it in the varible $color; Well here is what you would do:

    $color = explode($color);

    This would take "red,blue,green" in color, turn it into an array just like it was before, and stick it back in $color.

    Now, radio buttons are different from checkboxes. You can't select multiple answers. At least, you shouldn't be able to. The only way HTML can tell that a set of radio buttons go together is if they all have the same name. So like if you ask the question "Which color is your favorite?" you name each radio button the same thing, "color" for instance.

    Since only one answer is selected, you don't have to worry about messing with arrays or anything. The answer is going to be submitted just anything else in the form, e.g. If you had <input type='text' name='firstName'> and somebody entered "Larry", that would create $_GET['firstName'] or $_POST['firstName'] with the value "Larry"

    So, you could just use $_GET['color'] or $_POST['color'] to access the value.

    Hope this helps. Let us know if there are any more problems.
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  4. #4
    Join Date
    Mar 2008
    Posts
    15
    Nice reply evenstar7139 I like the detail.

  5. #5
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    Thanks.

    I enjoy explaining things, if you haven't already figured that out ^_^
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  6. #6
    Join Date
    Aug 2012
    Location
    Malaysia
    Posts
    9
    Thank you for all the reply.
    I will give it a shot.

  7. #7
    Join Date
    Aug 2012
    Location
    Malaysia
    Posts
    9
    I get the idea but I still dont understand how to stor the date into the database.

  8. #8
    Join Date
    Mar 2009
    Posts
    452
    hmm... how actually you want to store your data. you may store those checkbox values as a,b,c using implode or in a json array or a simply serialize and store

    would love to see your latest code to understand and reply better.

  9. #9
    Join Date
    Aug 2012
    Location
    Malaysia
    Posts
    9
    I'll try to post the latest coding by the end of this week.

  10. #10
    Join Date
    Aug 2012
    Location
    Malaysia
    Posts
    9
    This is insert coding so far but I got stuck here and I have problem to proceed.
    PHP Code:
    <?php
    //this is using Malay language
    ?>
    <?php
    require_once('connect.php');
    ?>
    <script language="javascript">
    alert("Nama Pesakit <?php echo $_POST['idpesakit']; ?> sudah didaftarkan.");
    </script>
    <html>

    <?php
    //If form not submitted, show form.
    if(!isset ($_POST['submit'])){
    ?>
    <html>
    <body>
    <header>Tambah Pesakit</header>
    <title><h2>Tambah Pesakit</h2></Title>
    <body>
    <form method="post" action="add.php">

    Nama: <input type="text" name="nama" /><br/>
    K/P Dokumen Diri: <input type="text" mode="text" name="kpdokumendiri" /><br/>
    Tarikh: <input type="int" name="tarikh" /><br/>


    <p />

    <input type="submit" name="submit" value="Simpan" />
    </form>
    <?php
    //If form submitted, respond to user.
    } else {

    $nama=$_POST['nama'];
    $kpdokumendiri=$_POST['kpdokumendiri'];
    $tarikh=$_POST['tarikh'];

    //Check if user already exists

    $getUSer =mysql_query("SELECT(*) from bel_iza where butiran_pesakit='$nama'");
    $userExist mysql_fetch_type($getUSer);

    if (
    $userExist[0]>0) {
                
    $status "<font color=red>User already exist \"$nama\" .<br>Record is not save.</font>"
                echo 
    $status ;
            } else {
            
                
    $addUser   mysql_query("INSERT INTO user (nama,kpdokumendiri,tarikh)
                    VALUES ('
    $nama','$kpdokumendiri','$tarikh')");
                if (
    $addUser) {
                
    ?>
                <header>Tambah Pesakit</header>
                <title><h2> Pesakit Sudah Ditambah </h2></Title>
                <body>
                

                <?php        
                    $alert    
    " onload=\"window.alert('Rekod Simpan!');\"";
                    
                    
                    
    header('Location: display.php');
                }
                else
                {
                echo
    "Record not save...some problems";
                
                }
            }


      }

    ?>

    </body></html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles