PHP代码位于顶部,并在其他地方回显

时间:2015-01-13 16:25:46

标签: php database

我在页面顶部有这个代码:

    <?php
if(isset($_POST['submit']))
{

if(!isset($_SESSION['IS_LOGEDIN']))
{
    $_SESSION['IS_LOGEDIN'] = 'N';
}
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes(mysql_real_escape_string($myusername));
$mypassword = stripslashes(mysql_real_escape_string($mypassword));

$encrypted_mypassword=md5($mypassword);

$sql="SELECT * FROM administratori WHERE username='$myusername' and password='$encrypted_mypassword'";
$result=mysql_query($sql);


// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['IS_LOGEDIN'] = 'Y';
//$lastlogin = $_POST['date("d/m/y  H:i:s", time()+25200)'];
header("location:administrare.php");
}
else {
echo '<img src="images/delete.png" width="16" height="16" />' . '&nbsp;' .'User sau Password gresit!';
}
}
?>

在HTML表单中显示echo需要做些什么?因为回显显示在页面顶部。请帮我! :)

3 个答案:

答案 0 :(得分:0)

    <?php
if(isset($_POST['submit']))
{

if(!isset($_SESSION['IS_LOGEDIN']))
{
    $_SESSION['IS_LOGEDIN'] = 'N';
}
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes(mysql_real_escape_string($myusername));
$mypassword = stripslashes(mysql_real_escape_string($mypassword));

$encrypted_mypassword=md5($mypassword);

$sql="SELECT * FROM administratori WHERE username='$myusername' and password='$encrypted_mypassword'";
$result=mysql_query($sql);


// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['IS_LOGEDIN'] = 'Y';
//$lastlogin = $_POST['date("d/m/y  H:i:s", time()+25200)'];
header("location:administrare.php");
}
else {
$img='<img src="images/delete.png" width="16" height="16" />' . '&nbsp;' .'User sau Password gresit!';
}
}
?>

你的HTML代码......

<?php echo $img; ?>

你的其他HTML代码......

答案 1 :(得分:0)

只需将测试放在表单中:

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['IS_LOGEDIN'] = 'Y';
//$lastlogin = $_POST['date("d/m/y  H:i:s", time()+25200)'];
header("location:administrare.php"); 
}
else {
echo '<img src="images/delete.png" width="16" height="16" />' . '&nbsp;' .'User sau Password    gresit!';
}
}

答案 2 :(得分:0)

只需将其发布到您的表单区域即可。

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['IS_LOGEDIN'] = 'Y';
//$lastlogin = $_POST['date("d/m/y  H:i:s", time()+25200)'];
header("location:administrare.php");
}
else {
echo '<img src="images/delete.png" width="16" height="16" />' . '&nbsp;' .'User sau Password gresit!';
}

或者如果你想要你可以使用jquery 使用以下代码进行php

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['IS_LOGEDIN'] = 'Y';
//$lastlogin = $_POST['date("d/m/y  H:i:s", time()+25200)'];
header("location:administrare.php");
}
else {
echo '<div id="test"><img src="images/delete.png" id="image1" width="16" height="16" /></div>' . '&nbsp;' .'User sau Password gresit!';
}

将以下代码用于jquery

<script>
$(document).ready(function(){
var r = $("#test").html();
$("#formid").append(r);
});
</script>

工作正常请在这里查看http://jsfiddle.net/n2zmno6v/1/

希望这有助于你