使用$ _GET []会导致发送带邮件的电子邮件()

时间:2014-03-14 20:29:14

标签: php email url

在我的网站上,它显示了一个节目标题列表。单击标题时,将显示内容和电子邮件表单。表格只需要一封电子邮件,并将标题作为主题和电子邮件中页面的内容邮寄。

该链接将传递变量' info' ' info' 包含我数据库中帖子的ID。单击提交按钮时会出现此问题。它不会发送电子邮件,并刷新页面。这会导致网址丢失' 变量并丢失页面上的所有内容。

如果我在php中对ID进行硬编码并且不使用$ _GET [' info'],该页面的效果非常好。 有什么我想念的吗?

<?php
            $id = $_GET['info'];

            /*****************************************************
                    open conection to the mySQL database
            ******************************************************/
            $configs = include('config.php');

            //Create a connection
            $con = mysqli_connect(
                $configs['host'],           //host
                $configs['username'],           //username
                $configs['password'],           //password
                $configs['dbname']          //dbname
            );

            if (!$con)
            {
                die('Could not connect: ' . mysqli_error($con));
            }

            /*****************************************************
                    Populate the page 
            ******************************************************/
            $sql="
                SELECT  p.post_title, p.post_content
                FROM    they_posts AS p
                WHERE   p.ID='".$id."'
            ";

            $result = mysqli_query($con,$sql);


            while($row = mysqli_fetch_array($result))
            {
                /*Title*/
                echo '<h1>'.$row['post_title'].'</h1><hr>';

                /*content*/
                echo '<h2>Details: </h2><br>'.$row['post_content'].'<br>';

                $title= $row['post_title'];
                $content = $row['post_content'];

                /*****************************************************
                            E-mail Form 
                ******************************************************/

                include('includes/email_test.php');
            }

?>

这是email_test.php

<div data-role="collapsible">
<h1>Send Reminder Email</h1>

    <?php
        function spamcheck($field)
        {
            // Sanitize e-mail address
            $field=filter_var($field, FILTER_SANITIZE_EMAIL);
            // Validate e-mail address
            if(filter_var($field, FILTER_VALIDATE_EMAIL))
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

        // display form if user has not clicked submit
        if (!isset($_POST["submit"]))
        {
    ?>

    <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
        Your Email: <input type="text" name="to"><br>
        <input type="submit" name="submit" value="Submit Feedback">
    </form>

    <?php 
        }
        else    // the user has submitted the form                          
        {
            // Check if the "from" input field is filled out
            if (isset($_POST["to"]))
            {
                // Check if "from" email address is valid
                $receivecheck = spamcheck($_POST["to"]);

                if ($receivecheck==FALSE)
                {
                    echo "Invalid input";
                }

                else
                {
                    $to = $_POST["to"]; //receiver
                    $subject = $title;
                    $message = $content;
                    // message lines should not exceed 70 characters (PHP rule), so wrap it
                    $message = wordwrap($message, 70);
                    // send mail
                    mail("$to",$subject,$message,"From: noreply@address.com\n");
                    echo "reminder has been sent";
                }
            }
        }
    ?>
</div>

我使用了isset($ id)来显示按下提交时的后退按钮。这将带回信息,但电子邮件仍未发送。

2 个答案:

答案 0 :(得分:0)

$_GET['info']仅在您的表单定义为method='GET'时才有效。

如果您的表单定义为method='POST',那么您需要使用$_POST['info']

如果您希望代码能够正常工作,无论表单是使用GET还是POST,请使用$_REQUEST['info']

答案 1 :(得分:0)

在您的方案中,您必须在当前网址中info=post_id才能获得$_GET['info']

第一路:
像这样更改表单操作:

<form method="post" action="<?php echo $_SERVER["PHP_SELF"].'?info='.$_GET['info']; ?>">

然后在行动中它将是:

/your_page.php?info=current_post_id

然后在操作页面中,您可以通过$_GET['info']

获取信息

第二路:

或者您可以在表单中为post_id添加额外的隐藏表单字段,如下所示:

 <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
        Your Email: <input type="text" name="to"><br>
        <input type="submit" name="submit" value="Submit Feedback">
        <input type="hidden" name="post_id" value="<?php echo $_GET['info'];">
    </form>

之后在您的操作页面中,您可以$_POST['post_id']

获取post_id

应该有意义!

由于