2

I have a safecracker form which I am using to register contacts for a contact manager. In an effort to eliminate duplications I would like the safecracker form to query the database and check for duplicate email or names in the contact entries.

I tried to install Ajax form Validator Plugin but the plugin does not seem to work and the developer is not responding in the help section.

How can I use Jquery/Ajax to perform an inline check before submitting the form?

Below is the script for the faulty Ajax Form Validator:

{exp:ajax_form_validator:script entry_id="none" form_id="comment_form" submit_button_id="submit_button" async_url="http://www.elinex.co.za/contact-manager/ajax_form_validator_async/" parse="inward"}

{ajax_form_validator field="name" required="yes" deny_duplicate_data_field="exp_members.screen_name|exp_members.username" duplicate_data_exception_field="exp_members.email" duplicate_data_exception_contained_in="email" field_required_alert_text="ERROR!\nName field cannot be empty!" invalid_indicator_id="name_field" invalid_class="invalidated" javascript_alert="yes" min_length="2" max_length="30" regex="/^[a-zA-Z0-9]+([\.\_\-][a-zA-Z0-9]+)*$/" min_length_alert_text="ERROR!\nThe name cannot consist of one letter." max_length_alert_text="ERROR!\nThe name cannot be longer than 30 characters." duplicate_data_alert_text="ERROR!\nThe name you entered is already in use by a registered member." regex_alert_text="ERROR!\nThe name you entered contains illegal characters or character combinations."}

{ajax_form_validator field="email" required="yes" deny_duplicate_data_field="exp_members.email" duplicate_data_exception_field="exp_members.screen_name|exp_members.username" duplicate_data_exception_contained_in="name" field_required_alert_text="ERROR!\nEmail field cannot be empty!" invalid_indicator_id="email_field" invalid_class="invalidated" javascript_alert="yes" max_length="50" max_length_alert_text="ERROR!\nThe email address cannot be longer than 50 characters." regex_alert_text="ERROR!\nInvalid email address." duplicate_data_alert_text="ERROR!\nThe email you entered is already in use by a registered member." regex="/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]:LD:2,6:RD:$/i"}

{/exp:ajax_form_validator:script}

{exp:safecracker channel="member_profiles" url_title="{segment_3}" dynamic_title="[member_first_name] [member_last_name]" return="contact-manager/added/ENTRY_ID"}

<label>Name</label>
<input type="text" name="name" value="" class="text"/>

<label>Email Address</label>
<input type="text" name="email" value="" class="text"/>

<input type="submit" name="submit" id="submit_button" style="margin-left:0px;" value="Add Contact"/>

{/exp:safecracker}
1

To answer this question, ignoring the AJAX Form Validator, which I'm unfamiliar with.

How can I use Jquery/Ajax to perform an inline check before submitting the form?

You can create a template that loads a segment variable, such as the email address, into an exp:query tag/SQL statement, and check for the address that way. You can then return a 1 or a 0.

If the URL is /email-check/someone@somewhere.com

{exp:query sql="Select * from exp_channel_data where field_id 2 = '{segment_2}'}
{if total_results > 0}1{if:else}0{/if}
{/exp:query}

Now you can just use your own jQuery/AJAX code to pass the email address to that template's URL, and validate the form based on the 1/0 response.

  • Thanks, I've been trying to implement the email check url but a cannot get a reponse (1/0) when using your suggested code for the sql query. Just shows a blank page. – Thomas K Feb 7 '13 at 19:29
  • {exp:query sql="SELECT * FROM exp_channel_data WHERE field_id_24 = {segment_3}} {if total_results > 0}1{if:else}0{/if} {/exp:query} – Thomas K Feb 7 '13 at 19:31
  • If segment_3 is not an integer, you need to wrap it in single quotes so MySQL compares a string. Check that first? Have you enabled the Query module under your Add-ons? – Mark J. Reeves Feb 7 '13 at 19:34
  • Thanks again for your assistance on this. I got it to work correctly by returning either a 1 or 0. Only problem I am having difficulty with is Remote JQuery Validation, I am not sure how to send the input data to the URL and receive a response. Currently the form does not submit and there are no errors in the console. – Thomas K Feb 8 '13 at 8:45
  • $("#form").validate({ rules: { member_first_name: { required: true}, member_email_address: { required: true, email: true, remote:'http://localhost/elinex/index.php/contact-manager/email-check/' + $('#member_email_address').val() } } }); – Thomas K Feb 8 '13 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.