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 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form and results on same page

    Can i have a form and results on the same php page?

    If so how could I do it so - when the user first hits the page they see 2 select boxes, once they submit the results will be shown below with what was the form before having the values that were selected in (not reseted)

    Any ideas would be super!

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    You'd have something like this:

    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
    <input type="text" name="searched" value="<?php echo $_GET['searched']; ?>" /> 
    <input type="submit" name="submit" value="Search" />
    </form>
    <?php
    if(isset($_GET['submit'])){
      
    // do search stuff here
    }
    ?>

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Nightfire....


    How would you put the value back in if it was a select box rather than a text box field?

    Thanks

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    You'd just need to change it to this.
    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
    <input type="checkbox" name="searched" value="1" <?php if($_GET['searched']=="1"){ echo 'checked="checked"';} ?>/><br />
    <input type="checkbox" name="searched" value="2" <?php if($_GET['searched'] == "2"){ echo 'checked="checked"';} ?>/><br /> 
    <input type="submit" name="submit" value="Search" />
    </form>
    <?php
    if(isset($_GET['submit'])){
      
    // do search stuff here
    }
    ?>
    That might be right, I'm not sure though as I've not used checkboxes for years and forgotten how they work.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cheers Nightfire - thats really useful but really I was after a select (combo box). The user selects an option from the box and in the search page I would like it to show the option they selected rather than reseting...

    cheers for the help - your a star!

  • #6
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Doh, I thought I read checkboxes. Sorry, still asleep

    It's not really that different from the checkbox example I posted, all you need to do is change the
    PHP Code:
    <?php if($_GET['searched']=="1"){ echo 'checked="checked"';} ?>
    to

    <?php if($_GET['Yourselectboxname']=="OPTION VALUE"){ echo 'selected="selected"';} ?>

    in each of your options, like
    PHP Code:
    <select name="searched">
      <option value="1" <?php if($_GET['searched'] == "1"){ echo 'selected="selected"';} ?> />First search</option>
      <option value="2" <?php if($_GET['searched'] == "2"){ echo 'selected="selected"';} ?> />Second search</option>
    </select>
    Last edited by Nightfire; 09-10-2003 at 10:09 AM.

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Nightfire....

    Got a problem though when I try this..

    (I'm trying to display a day select box with 1 to 31)

    <?php
    for($i=1; $i<=31; $i++){
    print("<option value="$i" if($_GET['Day'] == "$i"){ echo 'selected="selected"';}>$i</option>
    }
    ?>

    getting parse errors... any ideas?

  • #8
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    PHP Code:
    <?php
    for($i=1$i<=31$i++){
    print(
    "<option value='".$i."'");
    if(
    $_GET['Day'] == $i){ 
        echo 
    'selected="selected"';
    }
    print(
    ">".$i."</option>");
    }
    ?>
    <edit>4th time edited Think I better go back to bed</edit>
    Last edited by Nightfire; 09-10-2003 at 10:44 AM.

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cool - getting 'Undefined index in day.....'

    How do I define it? I'm just starting out in the world of php

    Don't go back to bed yet!!

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the first time the form appears 'Day' wouldn't be set, so you should test with isset or empty

    PHP Code:
    for($i 1$i 32$i++)
      {
      echo 
    '<option value="' .$i'"'
      
    .((isset($_GET['Day']) && $_GET['Day'] == $i) ? ' selected="selected"' '').
      
    '>' .$i'</option>';
      } 
    would be how I'd do it.
    Last edited by Ökii; 09-10-2003 at 12:06 PM.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #11
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Went back to bed and I see okii got my code properly lol, knew it looked weird when I did it, just couldn't realise why it was weird

  • #12
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    magic guys - much appreciated


  •  

    Posting Permissions

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