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
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
Link to comment
Share on other sites

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@website.com';
$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
}
?>
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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