SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can an onChange event (of a dropdown) call php?

    How can an onChange event (of a dropdown) call php?

    I want to do an action according to the user selection on my dropdown list (the <select> tag). I don't know how can I get the selected item id or name? And how can I set the onChange event of the dropdown?

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the onChange event is happening client side in javascript. your PHP script lives on the server. You can code up javascript to make a call to the server and fetch information, and this technique is usually referred to by the acronym AJAX.

    If you can give more information about what you are asking for, this can probably be moved to javascript forum if more appropriate.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used to have similar "probleme" with combining JS and PHP, and succeded to "build" a select menu for users to choice among different languages (without cookies), something like:
    PHP Code:
    <select onChange="window.location='<? print ($this_page); ?>.php?language='+this.value">
    where $this_page refers to the name of the loaded file "$this_page.php"
    I'm not sure if Betty_s's probleme is similar to that, it's just an example...

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, the html calls the JavaScript and the JavaScript calls the php?
    Sound interesting, can u give an example code?

    Thanks!

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to get or set html tags (input types) by php code?

    I'm adding a function a php file. It supposed to display html and php code.
    I discovered that there is no way to get or set html tags by php code within the same page or function.

    The only way I seen is by $_REQUEST but it's not suitable for me.

    I'm trying to rebuild a table by the selection on the dropdown. But how can get the selected item id or name? And how can I set the onChange event of the dropdown?

    In ASP.NET the input type run at the server side so there can be interaction between asp and html tags. Is there anything like in php?

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Betty_S View Post
    In ASP.NET the input type run at the server side so there can be interaction between asp and html tags. Is there anything like in php?
    ASP.NET has an abstraction, which makes you think that that's what happening, even though it's not. Such abstractions are commonly called frameworks. Try PRADO, which is a PHP framework, that uses the same type of abstraction as ASP.NET.

  7. #7
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a short example of a drop-down list (select) that is combining html, JS and PHP:
    PHP Code:
    <html>
    <head>
    </head>
    <table border="0" cellpadding="0" cellspacing="0" width=100%>
    <td width = "10%" valign="center" align="center"><p>Product</p></td>
    <td width = "10%" valign="top" align="center">
    <?php 
    $item 
    $_GET["product"];
    if (
    $item == "")
    {
        
    $item 1;
    }
    ?> 
    <select onChange="window.location='ban.php?product='+this.value" > 
    <?php 
    $product 
    = array(
        
    "",
        
    "PC",
        
    "Scanner",
        
    "Printer",
        
    "Monitor");
    for (
    $i 1$i <= 4$i++)
    {
    ?>
    <option value="<?=$i?><? if ($item == $i) { print "SELECTED";}?><?=$product[$i]?></option>
    <?
    }
    ?>
    </select>
    </td>
    <td width= "50" valign="middle"><img src = "<?=$item?>.gif" width="32" height="20">
    </td>
    </tr>
    </table>
    <hr>
    </html>
    You can create some images to display and name them 1.gif, 2.gif, etc.... or whatever you want

  8. #8
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it was all very helpful!

  9. #9
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS doesn't work with my PHP file

    I wrote a simple js function:
    echo "<script type=\"text/javascript\">";
    echo "function alertBtn()";
    echo"{alert(document.getElementById(\"btn\").id)}";
    echo "</script>";

    It refuses to fire, did I miss some important point or should I just recheck my code?

    Additionally, is there a special php command for conetcting the php file to a js file or should I just add "<script src=></script>"?

  10. #10
    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)
    How are you calling alertBtn() ?

    You will be connecting the html which PHP outputs to the JS file. Get it working without PHP first then convert bits to PHP slowly.

    You are using only double quotes, I'd advise on settling on a policy of single / double quotes as soon as you can - it can make your code more readable, and help stop some errors too.

    (outputting a string from PHP, to a string in JS inside a confirm box etc, can get real tricky....)

  11. #11
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I too Face Same kinda prob..
    When i type a value in textbox it should check
    whether there is a table with same name exists...
    if exists it will return false else new table with that name is created
    can anyone help me


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
  •