Solved

PHP FORM GET VALUES

Posted on 2011-09-29
6
609 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 109

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 109

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 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