PHP GET方法正在获取错误的数据

时间:2016-05-19 09:54:14

标签: php

我填写下一个表格:

 <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:xxx@gmail.com?Subject=Hello%20again" target="_top">
                        <button type="submit" class="btn btn-primary btn-lg">Enviar</button>
                    </a>
                </div>
            </div>
        </fieldset>
    </form>

PHP方面:

<?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 = 'xxx@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - 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: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to, $email_subject, $email_body, $headers);
return true;
?>

我认为GET方法是错误的数据,我用文本字符串填充表单,但echo方法是打印1: enter image description here

如果我用真正的字符串填充表单,为什么echo方法为每个字段打印1?此外,我认为警告是因为参数都是“1”,但这对我来说毫无意义。

4 个答案:

答案 0 :(得分:2)

您已将isset()的值分配给变量:

  

确定变量是否已设置且不为NULL。

当设置变量时,这将返回1

$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;

以下代码将检查$_GET变量是否已设置,然后分配:

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;
}

答案 1 :(得分:1)

您正在打印isset()的返回值,而不是您正在测试的数组索引,以查看它是否已设置。

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

应该更符合以下方面:

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

或者您可以使用三元运算符:

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

答案 2 :(得分:0)

你的问题在那里:

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

由于$ _GET存在,isset($_GET['name'])返回1,您存储在$name中。 $name现在等于1(或true)。

做类似

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

答案 3 :(得分:0)

你需要使用如下: -

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