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!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.