Jump to content

What does this do? $_SERVER['PHP_SELF']


11Tami

Recommended Posts

Hi, I already looked all over the web on this and still don't quite understand it.

From what I can see $_SERVER['PHP_SELF'] is supposed to keep the same page in the browser when a form sends. If you use it in the form action= area.

What I don't understand is usually in a form action you have to connect with what else you need the form to do. Such as sending form fields to something else. A database or an email or something. So if $_SERVER['PHP_SELF']
takes up the form action slot, how can you send the form fields?

Thanks a lot.
   
Link to comment
https://forums.phpfreaks.com/topic/32807-what-does-this-do-_serverphp_self/
Share on other sites

example
[code]
<?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'];
    }
?>
[/code]
like that
OK fert, do you know how I could send the form to an email, while its looping it to the same page? For instance. How can I get the same fields to send to an email, while its showing in the echos on the same page.

Usual email form.
<?php
$mailto = '[email protected]';
$sendfield = $_POST['sendfield']; 
$subject = 'Email subject'; 
$from = "From: '$sendfield'" . "\n" . 
$messageproper = 'Field sent to this email: $sendfield\n'; 
mail('$mailto','subject',$messageproper,$from);
?>

Your modified php self code, that keeps the form fields going. How to send to an email at the same time?
<?php
if($_POST['text']=="")
{
?><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><input type="text" name="text"><p><input type="submit" value="submit"></p></form>
<?php
}
else
     {
echo $_POST['text'];
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><input type="text" name="text"><p><input type="submit" value="submit"></p></form>
<?php
}
?>
[code]
<?php
  if($_POST['send_to']=="")
  {
        echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
        //insert form fields
    }
    else
    {
        $headers="From: ".$_POST['from']."<{$_POST['from']}>";
        mail($_POST['send_to'],$_POST['subject'],$_POST['msg'],$headers);
    }
?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.