对不起,如果我做错了,是我在StackOverflow上的第一个问题。
我想在执行函数内的几次检查后写入var的值,因此如果所有内容都是okey想要在特定div中打印变量。我创建了三个不同的div,一个用于显示错误,一个用于显示var,另一个用于表单。
我这样做是做什么的: https://mega.co.nz/#!3EQGQSAL!94ao1u6UhARYaYdjJzfFqY6ln1oPyO9JQ4_ElT7WEkA
代码:
<html>
<head>
<meta charset="UTF-8">
<title>Agenda Alex Ventura</title>
<style type="text/css">
#errores {
display: block;
background-color: grey;
border-color: red;
border-top-style: double;
border-right-style: double;
border-bottom-style: double;
border-left-style: double;
}
#contactos {
margin-top: 20px;
background-color: aquamarine;
}
#formulario{
margin-top: 50px;
}
</style>
</head>
<body>
<div id="errores">
<?php
if (!(isset($_POST['enviar']))) {
//Primera vez
$nombre = "";
$telefono = "";
} else {
if (errores()) {
//Segunda o siguientes veces con error
$nombre = "";
$telefono = "";
} else {
//Todo ok
$nombre = $_POST['nombre'];
$telefono = $_POST['telefono'];
imprimir_datos();
}
}
function errores() {
$nombre = $_POST['nombre'];
$telefono = $_POST['telefono'];
if ((empty($nombre)) || (empty($telefono))) {
echo "Los campos de usuariio y telefono no pueden estar vacíos";
return true;
}
if (strlen($telefono) < 9) {
echo "El telefono debe tener 9 digitos " . strlen($telefono);
return true;
}
if (strlen($telefono) > 9) {
echo "El telefono debe tener 9 digitos " . strlen($telefono);
return true;
}
$expresion = '[0-9]';
if (preg_match($expresion, $telefono) != true) {
echo "El telefono debe tener solo numeros " . ($telefono);
return true;
}
return false;
}
?>
</div>
<div id="contactos">
<?php
function imprimir_datos() {
$nombre = $_POST['nombre'];
$telefono = $_POST['telefono'];
echo "Nombre: $nombre, Telefono: $telefono</br>";
}
?>
</div>
<div id="formulario">
<form action=<?php echo $_SERVER['PHP_SELF'] ?> method="POST">
Nombre : <input type="text" name="nombre" value="<?php echo "$nombre"; ?>"/>
Telefono: <input type="text" name="telefono" value="<?php echo "$telefono"; ?>"/>`enter code here`
<input TYPE="submit" VALUE="Enviar" name="enviar"/>
</form>
</div>
</body>
https://mega.co.nz/#!CA5TmCQJ!utWGpH7PZwpPcHbDK3eTG6JMuTRz_Z6KKAFWtbqPm1o
答案 0 :(得分:1)
改变这个:
if (preg_match($expresion, $telefono) != true) {
到此:
if (preg_match("/" . $expresion . "/", $telefono) != true)
要打印细节,试试这个。将函数从div移到外部,并在div中调用它。
<div id="contactos"><?php imprimir_datos(); ?></div>
<?php
function imprimir_datos() {
$nombre = $_POST['nombre'];
$telefono = $_POST['telefono'];
echo "Nombre: $nombre, Telefono: $telefono</br>";
}
?>