Cotonti / Open Source PHP Content Management FrameworkContent Management Framework

Форуми / Craftwork / Server-side / Error handling with Ajax and PHP

Trustmaster
#37795 17.08.2013 18:00

Here is an example trick that I use:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
	$rpost = post_import();
	if (post_validate($rpost, $_POST['name']))
	{
		post_send($rpost);
		$response = array(
			'status'  => true,
			'message' => $L['post_message_sent']
		);
	}
	else
	{
		// Return error messages
		$error_fields = array();
		$error_messages = array();
		foreach ($_SESSION['cot_messages'][$sys['site_id']] as $src => $grp)
		{
			$error_fields[] = $src;
			foreach ($grp as $msg)
			{
				$error_messages[] = isset($L[$msg['text']]) ? $L[$msg['text']] : $msg['text'];
			}
		}
		$response = array(
			'status'         => false,
			'error_fields'   => $error_fields,
			'error_messages' => $error_messages
		);
		cot_clear_messages();
	}

	cot_sendheaders('application/json', '200 OK');

	echo json_encode($response);
}

 

May the Source be with you!