Forum Moderators: coopster

Message Too Old, No Replies

Need help with a $ SERVER['PHP SELF']

         

Simone100

8:32 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



Hello, I am trying to have a form send to the page the commands are already in, instead of to a separate php form. So that the success echo messages will show in the web page itself instead of a blank white page.

I am having trouble with a php self in the following code, using it either like this
print "<form action='<?=echo $_SERVER['PHP_SELF']?>' method='post'>
or this without the echo like this.
print "<form action='<?=$_SERVER['PHP_SELF']?>' method='post'>

This is the error message I am getting. Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING for the 'PHP_SELF' line.

Heres everything after the query, everything works fine up to this point.
$result=mysqlquery etc.
while ($row = mysql_fetch_array($result))
{
print "<form action='<?=echo $_SERVER['PHP_SELF']?>' method='post'>
<input type='text' name='anemailadress' size=15>
<input type='submit' name='submit' value='Submitting' /></form>"; //adding the new fields the form will send to here which variables will be sending information to someone email.
$mailto = 'anemailaddress' ;//Email address where the form fields will be sent.
mail($mailto, etc. and so on.;}}";
echo 'this line will work when the php self works, right now I get the error message instead';
?>

Can anyone see what I am doing wrong with the php self and if the new commands that the form is sending to at the end of the form are in the right place? Between the correct brackets and double quotes? Thank you very much.

eelixduppy

8:35 pm on Jan 2, 2007 (gmt 0)



Try this:

echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>";

;)

Simone100

9:08 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



Hey there! OK your making me very nervous having me get rid of the print completely and replace it with the echo but I'll try eeek, lol.

Looks like that helped a little. Now I'm getting the same error but for this line. My first email variable that the form is supposed to send to.

$mailto = 'anemailaddress'

Is it trying to tell me, I supposed to be putting my $_POST['anemailaddress'] directly inot the form itself instead of like how I have it like this?

$mailto = 'anemailaddress'

If so, how, like this?
<input style='type='text' name='$_POST['anemailaddress']' size=15>

Or does the error mean something else.

cmarshall

9:18 pm on Jan 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<input style='type='text' name='$_POST['anemailaddress']' size=15>

Without seeing the context, I can't tell, but you are using single quotes for everything, and you are not using the PHP string concatenation character (.), which does not bode well for syntax checking.

[edited by: cmarshall at 9:18 pm (utc) on Jan. 2, 2007]

eelixduppy

9:18 pm on Jan 2, 2007 (gmt 0)



Ok, first off, echo [us2.php.net] and print [us2.php.net] are almost identical. For our purposes let's just say they are. ;) Anyway, onto the rest here...


Now I'm getting the same error but for this line

You must remember to add the semi-colon to the end ;)


$mailto = 'anemailaddress';

Now for the rest of your question I need some clarification. What are you trying to do? I can see that you are querying a database to extract something (email addresses maybe?) to place into a form, and then you want the contents of that form to be emailed to a specific email address?

Is this even remotely correct? :)

Simone100

9:27 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



Sorry about that, I had the semi colon, just forgot to paste it with the rest. Well gee, you gave me a small idea. If that doesn't work, I'm sending you the whole code to your pm. Hope you don't mind. Maybe you can use some of it but as smart as you are you probably have all your own. Then you can reply to my private message again here so we can pick up again here. You'll probably here from me in the pm soon.

Thanks for the print and echo tip, I thought form fields could only go in prints and echos were well....echos!

coopster

9:53 pm on Jan 2, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Just one security concern that I note here ... PHP_SELF is user-supplied data. Anything that is user-supplied cannot be trusted. At the very least you should strip_tags [php.net] the data prior to printing.

mcavic

9:54 pm on Jan 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



name='$_POST['anemailaddress']'

When you're working with arrays, you never want brackets inside quotes if you can help it. Even if it works, it's hard to read.

Stick the email address into a variable first, then use that variable.

Simone100

11:09 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



You know I wondered if that had something to do with it because thats what the last error seemed to be saying. It didn't work when I tried it like that the first time, but I'll try exactly like that again. What about the variables at the end. Don't I still need these along with it in the name=?

$mailto = 'anemailaddress'; Do I need these?

Simone100

11:34 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



Mcavic please don't leave me now. I tried that,
<input style='type='text' name='$_POST['anemailaddress']' size=15>

It said it couldn't parse it. I don't have everything in double quotes because its inside a print or echo thats already in double quotes. What about this great string that eelix gave me. Even though I am not getting an error message on the PHP SELF anymore
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>

I'm a bit worried that the double quotes iside the first double quotes is incorrect. I tried changing them to single quotes or removing one of the quotes but it didn't like it again so better leave it as is. Why does this part allow double quotes when the rest doesn't?

Any other good suggestions on what you see wrong with it please get back to me.

cmarshall

11:52 pm on Jan 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<input style='type='text' name='$_POST['anemailaddress']' size=15>

<voice in the wilderness>
use a period as string concatenator
</voice in the wilderness>

coopster

12:06 am on Jan 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Simone100, you may want to read all about strings [php.net] in the PHP manual first. The PHP online manual pages are going to be one of your biggest resources and understanding how to print out strings will be vital to your programming growth and development. Of course, so will WebmasterWorld and we are always here to help you out with further issues. But I really recommend reading through the link given here on strings.

mcavic

12:33 am on Jan 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I agree with coopster. Fiddling around with a language without fully understanding it is very frustrating, if not dangerous. For beginners, I think books [amazon.com] are usually easier to follow than Web pages.

<input style='type='text' name='$_POST['anemailaddress']' size=15>

My preferred fix for this would be:
$email = $_POST['anemailaddress'];
echo "<input type='text' name='$email' size=15>\n";

echo "<form action='" . $_SERVER['PHP_SELF'] . "' method='post'>\n";

This should work too, and the quotes are correct. What it's doing is concatenating the contents of the PHP_SELF variable into the middle of the string that you're outputting.

Simone100

2:18 am on Jan 3, 2007 (gmt 0)

10+ Year Member



Thanks guys but I read php manual till I'm blue in the face and don't pick it up as easily as some. Usually when I read whats in the manual I leave going "huh"? But there are 'some' good examples in there I can understand. Usually I understand most of php, its the things I have never used before that I don't understand in the manual that get me. The frustrating part for me on this one is it tells me it wants and is expecting a T String. But you think php manual has anything about T strings? Nope. I wish they would use terms in their error messages that you can find in the manual. In the meantime I appreciate the help and will try your last post. ;)

eelixduppy

2:27 am on Jan 3, 2007 (gmt 0)



Simone, here is a very good page for you to bookmark: Appendix Q. List of Parser Tokens [us3.php.net] ;)

cmarshall

3:56 am on Jan 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



90% of being an expert is knowing where to look things up. I have a memory like a tea strainer, so I need to look things up all the time.

There's an old saying, attributed to Nasrudin (and repeated by such people as Rita Mae Brown and Will Rogers) that goes:

"Good judgment comes from experience. Experience comes from bad judgment."

I have a great deal of experience. I look forward to good judgment.

Simone100

4:14 am on Jan 3, 2007 (gmt 0)

10+ Year Member



Thanks a lot eelix and it looks like this is almost figured out, I hope. That was a BIG step in the right direction.
That helped a lot Mcavic, thank you so much for getting back to me!
My biggest problem is I have been working on this same script for many weeks now and I am getting very, very, tired of it, being so close just makes me want to finish it once and for all, so excuse my excitement.

I tried putting just a variable in the input fields before, but the mistake I made was not putting these
$email = $_POST['anemailaddress'];

*BEFORE* the form input fields. Makes perfect sense now, good thinking! I hope someone knows the last piece to this so it is finally done.
This is the error I am getting now.
Parse error: parse error, unexpected $ in the ending line?>

Pretty funny just my luck, the last line is the end of the php so something else is obviously wrong. But these php error messages have been clues so I think its just saying it is expecting something more that I don't have just before this?> I just don't know what it is. Or its saying my php opening and closing tags are incorrect within the php somewhere or something else I'm not understanding.

I kept this
echo "<form action='" . $_SERVER['PHP_SELF'] . "' method='post'> but I put this at the end of all the php like I show in my below example. "; Because I am pretty sure it has to wrap around the whole PHP SELF echo. I need to keep it because the form has to send to an email, input fields all by themselves won't work.

This is where I'm at now, I am including all the php opening and closing tags elsewhere in the script so that maybe someone can see something wrong with them that I can't spot, since its saying that
it doesn't like the ending closing tag.

<?php
$con = mysql_connect("my","database","password")
OR die('Could not connect: ' . mysql_error());
mysql_select_db("database", $con) OR die(mysql_error());
if(!$_POST){?><form action="" method="post"><input type="text" size="12" name="websitename"
value="<?=$_POST['websitename']?>" />" /><input type="submit" value="submitting" /></form><?}
else{$modify = mysql_real_escape_string($_POST['websitename']);
$query = "SELECT websitename FROM tablename WHERE websitename = '$modify'";
$result = mysql_query($query) or die('Error with query' . mysql_error());
while ($row = mysql_fetch_array($result))
{
// and on to the sending form fields to an email address.
$mailto = $_POST['anemailaddress'];
$test = $_POST['$row'];
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>
<input type='text' name='$mailto' size=15>
<input type='text' name='$test' size=15>
<input type='submit' name='submit' value='Submitting' /></form>";
$subject = 'Email subject';
$uself = 0;
$headersep = (!isset( $uself ) ¦¦ ($uself == 0))? '\r\n' : '\n' ;
'The E-mail: $mailto\n' . 'Testing field: $test\n';
mail($mailto, $subject, $messageproper, 'From: \'$mailto\' <$mailto>' . $headersep . 'Reply-To: \'$mailto\' <$mailto>' . $headersep);}}";
echo "this line will work when the php self works, right now I get the last error message instead";
?>

The error message says. Parse error: parse error, unexpected $ in the ending line?>
Thanks a lot!

Simone100

7:43 am on Jan 3, 2007 (gmt 0)

10+ Year Member



Ha, I like that cmarshall. Guess that means I'm getting pretty smart.
It does get a bit scary, the more you do this stuff, the more you start to understand it. But I'd really like to understand this PHP SELF technique.

I stripped it down completely. I finally got the items to appear without the error messages but then it wouldn't send to an email so I am analyzing this instead, took everything out but the very basics where the problem is. When I open this in a browser it does not like the line with the POST in it whether I put it on the top or the bottom.
$another = $_POST['anotherfield'];
The error message says. Parse error: unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
Does anyone know why the line with the $_Post won't parse correctly? Once I get this understood, then I can add other items later. Thanks.

<?php
echo "
$mailto = 'websitename@whatever.com';
$another = $_POST['anotherfield'];
<form action='".$_SERVER['PHP_SELF']."' method='post'>
<input type='text' name='$mailto' size=15>
<input type='text' name='$another' size=15>
<input type='submit' name='submit' value='Submitting' /></form>;
$subject = 'Email subject';
$uself = 0;
$headersep = (!isset( $uself ) ¦¦ ($uself == 0))? '\r\n' : '\n' ;
$messageproper = 'The E-mail: $mailto\n' . 'Another field: $another\n';
mail($mailto, $subject, $messageproper, 'From: '$mailto\' <$mailto>' . $headersep . 'Reply-To: \'$mailto\' <$mailto>' . $headersep);";
?>

cmarshall

11:37 am on Jan 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$another = $_POST['anotherfield'];
<form action='".$_SERVER['PHP_SELF']."' method='post'>
<input type='text' name='$mailto' size=15>
<input type='text' name='$another' size=15>
<input type='submit' name='submit' value='Submitting' /></form>;

You have a number of basic, serious issues here. You really do need to bone up on some of the basics. I'm not saying that to be snooty (I've been there myself). It's just that you are attempting to drive on the highway before finishing diver's ed. You will be frustrated and bedeviled until you figure this out.

In the one section above, you make some terrible assumptions that will cause a whole number of problems. PHP is a scripting language. It does not automatically place anything on the page when you are inside the PHP section (designated by <?php and?>). You treat it like a standard HTML page inside the PHP section, which makes pretty much the entire section a huge syntax error.

Also, when you concatenate strings, you can't just put one next to the other. They must be joined by a period (.).

One way to do the above is like so:

<?php $another = $_POST['anotherfield'];?>
<form action='"<?php echo $_SERVER['PHP_SELF']?>"' method='post'>
<input type='text' name='<?php echo $mailto?>' size=15>
<input type='text' name='<?php echo $another?>' size=15>
<input type='submit' name='submit' value='<?php echo Submitting?>' /></form>;[

Another way is to do it all in echoes, like so:

<?php $another = $_POST['anotherfield'];
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>
<input type='text' name='$mailto' size=15>
<input type='text' name='$another' size=15>
<input type='submit' name='submit' value='Submitting' /></form>";?>

I hope this helps.

cmarshall

2:01 pm on Jan 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I made a mistake in my example:

<form action='"<?php echo $_SERVER['PHP_SELF']?>"' method='post'>

Should be:

<form action='<?php echo $_SERVER['PHP_SELF']?>' method='post'>

Note I removed the quotes.

coopster

10:12 pm on Jan 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Don't forget to strip_tags as I mentioned earlier.
<form action='<?php echo strip_tags($_SERVER['PHP_SELF']);?>' method='post'>

cmarshall

10:54 pm on Jan 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't forget to strip_tags as I mentioned earlier.

[code[<form action='<?php echo strip_tags($_SERVER['PHP_SELF']);?>' method='post'>[/code]

Good point. I'm trying to habituate myself to strip_tags and mysql_real_escape_string.

Simone100

6:54 am on Jan 4, 2007 (gmt 0)

10+ Year Member



OK thanks, lol but I'm getting a bit confused, you said to use periods then both changed it to no periods at the end. I'd really like to know the proper way. I'll try it all ways but all this testing with no luck is starting to get to me.

So I guess I should re evaluate all this again before more testing. Hoping you guys can tell me if this php self is really going to help me.

For instance. PHP SELF looks like when the form sends it is supposed to stay on the same page in order to see the echos on the same page.
But I don't understand how php self is supposed to help send the form fields to the email. I looked all over for this explanation all over the web with no luck. Please explain so I can understand this better. Then I will know how to test further. Thanks all for your nice help, very, very, much.

Simone100

8:52 am on Jan 4, 2007 (gmt 0)

10+ Year Member



AYeeeeeeeee, I think I found it. Maybe you guys can use this for something too.

Thank you for all you nice guys help! If I can't get this to work for what I need, I'll be back in a new post, but it looks promising.

<?php
if($_POST['text']=="")
{
echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
echo "<p><input type=\"text\" name=\"text\"><p><input type=\"submit\" value=\"submit\"></p></form>";
}
else
{
echo $_POST['text'];
echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
echo "<p><input type=\"text\" name=\"text\"><p><input type=\"submit\" value=\"submit\"></p></form>";

}
?>

jatar_k

2:44 pm on Jan 4, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



for reference this thread has some basics in it
Basics of Submitting and Emailing Forms with PHP [webmasterworld.com]