Solved

PHP FORM GET VALUES

Posted on 2011-09-29
6
635 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 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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 110

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 110

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

617 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