اذهب إلى المحتوى
  • 0

[php][jQuery] شرط مقارنه بين مدخلين لا يتم تنفيذه رغم صحته

محمد المصري12

السؤال

سلام عليكم

مشكلة هاتجنني صراحة

 

if (qINkg >= sCustomercredit){
								Swal.fire(
									<?php echo json_encode($langs->trans('error')); ?>,
									<?php echo json_encode($langs->trans('invalidReturn1')); ?>,
									'error'
								)

								event.preventDefault(); // Prevent form submission
								return false;
							}

							else {
								Swal.fire(
									<?php echo json_encode($langs->trans('successReturn')); ?>,
									'success'
								)
		                       return true;
							}

متغير qINKg ( الكمية بالكيلوجرام) لو كانت أكبر من رصيد العميل بالمخزن sCustomercredit المفروض يديني رسالة خطأ كالتالي

2023-10-19_15-32-07.thumb.jpg.0e4995fff0faea046f87c171d7c46934.jpg

لاحظوا بقا قيمة الكمية بالكيلوجرام ، هتلاقيها اصغر من رصيد العميل ، يعني المفروض يقبل المدخلات و رغم كده بيرفض

السكريت كاملا

 

<script type="text/javascript">
		jQuery(document).ready(function() {
			// Store the initial value of targetWareHouse
			let initialTargetWareHouseValue = $(".targetWareHouseField").val();

			// When the form is submitted
			$("form.card_reyadastocks").submit(function(event) {
				// Get the selected value of the "sCustomer" field
				let warehouse = $(".targetWareHouseField").val();
				let customer = $(".sCustomerField").val();
				let select_product = $(".select_product").val();

				let credit = $(".credit").val();

				let ItemInSelectedWareHouse = $(".ItemInSelectedWareHouse").val();
				let sCustomercredit = $(".sCustomercredit").val();
				let qINkg = $(".qINkg").val();

				console.log(qINkg);
				// console.log(customer);
				// console.log(select_product);

				if (select_product > 0) {
					// Check if the selected value is greater than zero (meaning a valid product has been selected)
					if (warehouse > 0) {
						// Check if the selected value for the customer is also greater than zero
						if (customer > 0) {

							if (credit <= 0){
								// Both fields are valid; the form will be submitted
								Swal.fire(
									<?php echo json_encode($langs->trans('error')); ?>,
									<?php echo json_encode($langs->trans('noEnoughItemeCredit')); ?>,
									'error'
								)

								event.preventDefault(); // Prevent form submission
								return false;
							}
							else if (ItemInSelectedWareHouse <= 0 ){
								Swal.fire(
									<?php echo json_encode($langs->trans('error')); ?>,
									<?php echo json_encode($langs->trans('noEnoughwareHouseCredit')); ?>,
									'error'
								)

								event.preventDefault(); // Prevent form submission
								return false;
							}
							else if(sCustomercredit <= 0){
								Swal.fire(
									<?php echo json_encode($langs->trans('error')); ?>,
									<?php echo json_encode($langs->trans('noEnoughCustomerCredit')); ?>,
									'error'
								)

								event.preventDefault(); // Prevent form submission
								return false;
							}

							else if (qINkg >= sCustomercredit){
								Swal.fire(
									<?php echo json_encode($langs->trans('error')); ?>,
									<?php echo json_encode($langs->trans('invalidReturn1')); ?>,
									'error'
								)

								event.preventDefault(); // Prevent form submission
								return false;
							}

							else {
								Swal.fire(
									<?php echo json_encode($langs->trans('successReturn')); ?>,
									'success'
								)
		                       return true;
							}

						} else {
							// Customer field is invalid, show an alert message
							alert("Please select a valid customer.");
							event.preventDefault(); // Prevent form submission
							return false;
						}
					} else {
						// Warehouse field is invalid, show an alert message
						alert("Please select a warehouse.");
						event.preventDefault(); // Prevent form submission
						return false;
					}
				} else {
					// Product field is invalid, show an alert message
					alert("Please select a product.");
					$(".targetWareHouseField").val(''); // Reset the #targetWareHouse field
					event.preventDefault(); // Prevent form submission
					return false;
				}


			});

			// Watch for changes in select_product
			$(".select_product").change(function() {
				// If select_product is changed, reset targetWareHouse if it was changed previously
				if ($(".targetWareHouseField").val() !== initialTargetWareHouseValue) {

					$(".targetWareHouseField").val('');
					$(".ItemInSelectedWareHouse").val('');
					$("#sCustomer").val('');
					$(".sCustomercredit").val('');

				}
			});

			// Watch for changes in WareHouse
			$(".targetWareHouseField").change(function() {
				// If targetWareHouse is changed, reset other fields if they were changed previously
				if ($(".targetWareHouseField").val() !== initialTargetWareHouseValue) {

					$("#sCustomer").val('');
					$(".sCustomercredit").val('');

				}
			});
		});

	</script>

و دي حقول المتغيرات في php 

 

	print '<tr>';
	print '<td class="fieldrequired">'.$langs->trans('sCustomer').' </td>';
	print '<td class="fieldrequired">';
	print $reyadastocks->select_customer(0, 'sCustomer', 1, 'rowid', 'nom','sCustomer');
	print '</td>';

	print '<td style="width: 200px" class="fieldrequired">'.$langs->trans('ItemInSelectedWareHouse').' </td>';
	print '<td > <input type="text" readonly value="" class="bg-noha3 sCustomercredit" name="sCustomercredit"></td>';
	print '</tr>';

	print '<tr>';
	print '<td class="fieldrequired">'.$langs->trans('qINkg').' </td>';
	print '<td colspan="3" > <input type="text" value="" class="qINkg minwidth300 bg-m1" name="qINkg" required="required" autocomplete="off"></td>';

	print '</tr>';

شكيت ان يكون فيه قيم متكرر تعريفها و دورت مفيش

قلت يمكن السكربت بيقرا الداتا غلط ، طبعت الداتا بالكونسول و نفس الشئ 

العجيب بقا ان قاعدة البيانات لو فاضية و كانت الاستمارة دي تمثل اول بيان مرتجع ، البيان بيتم تنفيذه عادي جدا
لكن الخطأ ده مع تاني مرتجع ، و مش فاهم ايه علاقة قاعدة البيانات لاني اصلا معرف البيانات في الفرونت اند مش جايبها من الداتابيز

اللي فاهم حاجة يفهمني 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

ربما  المشكلة تكمن في أنك تستخدم متغيرات محلية في شرط المقارنة، وتلك المتغيرات يتم حذفها بعد كل طلب من الصفحة، لذلك حتى لو كانت القيمة صحيحة في البداية، ستصبح غير صحيحة بعد ذلك.

حاول استخدام متغيرات عالمية، حيث أن تلك المتغيرات لا يتم حذفها بعد كل طلب، لذلك ستظل صحيحة حتى لو قمت بإعادة تحميل الصفحة.

كالتالي:

// تعريف متغيرات عالمية
$qINkg = $_POST['qINkg'];
$sCustomercredit = $_POST['sCustomercredit'];

// Check if the quantity is greater than the customer's credit
if ($qINkg >= $sCustomercredit) {
    // Show an error message
    echo "The quantity is greater than the customer's credit.";
    exit;
}

وبخصوص jQuery الكود سيكون كالتالي:

let qINkg = $(".qINkg").val();
let sCustomercredit = $(".sCustomercredit").val();

// Check if the quantity is greater than the customer's credit
if (qINkg >= sCustomercredit) {
    // Show an error message
    Swal.fire(
        <?php echo json_encode($langs->trans('error')); ?>,
        <?php echo json_encode($langs->trans('invalidReturn1')); ?>,
        'error'
    );
    event.preventDefault(); 
    return false;
}

أيضًا من الأفضل إضافة فحص إضافي للتأكد من أن المتغيرات ليست فارغة باستخدام الشرط التالي:

PHP


if (!empty($qINkg) && !empty($sCustomercredit)) {
    // Check if the quantity is greater than the customer's credit
    if ($qINkg >= $sCustomercredit) {
        // Show an error message
        echo "The quantity is greater than the customer's credit.";
        exit;
    }

}

JS:

if (qINkg && sCustomercredit) {
    // Check if the quantity is greater than the customer's credit
    if (qINkg >= sCustomercredit) {
        // Show an error message
        Swal.fire(
            <?php echo json_encode($langs->trans('error')); ?>,
            <?php echo json_encode($langs->trans('invalidReturn1')); ?>,
            'error'
        );
        event.preventDefault(); // Prevent form submission
        return false;
    }

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...