refreshing the same php page

Discussion in 'General Chat' started by beginner_101, Dec 19, 2012.

to remove all ads.
  1. beginner_101

    beginner_101 New Member

    Messages:
    4
    I am still learning and programming in php and I need a help with filtering data. I have created a php page that displays data from a mysql table. I have to create filters now and I made a dropdown box in that same page. However I dont know how to make the page refresh once a filter is selected, with the appropriate data from mysql table being queried.

    Is it possible to make the php page refresh with the intended data being displayed on it? If possible can anyone tell me how?
     
  2. chrishirst

    chrishirst Well-Known Member Staff Member

    Messages:
    2,639
    You can use Javascript.

    Code:
    function formSubmit()
    {
    document.getElementById("FormID").submit();
    }
    // Change FormID to suit the id attribute value of the form
    
    Then put "formSubmit()" in the onChange event of the select element.
     
  3. beginner_101

    beginner_101 New Member

    Messages:
    4
    Thanks for your reply.

    I was actually trying to do this without a submit button as you said, in such a way that selecting the filter option from the dropdown box will automatically refresh the page with the queries I want.

    I found out from forums yesterday that the parameter that you mentioned will help: "onchange="document.getElementById("env").submit();">", when added with the <select> will do the trick.

    But when I select an option from the drop down box, the page stays still. This is how the form and select statements look like, and this dropdown is in a table cell:

    <td>
    <form method="post" id="env" action="<?php echo $_SERVER[PHP_SELF];?>" >
    <select name="name" onchange="document.getElementById("env").submit();">
    <option value="all">All</option>
    <option value="Windows">Windows</option>
    <option value="Win 2008">Win 2008</option>
    <option value="Win 7">Win 7</option>
    <option value="Win Vista">Win Vista</option>
    <option value ="Linux">Linux</option>
    <option value = "Win 7/Linux">Win 7/Linux</option>
    </select>
    </form>
    </td>


    And for trial purposes, I want to see if the option that I selected is being echoed as:

    <?php
    if(isset($_POST["name"]))
    {
    echo $_POST["name"];
    }
    ?>

    But the page remains still once I select an option. Can you let me know what I am doing wrong here?

    Also this page is an authenticated page, like it appears after user logs in with their username/pwd. Just letting you know this, so that you can have a better picture of this page. Please let me know if you have any questions.
     
  4. beginner_101

    beginner_101 New Member

    Messages:
    4
    Actually I think I made mistake in calling the javascript function. So I declared the function as you said:

    function formSubmit()
    {
    document.getElementById("report_filter").submit();
    }

    and called this in the "onchange" event as:

    <form method="post" id="report_filter" action="<?php echo $_SERVER[PHP_SELF]?>" >
    <select name="try" onchange="formSubmit();">
    <option value="all">All</option>
    <option value="Windows">Windows</option>
    <option value="Win 2008">Win 2008</option>
    <option value="Win 7">Win 7</option>
    <option value="Win Vista">Win Vista</option>
    <option value ="Linux">Linux</option>
    <option value = "Win 7/Linux">Win 7/Linux</option>
    </select>
    </form>

    The page reloads when I select an option, but a blank white page comes up after this. Please let me know if you have any idea about this.
     
  5. chrishirst

    chrishirst Well-Known Member Staff Member

    Messages:
    2,639
    No idea, your server side code has to generate the HTML once the form has been submitted.
     
  6. AndrewZerra

    AndrewZerra New Member

    Messages:
    1
    Well if their is any mistake i your java code doesn't matter you can exclude it.and make a new new function and call it wherever you want.
     

Share This Page