Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation getting the value of a varible to be recognised by a function?

    Hi All,

    i have a form which i have set as a function as i require calling this form in several sections of my code, the problem im having is that one my main page i have a varible which is being used in my form function but it doesnt recognise its value?

    here is my function
    PHP Code:
        <?php 
        
    function store_filterForm(){
        
    ?>
        <div class="form">
    <form id="filter" action="stores.php" method="get">
        <fieldset>
            <div>
                <div class="label">
                <label for="cat_select">Category</label></div>
                <select id="cat_select" name="cds">
                <?php
                
    print $category;
                    
    $options=array('entertainment','computing','electronics','clothing','health','misc');
                    foreach(
    $options as $option) {
                        if(
    $category==$option) {
                            echo 
    "<option value=\"{$option}\" selected=\"selected\">{$option}</option>";
                        }
                        else {
                            echo 
    "<option value=\"{$option}\">{$option}</option>";
                        }
                    }
                
    ?>
              </select>
            </div>
            <div>
                <div class="label">
                <label for="filter_by">Filter By</label></div>
                <select id="filter_by" name="filter_list">
                <?php
                    $options
    =array('None','Name','Rating');
                    foreach(
    $options as $option) {
                        if(
    $b==$option) {
                            echo 
    "<option value=\"{$option}\" selected=\"selected\">{$option}</option>";
                        }
                        else {
                            echo 
    "<option value=\"{$option}\">{$option}</option>";
                        }
                    }
                
    ?>
                </select>
            </div>
            <input type="submit" name="submit" id="submit" value="Filter" />
        </fieldset>
    </form>
    </div>
    <?php
    }
    ?>
    and the varible im trying to use is $category which i have tried printing out but nothing printed proving that the function is having trouble retrieving the value?

    i tried adding the varible to the function call like so
    PHP Code:
    store_filterForm($category); 
    but it said something about argument 1 missing so im assuming i need two arguments but im not sure what

    any ideas please
    Thanks

  • #2
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    you need to add variable to function, here

    PHP Code:
      <?php 
        
    function store_filterForm($category){

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hmm ok thats got rid of the error but its still not retaining the combo box value, which it did before i changed it into a function?

    this is how i set the $category on the main page
    Code:
    if(isset($_GET['cds'])){
    $category = $_GET['cds'];
    }
    else {
    $category = $_SESSION['page_category'];
    }
    but my print statment in my form doesnt print out any value?

    any ideas
    thanks
    Luke

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok got it working

    had to pass another varible as part of the function as well. its working a treat

    thanks mate
    Luke


  •  

    Posting Permissions

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