Need help with PHP_SELF

Discussion in 'PHP' started by Jen-, Jan 4, 2007.

  1. #1
    Hi, here's some php self code that when you enter something into a form field, it echo's on the same web page, while showing the same form over and over again. You can paste it into notepad to see how cool it works. I need to have the form fields, also send to an email at the same time. Can someone tell me how? Thank you very much.

    <?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
    }
    ?>

    Usual email form that I need the above fields to send to as well at the same time. Not sure how to list it on the page so the email will work. Thank you very much.

    <?php
    $mailto = 'email@website.com';
    $sendfield = $_POST['text'];
    $subject = 'Email subject';
    $from = "From: '$sendfield'" . "\n" .
    $messageproper = 'Field sent to this email: $sendfield\n';
    mail('$mailto','subject',$messageproper,$from);
    ?>
     
    Jen-, Jan 4, 2007 IP
  2. tanfwc

    tanfwc Peon

    Messages:
    579
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hope this code helps you.

    <?php
    if(empty($_POST['submit'])) {
    	echo stripslashes($_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>
    Code (markup):
    Demo : http://tanfwc.net/testing/demo1.php
     
    tanfwc, Jan 4, 2007 IP
  3. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What does it do? Thanks.
     
    Jen-, Jan 4, 2007 IP
  4. tanfwc

    tanfwc Peon

    Messages:
    579
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I code accordingly to the code above and clean up unnecessary stuff on it. :)

    Isn't that what you want?
     
    tanfwc, Jan 4, 2007 IP
  5. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    So I guess it doesn't send to an email yet.
     
    Jen-, Jan 4, 2007 IP
  6. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #6
    Here's the email code you wanted to be added.
    This is not a safe email form. You must add some checks before taking it live on the web.

    <?php
    if($_POST['text']=="")
    {
    print '
    <form method="post" action="">
    <p><input type="text" name="text"><p><input type="submit" value="submit"></p></form>
    ';
    }
    else
    {
    echo $_POST['text'];
    $mailto = 'email@website.com';
    $sendfield = $_POST['text'];
    $subject = 'Email subject';
    $from = "From: ".$sendfield. "\n" ;
    $messageproper = 'Field sent to this email: $sendfield\n';
    mail($mailto,$subject,$messageproper,$from);

    print '
    <form method="post" action="">
    <p><input type="text" name="text"><p><input type="submit" value="submit"></p></form>
    ';
    }
    ?>
     
    JEET, Jan 4, 2007 IP
  7. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Wow Jeet very, good but I don't understand how it works. How does everything appear on the same page without the PHP SELF?

    And a minor problem, this is showing in the email instead of the text entered and not sure why. Field sent to this email: $sendfield
    Please get back to me, Id really like to know how this is working. Thanks :)
     
    Jen-, Jan 4, 2007 IP
  8. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #8
    Change the message line to the below in the above code.

    $messageproper = 'Field sent to this email: '.$sendfield.'\n';

    About your other question, notice the <form action="" part?
    That means to submit the form to the same page/script. You can Post the form to another page or to the same page for processing.
    Bye :)
     
    JEET, Jan 4, 2007 IP
  9. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    That is sure great, cuts out all that PHP SELF stuff, who knew! I added the code underneath the following, gathering a field from my sql.

    When I do it send 5 fields to my email instead of just one. Can anyone see why its duplicating 5 times instead of just once? Thank you very much.
     
    Jen-, Jan 4, 2007 IP
  10. solidphp

    solidphp Peon

    Messages:
    46
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I think this is the problem, change this:

    $query = "SELECT websitename FROM getfield WHERE websitename = '$modify'"; 
    PHP:
    to this:


    $query = "SELECT DISTINCT websitename FROM getfield WHERE websitename = '$modify'"; 
    PHP:
    - Andy
     
    solidphp, Jan 4, 2007 IP
  11. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    VERY GOOD! That was it! Thanks. Jen
     
    Jen-, Jan 4, 2007 IP
  12. solidphp

    solidphp Peon

    Messages:
    46
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    No problem Jen, I'm glad I could help. Thanks for the PM!
     
    solidphp, Jan 4, 2007 IP