简体   繁体   中英

When html page loads/refreshes it triggers php document. I don't want that

I have website (dastiche.kz) and there is newsletter subscription. I have modified a bit my subscription, adding some "iframe", so that echo would appear without redirecting to another page. Everything now works well, but I now got annoying problem. Everytime I go to my main page(where the subscription form is located) or just refresh it, php is triggered and I receive blank email as though someone has made subscription. What did I do wrong?

<iframe name="myiframe" src="myiframe.php" width="100%" height="60px" frameborder="0"></iframe>
            <form method="POST" action="myiframe.php" class="subscribe" target="myiframe">
                <p><input type="text" name="Name" maxlength="10" style="font-family:'Times New Roman', Times, serif" value="Ваше имя"  onfocus="if (this.value == 'Ваше имя') {this.value = '';}" onblur="if (this.value == '') {this.value ='Ваше имя';}" class="line"></p>
                <p><input type="email" name="Email" maxlength="40" style="font-family:'Times New Roman', Times, serif" value="Ваш email"  onfocus="if (this.value == 'Ваш email') {this.value = '';}" onblur="if (this.value == '') {this.value ='Ваш email';}" class="line"></p>
                <p><input type="image" value="submit" name="Submit" src="img/subscribe.png" class="imgsub"></p>
            </form>

    <?php
Header("Content-Type: text/html; charset=utf-8");
$recipient = "order@dastiche.kz";
$subject = "Subscriber";
$name = $_POST['Name'];
$email = $_POST['Email'];
$location = "index.html";
$sender = $recipient;
$body .= "Name: ".$_REQUEST['Name']." \n";
$body .= "Email: ".$_REQUEST['Email']." \n";


if (($name != "") and ($email != ""))
// Если существуют проверяем... 
{
   if ((strlen($name) >= 2) and (strlen($name) <= 25))
   {
   $name = stripslashes($name);
   $name = html_entity_decode($name);
   $name = strip_tags($name);
   }
   else
   {
   echo " something is wrong with name field ";
   echo "<center><input name='back' type='button' value='get back'
   onclick= 'javascript:history.back()'></center>";
   }

   if (eregi("^[._a-zA-Z0-9-]+@[.a-zA-Z0-9-]+.[a-z]{2,6}$", $email))
   {
   $email = stripslashes($email);
   $email = htmlspecialchars($email);
   $email = strip_tags($email);
   }
   else
   {
   echo "There are some mistakes in the \"E-mail\" field";
   echo "<center><input name='back' type='button' value='try again'
   onclick= 'javascript:history.back()'></center>";
   }

}
// Если не существуют выводим сообщение... 
else
{
echo "Заполните следующие поля:";
}

   if (($name) and ($email))
{
   echo "Спасибо за подписку!";
}


mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
?>

As i Commented put a condition above of your code

if(isset($_POST['Submit']))
{
    // here put your complete php code
}

This will check if the POST request in received with the name of Submit . so the code will execute when you press the submit button Like

<?php
if(isset($_POST['Submit']))
{
    Header("Content-Type: text/html; charset=utf-8");
    $recipient = "order@dastiche.kz";
    $subject = "Subscriber";
    $name = $_POST['Name'];
    $email = $_POST['Email'];
    $location = "index.html";
    $sender = $recipient;
    $body .= "Name: ".$_REQUEST['Name']." \n";
    $body .= "Email: ".$_REQUEST['Email']." \n";


    if (($name != "") and ($email != ""))
    // ???? ?????????? ?????????... 
    {
       if ((strlen($name) >= 2) and (strlen($name) <= 25))
       {
       $name = stripslashes($name);
       $name = html_entity_decode($name);
       $name = strip_tags($name);
       }
       else
       {
       echo " something is wrong with name field ";
       echo "<center><input name='back' type='button' value='get back'
       onclick= 'javascript:history.back()'></center>";
       }

       if (eregi("^[._a-zA-Z0-9-]+@[.a-zA-Z0-9-]+.[a-z]{2,6}$", $email))
       {
       $email = stripslashes($email);
       $email = htmlspecialchars($email);
       $email = strip_tags($email);
       }
       else
       {
       echo "There are some mistakes in the \"E-mail\" field";
       echo "<center><input name='back' type='button' value='try again'
       onclick= 'javascript:history.back()'></center>";
       }

    }
    // ???? ?? ?????????? ??????? ?????????... 
    else
    {
    echo "????????? ????????? ????:";
    }

       if (($name) and ($email))
    {
       echo "??????? ?? ????????!";
    }


    mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be Sent");
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM