Solved

PHP FORM GET VALUES

Posted on 2011-09-29
6
600 Views
Last Modified: 2012-05-12
I cannot get the values when

                                                  <div class="indent-left">
                                                  <h3 class="p2">Contact Form</h3>
                                                    <form id="contact-form" method="post" enctype="multipart/form-data" action="contacts.php">                    
                                                        <fieldset>
                                                              <label><span class="text-form">Name:</span><input name="name" type="text" /></label>
                                                              <label><span class="text-form">Email:</span><input name="email" type="text" /></label>   
                                                              <label><span class="text-form">Phone:</span><input name="phone" type="text" /></label>                                    
                                                              <div class="wrapper"><div class="text-form">Message:</div><textarea name="message"></textarea></div>

                                                              <div class="buttons">
                                                                  <a class="button" href="#" onClick="document.getElementById('contact-form').reset()">Clear</a>
                                                                  <a class="button" href="#" onClick="document.getElementById('contact-form').submit()">Send</a>
                                                              </div>                             
                                                        </fieldset>						
                                                    </form>
                                                </div>
                                            
                                                <?php
                                                if (isset($_GET['name'])){
                                                
                                                $name = $_GET['name'] ;
                                                $email = $_GET['email'] ;
                                                $phone = $_GET['phone'] ;
                                                $message = $_GET['message'] ;
                                                    
                                                mail( "myemail@gmail.com", "NEW MESSAGE FROM - ".$name." - ".$phone , $message, "From: $email" );
                                                print "<h3 class=p6><br>Thank you for your message.</h3>";
                                                
                                                }
                                                ?>

Open in new window

0
Comment
Question by:rockas1982
6 Comments
 
LVL 14

Assisted Solution

by:Kalpan
Kalpan earned 200 total points
ID: 36813784
Please change the method from post to get

<form id="contact-form" method="post" enctype="multipart/form-data" action="contacts.php">  

to

<form id="contact-form" method="get" enctype="multipart/form-data" action="contacts.php">  
0
 
LVL 10

Expert Comment

by:ienaxxx
ID: 36813797
kalmax was faster...
:-)
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 36813806
It is good that you use POST method .. as you have message field which may have more character/text to send..

GET method won't be safe to send.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 100 total points
ID: 36813810
so you could use,

<?php
                                                if (isset($_POST['name'])){
                                               
                                                $name = $_POST['name'] ;
                                                $email = $_POST['email'] ;
                                                $phone = $_POST['phone'] ;
                                                $message = $_POST['message'] ;
                                                   
                                                mail( "myemail@gmail.com", "NEW MESSAGE FROM - ".$name." - ".$phone , $message, "From: $email" );
                                                print "<h3 class=p6><br>Thank you for your message.</h3>";
                                               
                                                }
                                                ?>
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 200 total points
ID: 36813868
You can change from $_GET to $_POST and that might work for you.  You could also try using $_REQUEST.

The GET arguments are created by a form with method="get" which is the default if the method attribute is omitted.  URL arguments show up in the $_GET array.  Example: http://path/to/script.php?q=aBc will give you $_GET["q"]===aBc

The POST arguments are created by a form with method="post" and they show up in $_POST.

No matter what the request method, the $_GET and $_POST arrays are set.  They may be empty, however.

You can use var_dump() to print out the contents of the $_GET or $_POST arrays.

You must use method="post" if your script makes any changes to the underlying data model.  Get-method requests are for display purposes only.

PHP has a tutorial about handling forms
http://us2.php.net/manual/en/tutorial.forms.php

You might also benefit from this excellent little book
http://www.sitepoint.com/books/phpmysql4/

HTH, ~Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36813886
Now having said all that, as I look at your script I see that it may become an open-relay for spam.  This script shows how to "sanitize" the variables so it does not give a hacker or spammer the ability to add headers to your messages.  A small CAPTCHA test might be useful, too.

Best of luck with your project, ~Ray
<?php // RAY_form_to_email.php
error_reporting(E_ALL);



// SEND MAIL FROM A FORM



// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";



// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // DISABLED ON THE SERVER SIDE
    var_dump($_POST);
    die(' DISABLED');

    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $telephone  = clean_string($_POST["telephone"]);

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION
    $content    = NULL;
    $content   .= "You have a New Query From $name" . PHP_EOL . PHP_EOL;
    $content   .= "Tel No: $telephone" . PHP_EOL;
    $content   .= "Email: $email" . PHP_EOL;

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="email" />
<br/>Phone: <input name="telephone" />
<br/>Name:  <input name="name" />
<br/><input type="submit" />
</form>
ENDFORM;

echo $form;



// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    // IF MAGIC QUOTES IS ON, WE NEED TO REMOVE SLASHES
    $str = stripslashes($str);

    // REMOVE EXCESS WHITESPACE
    $rgx
    = '/'               // REGEX DELIMITER
    . '\s'              // MATCH THE WHITESPACE CHARACTER(S)
    . '\s+'             // MORE THAN ONE CONTIGUOUS INSTANCE OF WHITESPACE
    . '/'               // REGEX DELIMITER
    ;
    $str = preg_replace($rgx, ' ', $str);

    // REMOVE UNWANTED CHARACTERS
    $rgx
    = '/'               // REGEX DELIMITER
    . '['               // START OF A CHARACTER CLASS
    . '^'               // NEGATION - MATCH NONE OF THE CHARACTERS IN THIS CLASS
    . 'A-Z0-9&+:?_.- '  // CHARACTERS WE WANT TO KEEP
    . ']'               // END OF THE CHARACTER CLASS
    . '/'               // REGEX DELIMITER
    . 'i'               // CASE-INSENSITIVE
    ;
    $str = preg_replace($rgx, NULL, $str);

    return $str;
}

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Error installing extension Linux 4 34
HTML CSS 7 19
uploading image to a profile page 1 5
Birthdays 3 14
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now