I have a panel.php in which I load my different pages by the include method. As there is a single panel, the message display system must be included here, and there will not always an error id to be displayed.
I have added the following code to my panel, calling function "envioMensaje" in case there is getting any variable 'msg':
<?php
if ($_GET) {
if ($_GET['msg']) {
$id_mensaje=$_GET['msg'];
$mensaje=envioMensaje($id_mensaje);
echo $mensaje;
}
}
?>
This works fine when a message id is sent, but when not I get a "Undefined index: msg in..." error. I have tried also:
if ($_GET) {
if ($_GET['msg']) {
$id_mensaje=$_GET['msg'];
$mensaje=envioMensaje($id_mensaje);
echo $mensaje;
} else {
echo"";
}
}
And some other variations with no result. Why is it always looking for 'msg'? Could it be because on that same document I have this other conditional asking for $_GET? (i need it for the content to be loaded):
if (!$_GET) {
include('config/shortcuts.php');
} else {
$directorio = $_GET['directory'];
include('config/'.$directorio);
}
Use isset() to check if a variable exists without throwing a PHP notice.
if (isset($_GET['msg'])) {
//then use $_GET['msg']
}
Check for
if(isset($_GET['msg'])){...}
Related
I want to show an error on the same as page as my form. My form is going to another page and then processing and returning the result. How can I show the error on the previous page?
if($_POST['btnManage']=="Signup"){
$objCustomer->Email=$_POST['txtEmail'];
$objCustomer->Password=$_POST['txtPassword'];
$Status=$objCustomer->Signup();
if($Status>0)
{
session_start();
$_SESSION['Email']=$objCustomer->Email;
header("Location:../index.php");
} else
{
echo "error";
}
}
Going with what you have using $_SESSION, you can set eorr in the session.
else
{
$_SESSION["eorr"] = "message_here";
header("Location:../{form_page_here}");
}
Then on the page that has your form, do a check if $_SESSION["eorr"] is set an display the message. You would also need to unset($_SESSION["eorr"]; after it is displayed so it is removed if you do not need to use it again.
Three step to handle the error
step1: define your error code and error message
define('SYSTEM_ERROR',1000);
define('SYSTEM_ERROR_MESSAGE','your error message');
step2: put the current error code to the reference url as a parameter
http://localhost?error=1000
step3: redirect to the reference page ,handle the error code and show to the page
$error = $_GET['error'];
if($error ==SYSTEM_ERROR){
$errorMessage = SYSTEM_ERROR_MESSAGE;
}
//show the $errorMessage
I have a form that user can update their details on page-1.php.
After user submit the form, it will got to page-2.php for validation and update database if user input is true.
May i know how to call a function from page-2.php and display the error message on page-1.php. Below are the function on page-2.php that I use to display the error message:
$_SESSION['err_msg'] = array();
function addError($msg) {
$_SESSION['err_msg'][] = $msg;
}
function printErrors() {
foreach ($_SESSION['err_msg'] as $err){
echo "<ul><li><span style='color: red;'>".$err."</span></li></ul>";
}
unset($_SESSION['err_msg']);
}
//other codes for the validation and update
if i use printErrors();on page-2.php, it will display the error message
You need to include page-2.php in page-1.php. Read this about that.
The best way is to put all your functions in one seperate file, like functions.php and include them on every page with an include.
I think no you don't need function on page-2.php. You can simply check session variable on page-1.php. If $_SESSION['err_msg'] exists and count is greater than 0. Then you can print all errors to page-1.php>
Please check below code.
if(isset($_SESSION['err_msg']) && count($_SESSION['err_msg'])>0 ){
foreach ($_SESSION['err_msg'] as $err){
echo "<ul><li><span style='color: red;'>".$err."</span></li> </ul>";
}
unset($_SESSION['err_msg']);
}
Hope this helps you.
Include the page-2.php in your page-1.php
I'd like to preface this by saying that I've read answers to similar questions, and haven't managed to find something that both does the job and does not slow the site down.
I have a site that displays messages based on users' choices and actions. The code might look something like this :
if (option 1) {
$message1 = "Message A";
}
else if (option 2){
$message1 = "Message B";
}
else {
$message1 = "Message C";
}
There are a hand full of these throughout the site. When i want to echo the messages somewhere within the html structure i have to write:
<?php
if (isset($message1)) {
echo $message1;
}
?>
I've written a simple function which does its job:
function message($msg){
if (isset($msg)) {
echo $msg;
}
}
The problem is that i get notices for undefined variables, which makes sense because the variable isn't defined before the user clicks a button. I would like to avoid turning off error reporting.
So, is adding # in front of the function the only way? The code would then look like:
<?php
#message($message1);
?>
If that is acceptable, then great. If not, I'm open to alternatives.
Based on your comment, your use-case seems to make sense. In that case I would have an array with all error messages, something like:
$messages = array();
...
$messages['registration-form']['error']['password-mismatch'] = 'Passwords do not match';
...
And when I validate the input and find mismatching passwords, I would do:
// at the top
$errors = array();
...
// passwords don't match
$errors['passwords-mismatch'] = $messages['registration-form']['error']['password-mismatch'];
And where I output the form below the passwords:
messages($errors, 'password-mismatch');
And finally the function would be something like:
function messages($errors, $error) {
if (isset($errors[$error])) {
// I would wrap it in a span to highlight the error
echo '<span class="error">' . $errors[$error] . '</span>';
}
}
You can pass the variable by reference, function message(&$msg){, no error will be raise.
From the documentation:
If you assign, pass, or return an undefined variable by reference, it will get created.
Try this php in built function:
error_reporting(0);
I am adapting a script from here, to use as verification when form is posted.
This is the code being sent form the form page:
<?php
$secret1 = '00001';
?>
And the code from the action page:
if(!empty(htmlentities($_POST['secret1'])) {
if(htmlentities($_POST['secret1']) == '00001') {
echo 'PASS';
}
}else { echo 'ERROR'; }
However, when I submit the form, I get "Can't use function return value in write context" error.
Am just starting to learn php so can't really identify the problem. Can you please help?
Thanks.
You are trying to pass a function return (htmlentities) to empty. Change your first line to:
if(!empty($_POST['secret1'])) {
I've got my login and session validity functions all set up and running.
What I would like to do is include this file at the beginning of every page and based on the output of this file it would either present the desired information or, if the user is not logged in simply show the login form (which is an include).
How would I go about doing this? I wouldn't mind using an IF statement to test the output of the include but I've no idea how to go about getting this input.
Currently the login/session functions return true or false based on what happens.
Thanks.
EDIT: This is some of the code used in my login/session check but I would like my main file to basically know if the included file (the code below) has returned true of false.
if ($req_method == "POST"){
$uName = mysql_real_escape_string($_POST['uName']);
$pWD = mysql_real_escape_string($_POST['pWD']);
if (login($uName, $pWD, $db) == true){
echo "true"; //Login Sucessful
return true;
} else {
echo "false";
return false;
}
} else {
if (session_check($db) == true){
return true;
} else {
return false;
}
}
You could mean
if (include 'session_check.php') { echo "yeah it included ok"; }
or
logincheck.php'
if (some condition) $session_check=true;
else $session_check=false;
someotherpage.php
include 'session_check.php';
if ($session_check) { echo "yes it's true"; }
OR you could be expecting logincheck.php to run and echo "true" in which case you're doing it wrong.
EDIT:
Yes it was the latter. You can't return something from an included file, it's procedure not a function. Do this instead and see above
if (session_check($db) == true){
$session_check=true;
} else {
$session_check=false;
}
Actually..
$session_check=session_check($db);
is enough
Depending on where you want to check this, you may need to declare global $session_check; or you could set a constant instead.
you could have an included file which sets a variable:
<?php
$allOk = true;
and check for it in you main file:
<?php
include "included.php";
if ($allOk) {
echo "go on";
} else {
echo "There's an issue";
}
Your question seems to display some confusion about how php includes work, so I'm going to explain them a little and I think that'll solve your problem.
When you include something in PHP, it is exactly like running the code on that page without an include, just like if you copied and pasted. So you can do this:
includeme.php
$hello = 'world';
main.php
include 'includeme.php';
print $hello;
and that will print 'world'.
Unlike other languages, there is also no restriction about where an include file is placed in PHP. So you can do this too:
if ($whatever = true) {
include 'includeme.php';
}
Now both of these are considered 'bad code'. The first because you are using the global scope to pass information around and the second because you are running globally scoped stuff in an include on purpose.
For 'good' code, all included files should be classes and you should create a new instance of that class and do stuff, but that is a different discussion.