gmc1103 Posted June 16, 2015 Share Posted June 16, 2015 Hello I have this fucntion and is working well but my div are not showing if (isset($_POST['submitted'])) { if($fgmembersite->ChangePassword()){ $result = '<div class="alert alert-success">Password alterada com sucesso</div>'; sleep(2); $fgmembersite->RedirectToURL("index.php"); } else{ $result = '<div class="alert alert-danger">A mudança de password não foi efetuadada....tente mais tarde</div>'; } } I expected to get teh result here <div class="form-group"> <?php echo $result; ?> </div> i'm having this error Notice: Undefined variable: result in /home/ebspma/public_html/gesdocente/newpass.php on line 81 Any help please? Quote Link to comment https://forums.phpfreaks.com/topic/296845-div-from-function/ Share on other sites More sharing options...
Ch0cu3r Posted June 16, 2015 Share Posted June 16, 2015 (edited) There does not appear to be anything wrong with that code you provided. I have this fucntion and is working well but my div are not showing Are you saying that this code is within a function your have defined yourself? if (isset($_POST['submitted'])) { if($fgmembersite->ChangePassword()){ $result = '<div class="alert alert-success">Password alterada com sucesso</div>'; sleep(2); $fgmembersite->RedirectToURL("index.php"); } else{ $result = '<div class="alert alert-danger">A mudança de password não foi efetuadada....tente mais tarde</div>'; } } eg function foo($bar...) { // your code here } And you are using/echo'ing $result outside of the function? If that is the case then functions have there own variable scope, meaning variables defined within the function is only available to that function, likewise variables defined outside the function cannot be used within the function. Please see the following documentation on the subject http://php.net/manual/en/language.variables.scope.php Read it but do not use GLOBALS as the solution! What you should do is have your function return the value of $result at the end of the function, eg function foo($bar...) { // your code return $result; // return the value of $result } When you call the function you assign it to a variable so you can capture the return value $result = foo($bar...); // call foo and capture the returned value Now $result will contain the value returned by your function. Edited June 16, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/296845-div-from-function/#findComment-1514034 Share on other sites More sharing options...
gmc1103 Posted June 16, 2015 Author Share Posted June 16, 2015 Hi Thanks for your reply This is the fucntion called from that piece of code function ChangePassword() { $email=trim($_POST['email']); if (!$this->CheckLogin()) { $this->HandleError("Não iniciou sessão!"); return false; } if (empty($_POST['oldpwd'])) { $this->HandleError("Password antiga vazia!"); return false; } if (empty($_POST['newpwd'])) { $this->HandleError("Nova password vazia!"); return false; } $user_rec = array(); if (!$this->GetUserFromEmail($this->UserEmail(), $user_rec)) { return false; } $pwd = trim($_POST['oldpwd']); $hash = md5($pwd); $nresult = mysqli_query($this->connection,"SELECT password FROM utilizador WHERE email = '$email'") or die(mysql_error()); $no_of_rows = mysqli_num_rows($nresult); if ($no_of_rows > 0) { $nresult = mysqli_fetch_array($nresult); $password = $nresult['password']; $password1 = md5($password); if ($password != $hash) { $this->HandleError("A password antiga não coincide com os nossos registos!"); return false; } } $newpwd = trim($_POST['newpwd']); if (!$this->ChangePasswordInDB($email, $newpwd)) { return false; } return true; } So, it the function returns true...it should be enought, no? Quote Link to comment https://forums.phpfreaks.com/topic/296845-div-from-function/#findComment-1514045 Share on other sites More sharing options...
Ch0cu3r Posted June 16, 2015 Share Posted June 16, 2015 Can you clarify where this code is being called from. if (isset($_POST['submitted'])) { if($fgmembersite->ChangePassword()){ $result = '<div class="alert alert-success">Password alterada com sucesso</div>'; sleep(2); $fgmembersite->RedirectToURL("index.php"); } else{ $result = '<div class="alert alert-danger">A mudança de password não foi efetuadada....tente mais tarde</div>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/296845-div-from-function/#findComment-1514048 Share on other sites More sharing options...
gmc1103 Posted June 16, 2015 Author Share Posted June 16, 2015 (edited) Yes This where is called from <?PHP error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); require_once("./include/membersite_config.php"); if (!$fgmembersite->CheckLogin()) { $fgmembersite->RedirectToURL("index.php"); exit; } if (isset($_POST['submitted'])) { if($fgmembersite->ChangePassword()){ $result = '<div class="alert alert-success">Password alterada com sucesso</div>'; sleep(2); $fgmembersite->RedirectToURL("index.php"); } else{ $result = '<div class="alert alert-danger">A mudança de password não foi efetuadada....tente mais tarde</div>'; } }} $email = $fgmembersite->UserEmail(); ?><html> <head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>EBSPMA Atividades</title> <link href="css/styles.css" rel="stylesheet"> <link href="css/bootstrap.min.css" rel="stylesheet"> <script src="js/modernizr.js?cb=2.2.3.2085"></script> <script type="text/javascript"> </script> </head> <body> <header role="banner" class="main-header"> <!-- MAIN HEADER --> <div class="main-header-body"> <div class="container-wrapper"> <div class="container-fluid"> <div class="row"> <div class="col-xs-3"> <h2>EBSPMA PAAD</h2> </div> <div class="col-xs-4 col-xs-offset-1 main-header-title"> <p class="sizeToggle" >Mudar Password</p> </div> </div> </div> </div> </div> </header><section class="content"> <div class="grid"> <div class="box"> <form name="changepwd" id="loginForm" action='<?php echo $fgmembersite->GetSelfScript(); ?>' method="POST"> <input type="hidden" name='submitted' id='submitted'/> <input type="hidden" name="path" value="painelAdquirente.action"/> <div> <p><b><font color="red">A sua password deve ser alterada!</font></b></p></div> <div class="icon-input"> <label for="password"></label> <div class="input password"> <i class="fa fa-lock"></i> <input type="password" name="oldpwd" id="oldpwd" placeholder="Senha antiga"> </div> </div> <br> <div class="icon-input"> <label for="password"></label> <div class="input password"> <i class="fa fa-lock"></i> <input type="password" name="newpwd" id="newpwd" placeholder="Senha nova"> </div> <input type="hidden" name="email" id="email" value="<?php echo $email; ?>"> </div> <br> <input type="submit" id="sbmtLogin" class="sa-btn1" value="Mudar"> </form> </div> </div> </section> <script src="js/jquery-1.11.1.min.js"></script> <script src="js/jquery.validate.min.js"></script> <script src="js/plugins.js?cb=2.2.3.2085"></script> </body> </html> Edited June 16, 2015 by gmc1103 Quote Link to comment https://forums.phpfreaks.com/topic/296845-div-from-function/#findComment-1514053 Share on other sites More sharing options...
Ch0cu3r Posted June 16, 2015 Share Posted June 16, 2015 Is this code in newpass.php? It appears this code is from a different file. The undefined variable error is coming from newpass.php on line 81. Quote Link to comment https://forums.phpfreaks.com/topic/296845-div-from-function/#findComment-1514060 Share on other sites More sharing options...
gmc1103 Posted June 16, 2015 Author Share Posted June 16, 2015 Yes,,i know but why? if the function returns true i have a result and that result should be in that div. But when the page loads...i have this error at the beginning because it expects that variable. So what's wrong?? Quote Link to comment https://forums.phpfreaks.com/topic/296845-div-from-function/#findComment-1514063 Share on other sites More sharing options...
Ch0cu3r Posted June 16, 2015 Share Posted June 16, 2015 So $result is used in a different page from where it is being set? If that the case then variables are not remembered between pages. The easiest option is to add the result to a session. You can then get the value from the session variable on the other page. In both pages make sure you are calling session_start() at the very top. When setting the result use if (isset($_POST['submitted'])) { if($fgmembersite->ChangePassword()){ $_SESSION['result'] = '<div class="alert alert-success">Password alterada com sucesso</div>'; sleep(2); $fgmembersite->RedirectToURL("index.php"); } else{ $_SESSION['result'] = '<div class="alert alert-danger">A mudança de password não foi efetuadada....tente mais tarde</div>'; } } Then in your other page use <div class="form-group"> <?php if(isset($_SESSION['result'])) echo $_SESSION['result']; // get result from session ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/296845-div-from-function/#findComment-1514068 Share on other sites More sharing options...
gmc1103 Posted June 16, 2015 Author Share Posted June 16, 2015 Hi Thank you The problem is they are not different page The newpass.php has thos code <?PHP error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); require_once("./include/membersite_config.php"); if (!$fgmembersite->CheckLogin()) { $fgmembersite->RedirectToURL("index.php"); exit; } if (isset($_POST['submitted'])) { if($fgmembersite->ChangePassword()){ $result = '<div class="alert alert-success">Password alterada com sucesso</div>'; sleep(2); $fgmembersite->RedirectToURL("index.php"); } else{ $result = '<div class="alert alert-danger">A mudança de password não foi efetuadada....tente mais tarde</div>'; } } $email = $fgmembersite->UserEmail(); ?> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>EBSPMA Atividades</title> <link href="css/styles.css" rel="stylesheet"> <link href="css/bootstrap.min.css" rel="stylesheet"> <script src="js/modernizr.js?cb=2.2.3.2085"></script> <script type="text/javascript"> </script> </head> <body> <header role="banner" class="main-header"> <!-- MAIN HEADER --> <div class="main-header-body"> <div class="container-wrapper"> <div class="container-fluid"> <div class="row"> <div class="col-xs-3"> <h2>EBSPMA PAAD</h2> </div> <div class="col-xs-4 col-xs-offset-1 main-header-title"> <p class="sizeToggle" >Mudar Password</p> </div> </div> </div> </div> </div> </header><section class="content"> <div class="grid"> <div class="box"> <form name="changepwd" id="loginForm" action='<?php echo $fgmembersite->GetSelfScript(); ?>' method="POST"> <input type="hidden" name='submitted' id='submitted'/> <input type="hidden" name="path" value="painelAdquirente.action"/> <div> <p><b><font color="red">A sua password deve ser alterada!</font></b></p></div> <div class="icon-input"> <label for="password"></label> <div class="input password"> <i class="fa fa-lock"></i> <input type="password" name="oldpwd" id="oldpwd" placeholder="Senha antiga"> </div> </div> <br> <div class="icon-input"> <label for="password"></label> <div class="input password"> <i class="fa fa-lock"></i> <input type="password" name="newpwd" id="newpwd" placeholder="Senha nova"> </div> <input type="hidden" name="email" id="email" value="<?php echo $email; ?>"> </div> <br> <input type="submit" id="sbmtLogin" class="sa-btn1" value="Mudar"> </form> </div> <div class="form-group"> <?php echo $result; ?> </div> </div> </section> <script src="js/jquery-1.11.1.min.js"></script> <script src="js/jquery.validate.min.js"></script> <script src="js/plugins.js?cb=2.2.3.2085"></script> </body> </html> When i press the button it calls that function changePassword() and i have a return (true or false), since my return is always true it should work but it doesn't If i use this code... if (isset($_POST['submitted'])) { echo '<div class="alert alert-success">Password alterada com sucesso</div>'; sleep(2); if($fgmembersite->ChangePassword()){ $fgmembersite->RedirectToURL("index.php"); } echo '<div class="alert alert-danger">A mudança de password não foi efetuadada....tente mais tarde</div>'; sleep(2); } i get in my debugger...the echo "Password alterada com sucesso" but the div is not so the user can't see if the password has been changed. Quote Link to comment https://forums.phpfreaks.com/topic/296845-div-from-function/#findComment-1514075 Share on other sites More sharing options...
Muddy_Funster Posted June 16, 2015 Share Posted June 16, 2015 are you absolutely sure $_POST['submitted'] is set? Quote Link to comment https://forums.phpfreaks.com/topic/296845-div-from-function/#findComment-1514076 Share on other sites More sharing options...
gmc1103 Posted June 16, 2015 Author Share Posted June 16, 2015 Yes because teh password is changed and like i told..i have this in my debugger <div class="alert alert-success">Verifique o seu mail....Obrigado</div> So it enters in the changePassword() function and the return is true Quote Link to comment https://forums.phpfreaks.com/topic/296845-div-from-function/#findComment-1514078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.