0

I fill the next form:

 <form name="frmContacto" class="form-horizontal" method="GET" action="./mail/contact_me.php">
     <fieldset>
            <legend class="text-center header">Formulario de contacto</legend>

            <div class="form-group">
                <span class="col-md-1 col-md-offset-2 text-center"><i
                        class="fa fa-user bigicon"></i></span>
                <div class="col-md-8">
                    <input id="name" name="name" type="text" placeholder="Nombre"
                           class="form-control">
                </div>
            </div>

            <div class="form-group">
                <span class="col-md-1 col-md-offset-2 text-center"><i
                        class="fa fa-envelope-o bigicon"></i></span>
                <div class="col-md-8">
                    <input id="email" name="email" type="text" placeholder="Correo electrónico"
                           class="form-control">
                </div>
            </div>

            <div class="form-group">
                <span class="col-md-1 col-md-offset-2 text-center"><i
                        class="fa fa-phone-square bigicon"></i></span>
                <div class="col-md-8">
                    <input id="phone" name="phone" type="text" placeholder="Teléfono"
                           class="form-control">
                </div>
            </div>

            <div class="form-group">
                <span class="col-md-1 col-md-offset-2 text-center"><i
                        class="fa fa-pencil-square-o bigicon"></i></span>
                <div class="col-md-8">
                    <textarea class="form-control" id="message" name="message"
                              placeholder="Introduce aquí tu mensaje." rows="7"></textarea>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-12 text-center">
                    <a href="mailto:[email protected]?Subject=Hello%20again" target="_top">
                        <button type="submit" class="btn btn-primary btn-lg">Enviar</button>
                    </a>
                </div>
            </div>
        </fieldset>
    </form>

PHP side:

<?php

$name = isset($_GET['name']);
echo $name;
$email_address = isset($_GET['email']);
echo $email_address;
$phone = isset($_GET['phone']);
echo $phone;
$message = isset($_GET['message']);
echo $message;

// Create the email and send the message
$to = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to.
$email_subject = "Formulario de contacto web:  $name";
$email_body = "Ha recibido un nuevo mensaje desde la web.\n\n" . "Detalles:\n\Nombre: $name\n\nEmail: $email_address\n\Teléfono: $phone\n\Mensaje:\n$message";
$headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
$headers .= "Reply-To: $email_address";
mail($to, $email_subject, $email_body, $headers);
return true;
?>

I think GET method is getting wrong data, I fill the form with text strings, but the echo methods are printing 1: enter image description here

If I fill the form with real strings, why is the echo method printing 1 for each field? Also, I think that the warning is because the parameters are all "1", but this make no sense for me.

4 Answers 4

2

You have assigned the value of isset() to the variable:

Determine if a variable is set and is not NULL.

This will return 1 when the variable is set.

$name = isset($_GET['name']);
echo $name;
$email_address = isset($_GET['email']);
echo $email_address;
$phone = isset($_GET['phone']);
echo $phone;
$message = isset($_GET['message']);
echo $message;

The following code will check if the $_GET variable is set, then assign it:

if (isset($_GET['name'])) { $name = $_GET['name'];
echo $name;
}
if (isset($_GET['email'])) { $email_address = $_GET['email'];
echo $email_address;
}
if (isset($_GET['phone'])) { $phone = $_GET['phone'];
echo $phone;
}
if (isset($_GET['message'])) { $message = $_GET['message'];
echo $message;
}
Sign up to request clarification or add additional context in comments.

1 Comment

To add to this answer, you can also use $name = isset($_GET['name']) ? $_GET['name'] : false;
1

You are printing the return value of isset() and not the array index you are testinging to see if it is set.

$name = isset($_GET['name']);

should be something more along the lines of:

$name = "";
if (isset($_GET['name'])) {
    $name = $_GET['name'])
};

or you could use a ternary operator:

$name = (isset($_GET['name'])) ? $_GET['name'] : "";

Comments

0

Your problem is there:

$name = isset($_GET['name']);
echo $name;

Because $_GET exists, isset($_GET['name']) returns 1, which you store in $name. $name is equal to 1 now (or true).

do something like

if(isset($_GET['name']))
{
   echo $_GET['name'];
}

Comments

0

You need to use like below:-

 if(isset($_GET['name']))
 {
 $name = $_GET['name'];
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.