Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP FORM GET VALUES

Posted on 2011-09-29
6
Medium Priority
?
644 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 14

Assisted Solution

by:Kalpan
Kalpan earned 800 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 400 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 111

Accepted Solution

by:
Ray Paseur earned 800 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 111

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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
The viewer will learn how to count occurrences of each item in an array.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

660 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