• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

PHP FORM GET VALUES

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
rockas1982
Asked:
rockas1982
3 Solutions
 
KalpanCommented:
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
 
ienaxxxCommented:
kalmax was faster...
:-)
0
 
Loganathan NatarajanLAMP DeveloperCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Loganathan NatarajanLAMP DeveloperCommented:
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
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now