Click to See Complete Forum and Search --> : echo inside an echo???
rch10007
08-16-2005, 03:34 AM
Can I place an echo statement inside another echo that creates a form.
I am trying to create a form "on-the-fly" and I want the action to post to itself.
It would look something like this, but since this doesn't work - maybe not!
Can I do this?
echo ("<form action=\"{<?php echo $_SERVER['PHP_SELF']; ?>}\" method=\"post\">\n");
or
echo ("<form action=\"" . <?php echo $_SERVER['PHP_SELF']; ?> ."\" method=\"post\">\n");
??
Why do you want one echo inside another?
This works: echo("<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n");Arto
Wart_Hog
08-16-2005, 03:58 AM
Or even better:
echo ('<form action="'.$_SERVER['PHP_SELF'].'" method="post">'."\n");
When printing out html with variables in the mix, I find it easier to read/debug if I use single quotes. Using single quotes (to wrap your strings) will allow you to use double quotes inside the strings without messy backslashes everywhere.
-Mike
BeachSide
08-16-2005, 05:06 AM
to answer your question...
No you cannot do it like you have it up there. echo isn't really a function it is a construct so you can't have <?php ... ?> inside of it
The best way is like Wart_Hog said use single quotes for something simple like that with lots of double quotes inside of it. Otherwise I like to use double quotes and have the variable inside of it...
If you want to really get crazy O_o (ok I'm a nerd laugh and point fingers) use heredoc for big chunks of code that way you don't have to worry about quotes at all and even newlines etc...
echo <<<FORM
<form action="{$_SERVER['PHP_SELF']}" method="post">
A whole bunch of form stuff...
...
...
...
More form stuff...
blah blah "some quotes"
...
maybe even a {$variable} or {$two}
blah blah some single 'quotes'
</form>
FORM;
rch10007
08-16-2005, 05:48 AM
I KNEW that! This project has me forgetting the basics - thx for clearing my eyes, if only for a short time! LOL
thx guys
bokeh
08-16-2005, 11:00 AM
One last thing... I often see people using this construct:
echo ('<form action="'.$_SERVER['PHP_SELF'].'" method="post">'."\n");
but sometimes people save forms and fill them in later and it is very frustrating finding the form doesn't work only after having filled it in so I recommend:
echo ('<form action="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'" method="post">'."\n");
rch10007
08-16-2005, 07:00 PM
thx all:
this is actually what I was looking for:
'SCRIPT_FILENAME'
The absolute pathname of the currently executing script.
Note: If a script is executed with the CLI, as a relative path, such as file.php or ../file.php, $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user.
This is the best way to get the absolute URL everytime.
rch10007
08-16-2005, 07:08 PM
I AM WRONG!!!
That's not what I was looking for!
Which variable calls only the page name??? Like: "page.php" not listing the directory too. (dir/another_dir/page.php) Is there one?
bokeh
08-16-2005, 07:34 PM
As far as I know there isn't one so you will need to extract it.<?php
$url = array_reverse(explode('/', $_SERVER['PHP_SELF']));
$filename = $url['0'];
print $filename;
?>
bokeh
08-29-2005, 03:49 AM
I AM WRONG!!!
That's not what I was looking for!
Which variable calls only the page name??? Like: "page.php" not listing the directory too. (dir/another_dir/page.php) Is there one?
basename()
print basename('dir/another_dir/page.php');// prints 'page.php'