By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,049 Members | 1,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,049 IT Pros & Developers. It's quick & easy.

$_SERVER[PHP_SELF] and <SELECT>

P: n/a
When I select Februar here and sends, selection returns to Januar.
I know why: no option is marked selected...
But can php get this right in an easy way?
<html><head><title>Part of a bigger page</title></head>
<?php
$form ="
<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
<select name=obs_mnd>
<option value=\"Jan\">Januar</option>
<option value=\"Feb\">Februar</option>
<option value=\"Des\">Desember</option>
</select>
<input type=\"submit\" name=\"submit\" value=\"Send\">
</form>
";
echo "$form"
?>
</body></html>
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"Salve Håkedal" <ik**************@fiolinmaker.no> ???????/???????? ?
???????? ?????????: news:bY******************@news4.e.nsc.no...
When I select Februar here and sends, selection returns to Januar.
I know why: no option is marked selected...
But can php get this right in an easy way?
<html><head><title>Part of a bigger page</title></head>
<?php
$form ="
<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
<select name=obs_mnd>
<option value=\"Jan\">Januar</option>
<option value=\"Feb\">Februar</option>
<option value=\"Des\">Desember</option>
</select>
<input type=\"submit\" name=\"submit\" value=\"Send\">
</form>
";
echo "$form"
?>
</body></html>


Why are you suprised? When you press SUBMIT the same script is execute
(there is no selected item in it) due to action=\"$_SERVER[PHP_SELF]\", so
where is pointing for previous selected item? No.
Jul 17 '05 #2

P: n/a
"Salve Håkedal" <ik**************@fiolinmaker.no> schrieb im Newsbeitrag
news:bY******************@news4.e.nsc.no...
When I select Februar here and sends, selection returns to Januar.
I know why: no option is marked selected...
But can php get this right in an easy way?
<html><head><title>Part of a bigger page</title></head>
<?php
$form ="
<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
<select name=obs_mnd>
<option value=\"Jan\">Januar</option>
<option value=\"Feb\">Februar</option>
<option value=\"Des\">Desember</option>
</select>
<input type=\"submit\" name=\"submit\" value=\"Send\">
</form>
";
echo "$form"
?>
</body></html>


As you write the form into a variable and then output it, it makes it
complicated. I usually do:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="myform"
method="post">
<select name="obs_mnd">
<option value="Jan" <?php if(isset($obs_mnd) && $obs_mnd=="Jan") echo
"selected"; ?>>Januar</option>
....

HTH
Markus
Jul 17 '05 #3

P: n/a
I noticed that Message-ID: <40***********************@news.easynet.ch>
from Markus Ernst contained the following:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="myform"
method="post">
<select name="obs_mnd">
<option value="Jan" <?php if(isset($obs_mnd) && $obs_mnd=="Jan") echo
"selected"; ?>>Januar</option>

I do a similar thing, but use a function. No easy way AFAIK (for some
value of 'easy')
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #4

P: n/a
Markus Ernst skreiv:
"Salve Håkedal" <ik**************@fiolinmaker.no> schrieb im Newsbeitrag
news:bY******************@news4.e.nsc.no...
When I select Februar here and sends, selection returns to Januar.
I know why: no option is marked selected...
But can php get this right in an easy way?
<html><head><title>Part of a bigger page</title></head>
<?php
$form ="
<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
<select name=obs_mnd>
<option value=\"Jan\">Januar</option>
<option value=\"Feb\">Februar</option>
<option value=\"Des\">Desember</option>
</select>
<input type=\"submit\" name=\"submit\" value=\"Send\">
</form>
";
echo "$form"
?>
</body></html>


As you write the form into a variable and then output it, it makes it
complicated. I usually do:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="myform"
method="post">
<select name="obs_mnd">
<option value="Jan" <?php if(isset($obs_mnd) && $obs_mnd=="Jan") echo
"selected"; ?>>Januar</option>
...

HTH
Markus

Thank you. This is neat and I can understand it.
But my script should lead the user back to the form if it's not properly
filled out, while it's going to present the user with only "Thank you
....etc" if it _is_ properly filled out. Am I right to say that the form has
to be buildt as a variable to achieve that?

$alve
Jul 17 '05 #5

P: n/a
Dennis Biletsky skreiv:

"Salve Håkedal" <ik**************@fiolinmaker.no> ???????/???????? ?
???????? ?????????: news:bY******************@news4.e.nsc.no...
When I select Februar here and sends, selection returns to Januar.
I know why: no option is marked selected...
But can php get this right in an easy way?
<html><head><title>Part of a bigger page</title></head>
<?php
$form ="
<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
<select name=obs_mnd>
<option value=\"Jan\">Januar</option>
<option value=\"Feb\">Februar</option>
<option value=\"Des\">Desember</option>
</select>
<input type=\"submit\" name=\"submit\" value=\"Send\">
</form>
";
echo "$form"
?>
</body></html>


Why are you suprised? When you press SUBMIT the same script is execute
(there is no selected item in it) due to action=\"$_SERVER[PHP_SELF]\", so
where is pointing for previous selected item? No.

I ain't surprised !
$alve
Jul 17 '05 #6

P: n/a
"Salve Håkedal" <ik**************@fiolinmaker.no> wrote in message
news:bY******************@news4.e.nsc.no...
When I select Februar here and sends, selection returns to Januar.
I know why: no option is marked selected...
But can php get this right in an easy way?
<html><head><title>Part of a bigger page</title></head>
<?php
$form ="
<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
<select name=obs_mnd>
<option value=\"Jan\">Januar</option>
<option value=\"Feb\">Februar</option>
<option value=\"Des\">Desember</option>
</select>
<input type=\"submit\" name=\"submit\" value=\"Send\">
</form>
";
echo "$form"
?>
</body></html>


To interpolate a array element into a string you need to use curly brackets.

$form = "<form action\"{$_SERVER['PHP_SELF']}\"> ... ";
Jul 17 '05 #7

P: n/a
On Tue, 6 Apr 2004 18:25:32 -0400, "Chung Leong" <ch***********@hotmail.com>
wrote:
"Salve Håkedal" <ik**************@fiolinmaker.no> wrote in message
news:bY******************@news4.e.nsc.no...

<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">


To interpolate a array element into a string you need to use curly brackets.

$form = "<form action\"{$_SERVER['PHP_SELF']}\"> ... ";


Strictly speaking you don't NEED to, but you certainly SHOULD do.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #8

P: n/a
"Salve Håkedal" <ik**************@fiolinmaker.no> wrote in message
news:JK******************@news2.e.nsc.no...
Thank you. This is neat and I can understand it.
But my script should lead the user back to the form if it's not properly
filled out, while it's going to present the user with only "Thank you
...etc" if it _is_ properly filled out. Am I right to say that the form has to be buildt as a variable to achieve that?


No. For a conditional block of HTML you can do:

<? if($is_properly_filled_out) { ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="myform"
method="post">
<select name="obs_mnd">
....
<? }?>

or as I prefer it

<? if($is_properly_filled_out): ?>
<form action="<?=$_SERVER['PHP_SELF']?>" name="myform"
method="post">
<select name="obs_mnd">
....
<? endif; ?>

For HTML that appears often, you can define it as a function

<? function PrintLoginForm($name) { ?>
<? $name = htmlspecialchars($name); ?>
<form>
<table><tr><td>
<input name="name" value="<?=$name?>"><br>
...
</table>
</form>
<? } ?>
Jul 17 '05 #9

P: n/a

"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:fp********************************@4ax.com...

Strictly speaking you don't NEED to, but you certainly SHOULD do.


Why, you are right! Though I would swear that's something that can't be
done. I guess you learn something new everyday.
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.