Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-09-2003, 01:32 PM   PM User | #1
holty
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 262
Thanks: 0
Thanked 0 Times in 0 Posts
holty is an unknown quantity at this point
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!
holty is offline   Reply With Quote
Old 09-09-2003, 04:03 PM   PM User | #2
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
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
}
?>
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 09-10-2003, 08:16 AM   PM User | #3
holty
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 262
Thanks: 0
Thanked 0 Times in 0 Posts
holty is an unknown quantity at this point
Thanks Nightfire....


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

Thanks
holty is offline   Reply With Quote
Old 09-10-2003, 09:36 AM   PM User | #4
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
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.
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 09-10-2003, 09:45 AM   PM User | #5
holty
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 262
Thanks: 0
Thanked 0 Times in 0 Posts
holty is an unknown quantity at this point
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!
holty is offline   Reply With Quote
Old 09-10-2003, 10:06 AM   PM User | #6
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
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>
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks

Last edited by Nightfire; 09-10-2003 at 10:09 AM..
Nightfire is offline   Reply With Quote
Old 09-10-2003, 10:37 AM   PM User | #7
holty
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 262
Thanks: 0
Thanked 0 Times in 0 Posts
holty is an unknown quantity at this point
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?
holty is offline   Reply With Quote
Old 09-10-2003, 10:39 AM   PM User | #8
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
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>
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks

Last edited by Nightfire; 09-10-2003 at 10:44 AM..
Nightfire is offline   Reply With Quote
Old 09-10-2003, 10:59 AM   PM User | #9
holty
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 262
Thanks: 0
Thanked 0 Times in 0 Posts
holty is an unknown quantity at this point
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!!
holty is offline   Reply With Quote
Old 09-10-2003, 12:04 PM   PM User | #10
Ökii
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 577
Thanks: 0
Thanked 0 Times in 0 Posts
Ökii is an unknown quantity at this point
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.
__________________
Ökii - formerly pootergeist
teckis - take your time and it'll save you time.

Last edited by Ökii; 09-10-2003 at 12:06 PM..
Ökii is offline   Reply With Quote
Old 09-10-2003, 12:31 PM   PM User | #11
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
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
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 09-10-2003, 01:14 PM   PM User | #12
holty
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 262
Thanks: 0
Thanked 0 Times in 0 Posts
holty is an unknown quantity at this point
magic guys - much appreciated
holty is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:49 AM.


Advertisement
Log in to turn off these ads.