Terance Wijesuriya
Terance Wijesuriya

Reputation: 1986

Why a php file cannot get data from html file using get method?

I've started learning php.Also I've tried to get data from html file and print all these data in a phpfile . But it wasn't worked.I here I wanted to send a email using php.

This is my html file:

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='[email protected]';
        $subject='New mail';
        $headers='From : '.$email."r\n".
                 'Reply-To : '.$email."r\n".
                 'X-Mailer: PHP/' . phpversion(); 
        mail($to,$subject,$message,$headers);

    }

    ?>

Why this cannot get the data from html file ?

Upvotes: 1

Views: 398

Answers (2)

Sougata Bose
Sougata Bose

Reputation: 31749

a tags will not be a part of $_GET as they are not submitted as form data. You must have some input field with name submit to make your code work. Or you can change it to some other fields. For this case the email should be compulsory so you can try with -

if(isset($_GET['email'])){ 
    // rest of the code 
}

And if it is not necessary to use GET method then try to use POST method instead.

Upvotes: 2

Bokul
Bokul

Reputation: 395

Where is your submit button. try the following 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>

BTW you should use POST method instead of GET situation like contact form.

Upvotes: 1

Related Questions