Problem with $_SERVER['PHP_SELF']

Discussion in 'PHP' started by nova_joseph2000, May 5, 2007.

  1. #1
    <?php

    if($_POST['submit'] == 'Submit')
    {
    if(!$_POST['email'] ||$_POST['email'] == "" || strlen($_POST['email'] >30 ))
    {
    $message = '<p> There is a Problem .Did you enter an email address ?</p>';
    }
    else
    {
    //Open connection to the database
    mysql_connect("localhost","root","") or die ("Failure to communicate with database");
    mysql_select_db("test");

    //Insert email adresses
    $as_email = addslashes($_POST['email']);
    $tr_email = trim($as_email);
    $query ="INSERT INTO test4040 (ID,Email,Source)
    VALUES(NULL,'$tr_emial',www.example.com')";
    $result = mysql_query($query);
    if(mysql_affected_rows() == 1)
    {
    $message ='<p>Your record has been recorded.</p>';

    $noform_var = 1;
    }
    else {
    error_log(mysql_error());
    $message = '<p>Your information has been wrong eith your sing up attempt.</p>';
    }
    }

    //Show The form in every cae except successful submission

    if (!noform_var)
    {
    $thisfile =$_SERVER['PHP_SELF'];
    $message = <<< EOMSG
    <p>Enter your email in every address and we will send you our weekly newsletter .</p>
    <form method="post" action="$thisfile">
    <br /><br />
    <input type="submit" value="Submit" />
    </form>
    EOMSG;
    }
    }
    ?>
    <html>
    <head>
    <style type="text/css">
    <!--
    body,p {color:#000000; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px;}
    -->
    </style>
    </head>
    <body>
    <table border="0" cellpadding="10" width="100%">
    <tr>
    <td bgcolor="#fof8ff" align="center" valign="top" width="17%">
    </td>
    <td bgcolor="#ffffff" align="left" valign="top" width="83%">
    <h1>Newsletter sing up form</h1>
    <?php echo $message; ?>
    </td>
    </tr>
    </table>
    </body>
    </html>



    I was executed in the the above programe the result page is xecuted only


    php:
    <html>
    <head>
    <style type="text/css">
    <!--
    body,p {color:#000000; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px;}
    -->
    </style>
    </head>
    <body>
    <table border="0" cellpadding="10" width="100%">
    <tr>
    <td bgcolor="#fof8ff" align="center" valign="top" width="17%">
    </td>
    <td bgcolor="#ffffff" align="left" valign="top" width="83%">
    <h1>Newsletter sing up form</h1>
    <?php echo $message; ?>
    </td>
    </tr>
    </table>
    </body>
    </html>
     
    nova_joseph2000, May 5, 2007 IP
  2. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Are you receiving an error message?

    $thisfile =$_SERVER['PHP_SELF'];
    PHP:
    Add a space between the = and the $ and see what happens...
     
    dp-user-1, May 5, 2007 IP
  3. nova_joseph2000

    nova_joseph2000 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i put the space but it could not executed.


    if(!noform_var)
    {
    $thisfile =$_SERVER['PHP_SELF'];
    $message = <<< EOMSG
    <p>Enter your email in every address and we will send you our weekly newsletter .</p>
    <form method="post" action="$thisfile">
    <br /><br />
    <input type="submit" value="Submit" />
    </form>
    EOMSG;

    its not working
     
    nova_joseph2000, May 5, 2007 IP
  4. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #4
    You still have -----> =$_SERVER['PHP_SELF']

    Make it ------------> = $_SERVER['PHP_SELF']


    Get your spacing right, it's just neater.

    You really need to clean up your code.

    $message = <<< EOMSG <--- What are you trying to accomplish with that?

    Also, you aren't escaping any of the quotation marks after that line.
     
    dp-user-1, May 6, 2007 IP
  5. TechEvangelist

    TechEvangelist Guest

    Messages:
    919
    Likes Received:
    140
    Best Answers:
    0
    Trophy Points:
    133
    #5
    I didn't try to run your script, but I do see a malformed statement.

    if(!$_POST['email'] ||$_POST['email'] == "" || strlen($_POST['email'] >30 ))
    Code (markup):
    should be:
    if(!$_POST['email'] ||$_POST['email'] == "" || strlen($_POST['email']) > 30)
    Code (markup):
    You should be testing strlen($_POST['email']) to see if it is greater than 30. That is not what you are doing in your statement.


    You do not need spaces around equal signs in PHP, but you should be consistent with your formatting. It makes the code easier to read.
     
    TechEvangelist, May 6, 2007 IP
    GTech likes this.
  6. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #6
    if (!noform_var)
    {
    $thisfile =$_SERVER['PHP_SELF'];
    $message = <<< EOMSG
    <p>Enter your email in every address and we will send you our weekly newsletter .</p>
    <form method="post" action="$thisfile">
    <br /><br />
    <input type="submit" value="Submit" />
    </form>
    EOMSG;
    }
    PHP:
    This section of code will never be executed because you attempting to check the value of a constant when it should be the variable. Haven't looked over the rest so there may be other problems (such as the one posted by TechEvangelist) but changing that line to if (!$noform_var) should be a step in the right direction.

    @NinjaNoodles, I take it you're not familiar with the heredoc syntax?
     
    rodney88, May 6, 2007 IP
  7. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #7
    Ah, I stand corrected. I'm still eagerly learning PHP, and one method of that learning is trying to help others.

    Thanks for the correction, I'll do more research next time.
     
    dp-user-1, May 6, 2007 IP