为什么php文件无法使用get方法从html文件中获取数据?

时间:2015-06-24 06:02:48

标签: php html5

我开始学习php。我也尝试从html文件中获取数据并将所有这些数据打印在php文件中。但它没有用。我在这里想用php发送电子邮件。

这是我的html文件:

contact.html

<form id="contact-form" action="contact_email.php" method="get">
    <div class="contact-form-loader"></div>
    <fieldset>

        <label class="name">
            <input type="text" name="name" placeholder="Name:" value=""
                   data-constraints="@Required @JustLetters"/>
            <span class="empty-message">*This field is required.</span>
            <span class="error-message">*This is not a valid name.</span>
        </label>

        <label class="email">
            <input type="text" name="email" placeholder="Email:" value=""
                   data-constraints="@Required @Email"/>
            <span class="empty-message">*This field is required.</span>
            <span class="error-message">*This is not a valid email.</span>
        </label>

        <label class="phone">
            <input type="text" name="phone" placeholder="Phone:" value=""
                   data-constraints="@JustNumbers"/>
            <span class="empty-message">*This field is required.</span>
            <span class="error-message">*This is not a valid phone.</span>
        </label>


        <label class="message">
            <textarea name="message" placeholder="Message:"
                      data-constraints='@Required @Length(min=20,max=999999)'></textarea>
            <span class="empty-message">*This field is required.</span>
            <span class="error-message">*The message is too short.</span>
        </label>

        <div class="btn-wrapper">
            <a class="btn_2 text_3 color_7" href="#" data-type="reset">Clear</a>
            <a class="btn_2 text_3 color_7" name="submit" href="#" data-type="submit">Send</a>
        </div>
    </fieldset>
    <div class="modal fade response-message">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                            aria-hidden="true">&times;</button>
                    <h4 class="modal-title">Modal title</h4>
                </div>
                <div class="modal-body">
                    You message has been sent! We will be in touch soon.
                </div>
            </div>
        </div>
    </div>
</form>

contact_email.php

<?php
    if(isset($_GET['submit'])){

        $name=$_GET['name'];
        $email=$_GET['email'];
        $phone=$_GET['phone'];  
        $message=$_GET['message'];

        echo $name;
        echo $email;
        echo $phone;
        echo $message;

        $to='mmm@xxxx.com';
        $subject='New mail';
        $headers='From : '.$email."r\n".
                 'Reply-To : '.$email."r\n".
                 'X-Mailer: PHP/' . phpversion(); 
        mail($to,$subject,$message,$headers);

    }

    ?>

为什么这不能从html文件中获取数据?

2 个答案:

答案 0 :(得分:2)

a代码不会成为$_GET的一部分,因为它们不会作为表单数据提交。您必须拥有名称为input的{​​{1}}字段,才能使代码正常运行。或者您可以将其更改为其他字段。对于这种情况,submit应该是强制性的,因此您可以尝试使用 -

email

如果没有必要使用if(isset($_GET['email'])){ // rest of the code } 方法,请尝试使用GET方法。

答案 1 :(得分:1)

您的提交按钮在哪里。尝试以下html

<form id="contact-form" action="contact_email.php" method="get">

                        <div class="contact-form-loader"></div>

                        <fieldset>



                            <label class="name">

                                <input type="text" name="name" placeholder="Name:" value=""

                                       data-constraints="@Required @JustLetters"/>

                                <span class="empty-message">*This field is required.</span>

                                <span class="error-message">*This is not a valid name.</span>

                            </label>



                            <label class="email">

                                <input type="text" name="email" placeholder="Email:" value=""

                                       data-constraints="@Required @Email"/>

                                <span class="empty-message">*This field is required.</span>

                                <span class="error-message">*This is not a valid email.</span>

                            </label>



                            <label class="phone">

                                <input type="text" name="phone" placeholder="Phone:" value=""

                                       data-constraints="@JustNumbers"/>

                                <span class="empty-message">*This field is required.</span>

                                <span class="error-message">*This is not a valid phone.</span>

                            </label>





                            <label class="message">

                                <textarea name="message" placeholder="Message:"

                                          data-constraints='@Required @Length(min=20,max=999999)'></textarea>

                                <span class="empty-message">*This field is required.</span>

                                <span class="error-message">*The message is too short.</span>

                            </label>



                            <div class="btn-wrapper">

                                <a class="btn_2 text_3 color_7" href="#" data-type="reset">Clear</a>

                                <input type="submit" name="submit" value="send" class="btn_2 text_3 color_7" data-type="submit"/>

                            </div>

                        </fieldset>

                        <div class="modal fade response-message">

                            <div class="modal-dialog">

                                <div class="modal-content">

                                    <div class="modal-header">

                                        <button type="button" class="close" data-dismiss="modal"

                                                aria-hidden="true">&times;</button>

                                        <h4 class="modal-title">Modal title</h4>

                                    </div>

                                    <div class="modal-body">

                                        You message has been sent! We will be in touch soon.

                                    </div>

                                </div>

                            </div>

                        </div>

                    </form>

顺便说一下你应该使用POST方法代替GET情况,比如联系表格。