SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: how do I write a function that queries a database with onchange?

  1. #1
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how do I write a function that queries a database with onchange?

    I have no knowledge of javascript, and would like to write a function that queries my database using onchange. I have a dropdown which shows my mysql tables. when selected (and pushing the select button) it queries the database, and shows the contents of the table. How do I get rid of that submit button, and start using onchange. I tried searching around, but apparently this is a difficult topic to find. Anyone have any ideas?

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    679
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,

    i'm presuming you simply wish to send the tablename as a variable to another page. if you wish to include it within a sql statement, you only need the second parameter (tblName)

    Code:
    <form name='theform'>
    <select name='thesel' onchange='sendit(this.form, this.options[this.selectedIndex].value)'>
    <option value='someTableName'>someTableName</option>
    ....more options....
    </select>
    </form>
     
    function sendit(formObj, tblName)
    {
       formObj.action = 'somepage.php?table=' + tblName;
       formObj.submit();
    }
    Vinny
    moderator at:Webxpertz Forums

  3. #3
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    let me try to be more descriptive....I currently have a dropdown, which queries, and shows a list of my tables. Upon selection (submit button) appears everything in the table under the dropdown. everything's all in one page with some if statements. How can I use the onchange to get it to do the same thing, without needing the submit button? Check out the link to view the page I'm talking about. I didnt really understand your reply, so I can't tell if its what I'm asking for or not.

    http://dky.sytes.net/golesnew/cat/showtable.php

  4. #4
    SitePoint Guru bronze trophy blufive's Avatar
    Join Date
    Mar 2002
    Location
    Manchester, UK
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm no expert, but Vinny's answer looks to be exactly what you're asking for: using the onchange event on the <select> to call a function which constructs the action attribute and fires the form submit.

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you could just add this to the <select>:
    Code:
    onchange="this.form.submit();"
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  6. #6
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but what takes the place of the this, and what takes the place of the form and the submit.... I want it to be submitted to the current form.

  7. #7
    “He won’t leave me alone...” Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing takes their place. They are used literally to indicate the current form.
    Of course, that's just my opinion. I could be wrong.

  8. #8
    SitePoint Member fidel's Avatar
    Join Date
    Aug 2003
    Location
    harrisburg pa
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is a magic word
    </spam>

  9. #9
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    679
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Think of 'this' as a pointer. It points to whatever you are pointing to. So,

    <form name='a'>
    <select name='b' onchange=doit(this, this.form, this.selectedIndex)

    the 1st 'this' means the selection list object
    the 2nd 'this' means form 'a'
    the 3rd 'this' means the index of the selected option in select 'b'

    Vinny
    moderator at:Webxpertz Forums

  10. #10
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This makes perfect sense now, onchange points to the action in the form, I got it. and if I want, I can put it like so mypage.php?mode=change in the action, and then use a php if statement to run the query, like so.
    PHP Code:
    if('change' == $_GET['mode']) 
    {


    That's awesome!!! Thanks a lot everyone. One problem I have though, my dropdown disappears when selected. here's the page I'm working on. http://dky.sytes.net/golesnew/cat/pickitem.php and here's the code, in case anyone knows a little php, and wants to see if they can tell my where my dropdown went??
    PHP Code:
    <html><head><title>Pick an Item Page</title><LINK HREF="catalogwhite.css" REL="STYLESHEET"></head><body>
    <?
    require("connect.php" );
    database_connect();
    display_tables();

    if (!
    $_GET
    {
    echo 
    "<form action=\"$_SERVER['PHP_SELF']?mode=change\" method=\"post\" NAME=\"show_tables\">";
    echo 
    "<br />";
    echo 
    "<table align=\"center\">";
    echo 
    "<tr>";
    echo 
    "<td width=\"100%\" valign=\"top\" align=\"center\">";
    # must add this in the select below onChange=\"location.href="?mode=change&table=" . $table . ''\"
    echo "<select name=\"view_category\" onchange=\"this.form.submit();\">";
    echo 
    "<option value=\"ViewTheContentsofaTable\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    echo 
    "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;View a Category&nbsp;&nbsp;";
    echo 
    "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    echo 
    "</option>;";

    while(
    $rowmysql_fetch_array($result)) {
    echo 
    "\t\t\t\t<option>{$row[0]}</option>\n";
    }
    echo 
    "</select>";
    #echo "&nbsp;&nbsp;<input type=\"submit\" name=\"Select\" value=\"Select\" />";
    echo "</td></tr></table>";
    }

    #if ($_POST["Select"]) {
    #$table = $_POST["view_category"];

    if('change' == $_GET['mode']) { 
    $table $_POST["view_category"];
    # $table=$_GET['table'];

    if ($table == "ViewTheContentsofaTable" )
    {
    echo 
    "<br><br><br><br><font id='title'>Please Select a Category</font>"
    }
    else
    {
    $query "SELECT * FROM $table";
    $result mysql_query($query);
    if (!
    $result)
    {
    $mcrmessage mysql_error();
    echo 
    "$mcrmessage<br>";
    die();

    ECHO 
    "<TABLE BORDERCOLOR='#003300' BORDER='0' CELLSPACING='0' CELLPADDING='10'>";
    echo 
    "<tr><td colspan='2' STYLE='border-bottom-style: solid; border-bottom-width:1'><center>
    <font id='title'>
    $table</font></td></tr>";
    while (
    $row mysql_fetch_array($result))
    {
    extract($row);
    echo 
    "<tr onclick=\"java script:top.showitem.location.href('showitem.php?mode=view&
    table="
    .$table."&main_id=".$row["Product_Number"]."');\" STYLE=\"Cursor:Hand;\" 
    ONMOUSEOVER=\"this.style.backgroundColor='#E2EEE2';\" 
    ONMOUSEOUT=\"this.style.backgroundColor='#FFFFFF';\">
    <td STYLE='border-bottom-style: solid; border-bottom-width:1'>
    $Thumbnail</td>
    <td STYLE='border-bottom-style: solid; border-bottom-width:1'>
    $Item_Name</td></tr>\n";
    }
    echo 
    "</table>\n";
    }
    }
    database_disconnect();
    ?>
    </form></body></html>

  11. #11
    “He won’t leave me alone...” Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried your page on Mozilla 1.5 and IE6 and it seems to work just fine.
    Of course, that's just my opinion. I could be wrong.

  12. #12
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, it works fine, but like i said the dropdown disappears when you select something, any ideas why?

  13. #13
    “He won’t leave me alone...” Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, the dropdown doesn’t exactly “disappear.” Selecting something in the dropdown submits the form, which in turn takes you to the form-submission page. Since the dropdown isn’t on the form-submission page, it would give the effect of it “disappearing.”
    Of course, that's just my opinion. I could be wrong.

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
  •