Error Message Handling in Dolibarr Module Development

Dear valued Dolibarr support team,

I’m programming a custom module for warehouses to add some enhancements related to customer balances from the stock. Things are going well, and I’ve been following your documentation.

However, I’ve encountered an issue with validating input values before sending them to the database. When I use the setEventMessages function, the error message is displayed, but I’m redirected back to the following page:

http://localhost/jotex/custom/reyadastocks/card.php

and the page i work on

http://localhost/jotex/custom/reyadastocks/card.php?action=addreturn&idmenu=80&mainmenu=reyadastocks&leftmenu=

After the system detects an error, the error message appears, but it redirects me back to the same page. In this case, a blank page is displayed. How can I display the error message while keeping the user on the same page, similar to adding a user or a product?

I hope this clarifies the situation.

this is th code

if ($action == 'addreturns' && $request_method === 'POST') {
    global $dolibarr_main_data_root;

    // debut
    $date = explode('/', GETPOST('issuedate'));
    $issuedate = $date[2] . '-' . $date[1] . '-' . $date[0]; // تاريخ بالشكل الصحيح
    $current_time = date('H:i:s'); // الوقت الحالي بالشكل الصحيح
    $issuedatatime = $issuedate . ' ' . $current_time;
    $description = addslashes(GETPOST('description'));
    $issuedate = $issuedate;
    $receiptcode = GETPOST('receiptcode');
    $targetWareHouse = GETPOST('targetWareHouse');
    $itemName = GETPOST('itemName');
  //  $boxCount = GETPOST('boxCount');
    $qINkg = GETPOST('qINkg');
   // $lotNumber = GETPOST('lotNumber');
    $sCustomer = GETPOST('sCustomer');
    $inventorycode = GETPOST('inventorycode');
    $moveLable = GETPOST('moveLable');
    //$moveLable = real_escape_string($lable);
    $type_mouvement = 1; // 0 For insert - 1 for decrease
	$paper_code = GETPOST('paper_code');
	$credit = GETPOST('credit');
    $user222 = $user->id;
    // Data will be inserted into reyadastocks table
    $insert = array(
        'fk_user' => $user->id,
        'issuedate' => $issuedate,
        'receiptcode' => $receiptcode,
		'paper_code' => $paper_code,
        'itemName' => $itemName,
        'targetWareHouse' => $targetWareHouse,
       // 'boxCount' => $boxCount,
        'qINkg' => $qINkg,
       // 'lotNumber' => $lotNumber,
        'sCustomer' => $sCustomer,
        'description' => $description,
        'entity' => $conf->entity,
    );
if ($credit > 0){
	// Step 1 - Inserting new data to table reyadastocks_returns
	$sql1 = "INSERT INTO " . MAIN_DB_PREFIX . "reyadastocks_returns (fk_user, issuedate, receiptcode, paper_code, targetWareHouse, itemName, qINkg, sCustomer, description, entity)";
	$sql1 .= "VALUES ($user222, '$issuedate', $receiptcode, $paper_code, $targetWareHouse, $itemName, $qINkg, $sCustomer, '$description', $conf->entity)";

	$step1 = $reyadastocks->db->query($sql1);

	if ($step1) {
		$newReturnID =  $reyadastocks->db->db->insert_id;

		// Step 2 - Register stock Movement
		if ($newReturnID > 0) {
			$negativeqINkg = -$qINkg;
			$sql2 = "INSERT INTO " . MAIN_DB_PREFIX . "stock_mouvement (datem, fk_product, fk_entrepot, value, type_mouvement, fk_user_author, label, inventorycode, fk_soc)";
			$sql2 .= "VALUES ('$issuedate', $itemName, $targetWareHouse, $negativeqINkg, $type_mouvement, $user222, '$moveLable', $inventorycode, $sCustomer)";
			$step2 = $reyadastocks->db->query($sql2);

			if (!$step2) {
				echo 'Error In step2' . $reyadastocks->db->db->error;
				exit;
			}

			// Step 3 - Update total stock in product_stock table
			$reelValue = $reyadastocks->getReelValue($itemName, $targetWareHouse);

			if ($reelValue > 0) {
				$newreelValue = $reelValue - $qINkg;
				$step3 = $reyadastocks->UpdateReelValue($itemName, $targetWareHouse, $newreelValue);

				if ($step3) {
					header('Location: ./card.php?action=viewReturnreceipt&id=' . $newReturnID);
				} else {
					echo $reyadastocks->db->error();
				}

}}}
else {
	echo $reyadastocks->db->error();
}
}
else {
	setEventMessages($langs->trans('NoEnoughCredit'), null, 'warnings');
}
}