I have been searching Google and ACF for a possible solution but cannot find anything that works completely.
Basically, I have set up an ACF repeater named coupon_directory which includes subfields named coupon_number and expiry_date.
I want to create a basic form that will take the value of a text field and validate whether it exists as a coupon_number within the coupon_directory repeater.
I would also like to check if the expiry_date associated with the coupon_number is before the current date (ie. the date the form is submitted).
If the text input matches a coupon_number and is entered before the expiry_date then I want to print a ‘success’ message.
If the coupon_number doesn’t exist in the repeater or the expiry_date has passed, I would like to print a ‘failed’ message.
I have gotten this (kinda) working via the below PHP code, however would really like to know how I can achieve this via AJAX in order to stop the page reload and remove the search term from appearing in my URL as ?result=SAMPLEINPUT
<form class="coupon-form" action="" method="GET">
<label>Enter Number</label>
<input type="text" name="result" id="result" value="">
<?php if (isset($_GET['result'])) : $result = $_GET['result'];
while (have_rows('coupon_directory')) : the_row();
$coupon = get_sub_field('coupon_number');
$expiry = get_sub_field('expiry_date');
if ($result == $coupon && $expiry < date('F j, y')) :
$found = true;
elseif ($result == $coupon && $expiry > date('F j, y')) :
$found = false;
elseif ($result !== $coupon) :
$found = false;
endif;
endwhile;
if ($found) : echo 'Success'; elseif (!$found) : echo 'Failed'; endif;
endif; ?>
</form>
If anyone can help lead me in the right direction or map out how I can validate this input again ACF fields via AJAX that would be greatly appreciated!
* Apologies in advance if this is duplicated or posted incorrectly!