Results 1 to 3 of 3

Thread: using onChange with my PHP script

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Question using onChange with my PHP script

    Im somewhat stuck here

    Code:
    <select name="companyfilter">
    <option value="">Filter by company</option>
    <?php
    /* SHOW COMPANY NAMES */
    $query="SELECT callid, company FROM ring WHERE userid=? ORDER BY callid ASC LIMIT ".($nCurrentPage-1)*$nItemsPerPage.", $nItemsPerPage";
    $stmt = $mysqli->prepare($query);
    $stmt->bind_param('i', $uid); 
    $stmt->execute(); 
    $stmt->bind_result($callid1, $company1);
    $stmt->store_result();
    if ($stmt->num_rows>0) {
    while ($stmt->fetch()) {
    ?>
    <option value="<?php echo $company1;?>"><?php echo $company1;?></option>
    <?php
    }
    }
    ?>
    </select>
    <input name="show" value="Show" type="submit"><br />
    </form>
    So the above code simply fills my dropdown box with company names from a database.

    Code:
    /* IF COMPANY FILTER USED THEN FILTER */
    if (isset ($_POST['show'])) {
    $company2filter = $_POST['companyfilter'];
    }
    The above just gets whatever has been chosen and uses it to filter results further in my page.

    What I'd like to do is get rid of the submit button, and instead just by using onChange post the correct variable as required.

    Can you help with this? I'm confused how to go about it.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: using onChange with my PHP script

    This isn't really a PHP question. I've moved the thread.

    There are two methods, both using JavaScript :-

    — Upon the change event, post the containing form by calling its 'submit' method.

    — Upon the change event, use an XMLHttpRequest object to post the value or the entire form's values to the server asynchronously.

    The first is quite simple:
    Code:
    myelement.onchange = function() {
      document.forms['myform'].submit();
    };
    For the second, look up 'Ajax'.


    From what you've described, I think the first method is what you want, unless you also want to perform the subsequent filtering on the client side. If that is the case, then the callback may be unnecessary.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: using onChange with my PHP script

    Thanks for that - you're right, the first method is more what I'm looking for.

    I've done this:

    Code:
    <script type="text/javascript">
    
    document.companyfilter.onchange = function() {
      document.forms['companyfilter'].submit();
    };
    </script>
    And the page refreshes/submits when I change the select menu - but it seems nothing is being 'posted'. I've tested it using the input button to submit and the php code is fine, but the javascript does not seem to post the variable from the form for the php to use.

    Any ideas how to fix this?

Posting Permissions

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



Click Here to Expand Forum to Full Width