SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checkbox MySQL Search

    Hello,

    I'm wondering how to perform a mySQL search with checkbox values. Basically, the user checks what he/she wants to search, and it will return the results.

    However I've come across a problem. The searching statement is this:
    Refer: $cpm = $_GET['cpm'] << Retrieves value from checkbox, if checked it has a value of "1"

    $query = "SELECT * FROM name WHERE cpm=$cpm";

    If the checkbox is not selected, there will be an incorrect mySQL statement therefore returning an error. How can I fix this, where the statement changes if the checkbox is not selected?

    Sorry I'm a PHP noob, Help is appreciated.

    Thanks
    Your DHTML, AJAX and CSS Resource - http://dhtmlsite.com

  2. #2
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your sql statement, generate it with an if/else statement based upon the value of the checkbox:

    PHP Code:
    if(isset($_GET['cpm']))
    {
    $query "SELECT * FROM name WHERE cpm=$cpm";
    }
    else
    {
    $query "SELECT * FROM name";

    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  3. #3
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes i've tried the isset function, but for some reason, it thinks it has a value (undefined) whether the checkbox is checked or unchecked.

    I put in the script

    if(isset($_GET['cpm']))
    {echo "CPM has value";}
    And whether the checkbox is checked or not, it still echo's "CPM has value".

    If this helps, this is the form field: <input id="cpm" type="checkbox" value="1" />
    Your DHTML, AJAX and CSS Resource - http://dhtmlsite.com

  4. #4
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok - it would be worthwhile echoing out the value of that variable to see what it is holding...ie

    PHP Code:
    echo $_GET['cpm']; 
    I think that you also need to add a name value to your input (ie checkbox)
    also if you use
    PHP Code:
    <input id="cpm" type="checkbox" value="1" name=\"cpm"\ /> 
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  5. #5
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried echoing the output, it writes "undefined".

    As for the name, I've tried it with and without the name, makes no difference.
    Your DHTML, AJAX and CSS Resource - http://dhtmlsite.com

  6. #6
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you confirm the "method" of your form - ie GET or POST?
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  7. #7
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The form is sent via GET. I've also tried POST.

    Fact is, when the checkbox is checked the mySQL runs fine, therefore I know it's working properly (when it's checked). But I just need a way to change the statement for when its unchecked.

    The script is being used here if you want to see: http://www.dhtmlsite.com/test

    Don't worry about the AJAX part. It's working fine as it should. If you want to bypass the AJAX-y stuff just hit enter and it will act as a normal form.
    Your DHTML, AJAX and CSS Resource - http://dhtmlsite.com

  8. #8
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok - can you post some more of the code for that page?
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  9. #9
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, You can View Source on that page, and this is greeting.php

    Yes, please excuse the junk.. I'm just slowly evolving from a very basic AJAX script i made.

    PHP Code:
    <?php
        $the_name 
    htmlspecialchars($_GET['cpm']);
        if(isset(
    $_GET['cpm'])) 
        {echo 
    "CPM has value";}
        
        
    $surname htmlspecialchars($_GET['greeting-surname']);
        
    $username "user";
        
    $password "pass";
        
    $database "database";
        
        
    mysql_connect(localhost,$username,$password);
        
        @
    mysql_select_db($database) or die( "Unable to select database");
        
        
        
    $query "SELECT * FROM name WHERE cpm=$the_name";
        
        
    $result mysql_query($query);
         
        
    $num mysql_num_rows($result);
        
        
    mysql_close();
         
        echo 
    "<b><center>Database Output</center></b><br><br>";
         
        if (
    $num==0) {
        echo 
    "No Matches!";
        } else {
         
    $i=0;
        while (
    $i $num) {

        
    $name=mysql_result($result,$i,"name");


        echo 
    "$name";
        
        
    $i++;
        
        }
        }
        
         echo 
    "<p>Season's Greetings, $the_name $surname !</p>";
    ?>
    Your DHTML, AJAX and CSS Resource - http://dhtmlsite.com

  10. #10
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try a print_r($_GET);

    to see what output you get and post back.
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  11. #11
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Done, http://www.dhtmlsite.com/test/

    Checkbox Checked: Array ( [cpm] => 1 [greeting-surname] => [_] => )
    Unchecked: Array ( [cpm] => undefined [greeting-surname] => [_] => )
    Your DHTML, AJAX and CSS Resource - http://dhtmlsite.com

  12. #12
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh I've found a breakthrough.

    I've added names to the fields. Now submitting them, it can detect the values properly.

    However that means I now need to get this line fixed:
    PHP Code:
    var pars 'cpm='+escape($F('cpm'))+"&greeting-surname="+escape($F('greeting-surname')); 
    since this was the line that was passing these parameters, including CPM which had no value, leaving it to be "undefined".

    Any ideas how to sort this out?

    I'm thinking maybe it can detect if its sent with no value, ("") but i'm not sure how to go about this.

    UPDATE: it is the $F('cpm') that is causing the undefined being sent. ($F() is a function that grabs the value from the field cpm). Any ideas how to stop it from sending "undefined"?

    Thanks
    Your DHTML, AJAX and CSS Resource - http://dhtmlsite.com

  13. #13
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That looks like some AJAX help is needed - not my area unfortunately
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  14. #14
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Solved!

    I tried using a if/else statement in the javascript, and it works!

    PHP Code:
        {
            if (
    $F('cpm') == undefined 
            { 
              var 
    url 'greeting.php';
            var 
    pars "greeting-surname="+escape($F('greeting-surname')); 
            var 
    target 'greeting';
            var 
    myAjax = new Ajax.Updater(targeturl, {    method'get',    parametersparsonLoadinginform_loading});
            }
        else
            {
            var 
    url 'greeting.php';
            var 
    pars 'cpm='+$F('cpm')+"&greeting-surname="+escape($F('greeting-surname'));
            var 
    target 'greeting';
            var 
    myAjax = new Ajax.Updater(targeturl, {    method'get',    parametersparsonLoadinginform_loading});
            }
        } 
    Thanks for the help you have given.
    Your DHTML, AJAX and CSS Resource - http://dhtmlsite.com


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
  •