cotonti.com : Error handling with Ajax and PHP https://www.cotonti.com Son konu mesajları Cotonti en Mon, 01 Dec 2025 08:11:23 -0000 Twiebie OK, got it.

Thanks!

]]>
Paz, 18 Ağu 2013 10:05:44 -0000
Trustmaster Yes, I check the status and error messages in success function.

]]>
Paz, 18 Ağu 2013 06:25:08 -0000
Twiebie Thanks, Trustmaster! Looks like I sort of was on the right track with my JSON response effort.

I'm guessing you are using this with success: function() {}, as you return 200 regardless?

]]>
Paz, 18 Ağu 2013 00:33:07 -0000
Trustmaster 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);
}

 

]]>
Cmt, 17 Ağu 2013 18:00:59 -0000
Twiebie I'm wondering what in Cotonti the best way is to handle errors with an Ajax submitted form to a PHP file.

Normally with a simple form the errors can be handled with Cotonti's cot_error(); function like this:

cot_error('someErrorMessage', '');
cot_redirect(cot_url('myPlug', 'm=form', '', true));

How would you guys handle something like this if the form is submitted with Ajax?

]]>
Cmt, 17 Ağu 2013 14:51:02 -0000