SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with code displaying checkbox results in php

    Been trying for over a week to solve this! I want to display the results of a query that contain selected checkboxes and and a text field (which will be a wildcard search)I'm not sure where the $_POST & query for this goes. They are selected from a MySQL db via an HTML form.
    Any help/advice would be gratefully received as I'm in a blind panic now!
    HTML Form consists of -

    <form action="extype.php" method="post">
    <P>
    <font face="Verdana, Arial, Helvetica, sans-serif" size="2">
    <h3><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> </font> </h3>
    <h3>2. Experiment Information </h3>
    <font face="Verdana, Arial, Helvetica, sans-serif" size="2">
    <p>2.1 Experiment Type</P>
    <p>(Please note that multiple answers are possible.)</P>
    </font>
    <table width="89%" border="0">
    <tr>
    <td width="26%" height="23"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Building/Structure:
    <input type="checkbox" name="baexperimentype[babuilding]" value="1"/> </font></td>
    <td width="74%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Manufacture:
    <input type="checkbox" name="baexperimentype[bamanufacture]" id="bamanufacture" value="1"/></font></td>
    </tr>
    <tr>
    <td height="26"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Use-Wear (Micro-wear):
    <input type="checkbox" name="baexperimentype[bawear]" id="bawear" value="1"/></font></td>
    <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Activity:
    <input type="checkbox" name="baexperimentype[baactivity]" id="baactivity" value="1"/></font></td>
    </tr>
    <tr>
    <td height="27"></td>
    <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Other (Please Specify)
    <input name="baother21" type="text" id="baother21" value="" size="100" maxlength="100"/></font></td>
    </tr> </table>
    <BR>
    <BR>
    <INPUT name="submit" TYPE=SUBMIT id="submit" value="Submit"/>
    </FORM>


    extype.php is -
    <?php
    $con = mysql_connect("localhost","login","pwd");
    if (!$con)
    {
    die ('Could not connect: ' . mysql_error());
    }
    mysql_select_db("database", $con);

    if(isset($_POST['baexperimentype']) && !empty($_POST['baexperimentype'])){
    foreach($_POST['baexperimentype'] as $key=>$value){
    if($value==1) $baexperimentype[] = mysql_escape_string($key);
    }
    $baexperimentype = implode('=true OR ', $baexperimentype);
    $baexperimentype = $baexperimentype.'= true';
    }

    $query = "SELECT aaname1, abaffiliation, acemail, babuilding, bamanufacture, bawear, baactivity, baother21 FROM dexar WHERE $baexperimentype";
    echo $query;
    $results = mysql_query ($query, $con);

    while ($row = mysql_fetch_array($results, MYSQL_NUM)) {
    /*echo $baexperimentype['baexperimentype'];*/
    printf("<BR>Name: %s <br>
    Affiliation: %s <br>
    Email: %s <br><br>",
    $row[0], $row[1], $row[2]);
    }
    ?>

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Does any matching data exist in your database?

    Does your query builder actually work as you expect?

    You echo out $query onto the screen, what happens when you paste that into your database management tool? Does it return any results?

    Put a var_dump( $row ) inside your results loop and check what is being returned.

  3. #3
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply.
    The query returns the correct results. Echoing the query gives -

    SELECT aaname1, abaffiliation, acemail, babuilding, bamanufacture, bawear, baactivity, baother21 FROM dexar WHERE babuilding=true OR bamanufacture= true

    Which is the correct result when the checkboxes babuilding and bamanufacture are selected on the HTML form.

    And the result returned is -

    Name: test
    Affiliation: test
    Email: test

    What I've been trying to do is to make it a bit more clearer/stylish with some formatting and also if possible to print the name of the checkboxes that the user selected! Also, where the $_POST['baother21'] should be in the query. I know I'm asking a lot but any help is much appreciated! Thanks.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    So you have no problem with your query builder, your test data or your connection to the database.

    PHP Code:
    foreach( $row as $key=>$value ) {

    echo 
    "checkbox $key has the value $value " PHP_EOL ;


    Is that what you mean?

  5. #5
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thank you, that looks exactly what I've been trying to do! Only I've been going about it in a roundabout way...and getting nowhere.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You might simplify your example next time, many times when I see reams of html it makes me shy away from answering.

    You should always try and distill your problem down to the simplest form which still encapsulates the problem, youve posted html, sql and PHP when in fact seemingly you only wanted to know how to handle an array.

    I doubt my suggestion is the ultimate answer you need, but if you posted what a typical $row array contained, and what you want the finished html to look like, then someone else might be able to help you.

    Wrapping code in [ php ] [ /php ]( <- without the spaces ) tags helps too.

  7. #7
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, sorry about that, I know what you mean! It was all pasted in desperation!
    Also, if the user selects the wildcard search 'baother21' from the form where would the $baother21=$_POST['baother21']; go? I've added it as an 'else if'
    Thanks.

    Code PHP:
    if(isset($_POST['baexperimentype']) && !empty($_POST['baexperimentype'])){
    foreach($_POST['baexperimentype'] as $key=>$value){
    if($value==1) $baexperimentype[] = mysql_escape_string($key);
    }
    $baexperimentype = implode('=true OR ', $baexperimentype);
    $baexperimentype = $baexperimentype.'= true';
    }
    else
    if 
    $baother21=$_POST['baother21'];
       mysql_query("select fieldnames from 'table' where baother21 like '%$baother21%'  ");


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
  •