Jump to content

print value in an array with keys error?


cougar23

Recommended Posts

I have an array of error messages with keys defined, and I want to output the message associated with a key to the screen, but my code is outputting the key.

 

My array is like this:

$studentErrorMessages = array(

	'LATE_NEXT_DAY_ERROR' => 'You can not reserve an appointment for the following day after 3:00 PM. Please select another appointment',

	'APPT_NOW_RESERVED_ERROR' => 'The requested appointment is no longer available. Please select a different appointment',

	'CANCEL_NOT_ALLOWED' => 'You can not cancel your appointment after 3:00 PM the day before your appointment. Please email your advisor if you are unable to attend your scheduled appointment'
);

 

And then my code to output the error message is this:

				//if key was found in the error messages array, echo javascript to show the message in RED error message color
			if(array_key_exists($messageKey, $errorMsgDefinitionsArray)){
				echo '<script type="text/javascript">';
					echo ' document.getElementById("statusMsgDiv").style.display = "block"; ';
					echo ' document.getElementById("statusMsgOutput").classname = "red bold"; ';
					echo ' document.getElementById("statusMsgOutput").value = "'.$errorMsgDefinitionsArray[$messageKey].'"; ';
				echo '</script>';
			}

 

So testing, I have set the $messageKey = 'LATE_NEXT_DAY_ERROR'  and instead of displaying the error description, it's displaying 'LATE_NEXT_DAY_ERROR' .  What am I doing wrong?  Thanks for the help ahead of time.

Link to comment
https://forums.phpfreaks.com/topic/130523-print-value-in-an-array-with-keys-error/
Share on other sites

in a definitions.inc file that I include at the top of the page.  Right now debugging, the page looks like this:

 

<!-- import common php servier-side functions library -->
<?php require_once('../includes/common_fns.inc'); ?>
<!-- import directory defintions file -->
<?php require_once('definitions.inc'); ?>

<!-- DIV TO SHOW STATUS MESSAGE, IF REQUIRED AFTER PAGE RENDERS -->
			<div id="statusMsgDiv">
				<label id="statusMsgOutput"></label>
			</div>
<?php
//show status message if necessary after page has rendered; HARD-CODED TEST FOR NOW
				$_SESSION['statusMsg'] = 'LATE_NEXT_DAY_ERROR';
				displayStatusMessageIfRequired($studentErrorMessages, null);
?>

 

and then the function code in common_fns.inc is:

 

				//if key was found in the error messages array, echo javascript to show the message in RED error message color
			if(array_key_exists($messageKey, $errorMsgDefinitionsArray)){
				echo '<script type="text/javascript">';
					echo ' document.getElementById("statusMsgDiv").style.display = "block"; ';
					echo ' document.getElementById("statusMsgOutput").classname = "red bold"; ';
					echo ' document.getElementById("statusMsgOutput").value = "'.$errorMsgDefinitionsArray[$messageKey].'"; ';
				echo '</script>';
			}

 

 

label doesn't have a value property instead use innerText property

 

instead of this

 

echo ' document.getElementById("statusMsgOutput").value = "'.$errorMsgDefinitionsArray[$messageKey].'";

 

use

 

echo ' document.getElementById("statusMsgOutput").innerText = "'.$errorMsgDefinitionsArray[$messageKey].'";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.