10/10/2018, 11:22
[HỎI] Vì sao biến $_POST[''submit''] không được nhận
Đoạn mã này của tôi khung nhập liệu hiển thị bình thường nhưng khi submit dù có nhập hay không nhập dữ liệu thì nó vẫn cứ refresh lại trang nhập liệu chứ không nhảy đến trang php và hiển thị các xử lí trong lệnh if. Rất mong nhận được sự trợ giúp của các bạn.
<?PHP
$page_tittle="Dang Ki";
include ('./header.inc');
if (isset($_POST['submit']))
{
if (strlen($_POST['name']) >0)
{
$name=TRUE;
} else
{
$name=FALSE;
echo "<p>Ban chua nhap ten</p>";
}
if (strlen($_POST['username'])>0)
{
$username=TRUE;
} else
{
$username=FALSE;
echo "<p>Ban chua nhap Username</p>";
}
if (strlen($_POST['email']) > 0)
{
$email=TRUE;
} else
{
$email=FALSE;
echo "<p>Ban chua nhap email</p>";
}
if (strlen($_POST['password1']) > 4)
{
if ($_POST['password1'] == $_POST['password2'])
{
$password = TRUE;
} else
{
echo "<p>Mat ma xac nhan ko trung</p>";
}
} else
{
$password = FALSE;
echo "Ban chua nhap mat ma hoac mat ma it hon 4 ki tu";
}
if ($name && $username && $email && $password)
{echo "Ban da dang ki thanh cong";}
else
{echo "Hay thu lai lan nua";}
} else
{
?>
<form action="<?PHP echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset> <legend>Hay nhap thong tin cua ban</legend>
<p><b>Ten:</b> <input type="text" name"name" size="40" maxlength="40" /> </p>
<p><b>Username:</b> <input type="text" name"username" size="40" maxlength="30" /> </p>
<p><b>Email:</b> <input type="text" name"email" size="50" maxlength="60" /> </p>
<p><b>Password:</b> <input type="password" name"password1" size="50" maxlength="20" /> </p>
<p><b>Xac nhan:</b> <input type="password" name"password2" size="50" maxlength="20" /> </p>
</fieldset>
<div align="center"> <input type="submit" name"submit" value="submit" /> </div>
</form>
<?PHP
}
include ('./footer.inc');
?>
$page_tittle="Dang Ki";
include ('./header.inc');
if (isset($_POST['submit']))
{
if (strlen($_POST['name']) >0)
{
$name=TRUE;
} else
{
$name=FALSE;
echo "<p>Ban chua nhap ten</p>";
}
if (strlen($_POST['username'])>0)
{
$username=TRUE;
} else
{
$username=FALSE;
echo "<p>Ban chua nhap Username</p>";
}
if (strlen($_POST['email']) > 0)
{
$email=TRUE;
} else
{
$email=FALSE;
echo "<p>Ban chua nhap email</p>";
}
if (strlen($_POST['password1']) > 4)
{
if ($_POST['password1'] == $_POST['password2'])
{
$password = TRUE;
} else
{
echo "<p>Mat ma xac nhan ko trung</p>";
}
} else
{
$password = FALSE;
echo "Ban chua nhap mat ma hoac mat ma it hon 4 ki tu";
}
if ($name && $username && $email && $password)
{echo "Ban da dang ki thanh cong";}
else
{echo "Hay thu lai lan nua";}
} else
{
?>
<form action="<?PHP echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset> <legend>Hay nhap thong tin cua ban</legend>
<p><b>Ten:</b> <input type="text" name"name" size="40" maxlength="40" /> </p>
<p><b>Username:</b> <input type="text" name"username" size="40" maxlength="30" /> </p>
<p><b>Email:</b> <input type="text" name"email" size="50" maxlength="60" /> </p>
<p><b>Password:</b> <input type="password" name"password1" size="50" maxlength="20" /> </p>
<p><b>Xac nhan:</b> <input type="password" name"password2" size="50" maxlength="20" /> </p>
</fieldset>
<div align="center"> <input type="submit" name"submit" value="submit" /> </div>
</form>
<?PHP
}
include ('./footer.inc');
?>
Bài liên quan
Bạn đổi cái đó thành trang php mà chứa code xử lý đi bạn.
[=========> Bổ sung bài viết <=========]
@dokhacluan bạn thay như vậy thì nó gần như đồng nghĩa mà. Không xử lý được cái này.
ini_set('display_errors', 0);
$page_tittle="Dang Ki";
include ('./header.inc');
if ($_POST['form_abc']=="form_abc")
{
if (strlen($_POST['name']) >0)
{
$name=TRUE;
} else
{
$name=FALSE;
echo "<p>Ban chua nhap ten</p>";
}
if (strlen($_POST['username'])>0)
{
$username=TRUE;
} else
{
$username=FALSE;
echo "<p>Ban chua nhap Username</p>";
}
if (strlen($_POST['email']) > 0)
{
$email=TRUE;
} else
{
$email=FALSE;
echo "<p>Ban chua nhap email</p>";
}
if (strlen($_POST['password1']) > 4)
{
if ($_POST['password1'] == $_POST['password2'])
{
$password = TRUE;
} else
{
echo "<p>Mat ma xac nhan ko trung</p>";
}
} else
{
$password = FALSE;
echo "Ban chua nhap mat ma hoac mat ma it hon 4 ki tu";
}
if ($name && $username && $email && $password)
{echo "Ban da dang ki thanh cong";}
else
{echo "Hay thu lai lan nua";}
} else
{
?>
<form action="" method="post">
<fieldset> <legend>Hay nhap thong tin cua ban</legend>
<p><b>Ten:</b> <input type="text" name"name" size="40" maxlength="40" /> </p>
<p><b>Username:</b> <input type="text" name"username" size="40" maxlength="30" /> </p>
<p><b>Email:</b> <input type="text" name"email" size="50" maxlength="60" /> </p>
<p><b>Password:</b> <input type="password" name"password1" size="50" maxlength="20" /> </p>
<p><b>Xac nhan:</b> <input type="password" name"password2" size="50" maxlength="20" /> </p>
</fieldset>
<input type="hidden" name="form_abc" value="form_abc">
<div align="center"> <input type="submit" name"submit" value="submit" /> </div>
</form>
<?PHP
}
include ('./footer.inc');
?>
@bluewayvn : Bài tập này là dùng cả đoạn mã HTML và PHP trên cùng 1 page.
ini_set('display_errors', 0);
$page_tittle="Dang Ki";
include ('./header.inc');
//print_r($_POST);
if ($_POST['form_abc']=="form_abc")
{
if(trim($_POST['ten'])!="")
{
echo "ten cua ban : <b>$_POST[ten]</b> <br>";
}
else
{
echo "ban chua nhap ten <br>";
}
if(trim($_POST['username'])!="")
{
echo "ten cua ban : <b>$_POST[username]</b> <br>";
}
else
{
echo "ban chua nhap username <br>";
}
if(trim($_POST['password1'])!="")
{
echo "mat khau : <b>$_POST[password1]</b> <br>";
}
else
{
echo "ban chua nhap mat khau <br>";
}
if(trim($_POST['password2'])!="")
{
echo "mat khau 2 : <b>$_POST[password2]</b> <br>";
}
else
{
echo "ban chua nhap mat khau 2 <br>";
}
} else
{
?>
<form action="" method="post">
<legend>Hay nhap thong tin cua ban</legend>
<p><b>Ten:</b> <input type="text" name="ten" size="40" maxlength="40" /> </p>
<p><b>Username:</b> <input type="text" name="username" size="40" maxlength="30" /> </p>
<p><b>Email:</b> <input type="text" name="email" size="50" maxlength="60" /> </p>
<p><b>Password:</b> <input type="password" name="password1" size="50" maxlength="20" /> </p>
<p><b>Xac nhan:</b> <input type="password" name="password2" size="50" maxlength="20" /> </p>
<input type="hidden" name="form_abc" value="form_abc">
<input type="submit" name"submit" value="submit" />
</form>
<?PHP
}
include ('./footer.inc');
?>
<fieldset> <legend>Hay nhap thong tin cua ban</legend>
<p><b>Ten:</b> <input type="text" name"name" size="40" maxlength="40" /> </p>
<p><b>Username:</b> <input type="text" name"username" size="40" maxlength="30" /> </p>
<p><b>Email:</b> <input type="text" name"email" size="50" maxlength="60" /> </p>
<p><b>Password:</b> <input type="password" name"password1" size="50" maxlength="20" /> </p>
<p><b>Xac nhan:</b> <input type="password" name"password2" size="50" maxlength="20" /> </p>
</fieldset>
<input type="hidden" name="form_abc" value="form_abc">
<div align="center"> <input type="submit" name"submit" value="submit" /> </div>
</form>
<fieldset> <legend>Hay nhap thong tin cua ban</legend>
<p><b>Ten:</b> <input type="text" name="name" size="40" maxlength="40" /> </p>
<p><b>Username:</b> <input type="text" name="username" size="40" maxlength="30" /> </p>
<p><b>Email:</b> <input type="text" name="email" size="50" maxlength="60" /> </p>
<p><b>Password:</b> <input type="password" name="password1" size="50" maxlength="20" /> </p>
<p><b>Xac nhan:</b> <input type="password" name="password2" size="50" maxlength="20" /> </p>
</fieldset>
<input type="hidden" name="form_abc" value="form_abc">
<div align="center"> <input type="submit" name="submit" value="submit" /> </div>
</form>