...

View Full Version : Form and results on same page



holty
09-09-2003, 02:32 PM
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!

Nightfire
09-09-2003, 05:03 PM
You'd have something like this:



<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
}
?>

holty
09-10-2003, 09:16 AM
Thanks Nightfire....


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

Thanks

Nightfire
09-10-2003, 10:36 AM
You'd just need to change it to this.


<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.

holty
09-10-2003, 10:45 AM
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!

Nightfire
09-10-2003, 11:06 AM
Doh, I thought I read checkboxes. Sorry, still asleep :o

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

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

in each of your options, like


<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>

holty
09-10-2003, 11:37 AM
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?

Nightfire
09-10-2003, 11:39 AM
<?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 :rolleyes: Think I better go back to bed</edit>

holty
09-10-2003, 11:59 AM
Cool - getting 'Undefined index in day.....'

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

Don't go back to bed yet!! :) ;)

Íkii
09-10-2003, 01:04 PM
the first time the form appears 'Day' wouldn't be set, so you should test with isset or empty


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.

Nightfire
09-10-2003, 01:31 PM
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

holty
09-10-2003, 02:14 PM
magic guys - much appreciated



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum