We help IT Professionals succeed at work.
Get Started

Prevent PHP form submit depending on Ajax form response

Richard Lloyd
Last Modified: 2017-11-03
I have a PHP form that is used for entering a membership number.

When the input field changes, an ajax call is fired to a. check that the card number is from a valid range of possible card numbers and b. check if the card has already been issued.

If both checks are satisfied then no error message are displayed and the user can submit the form.

If either check fails, a warning message is displayed (either card not valid, or card already issued), but the form can still be submitted.

I also use https://jqueryvalidation.org/ to validate the format of the field and other in the form.

I am looking for a way of making the submit button "disabled"  unless the 2 checks on the field are satisfied. This could be either via the https://jqueryvalidation.org/ or otherwise.

The relevant parts of the code are shown below

Thank you.

<form action'#' method='post' id='account-add'>";
Card No<br/>
<input type='text' name='cardno' id='cardno'/><div id='disp'></div>
<input type='hidden' name='postback' value='add'/>
<input type='submit' id='submit' value='Add' />
<!--jquery validation-->
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js?v=1"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.0/additional-methods.min.js?v=1"></script>
$().ready(function() {
					$.validator.addMethod("cardno", function(value, element) {
							return this.optional(element) || /^xyz[0|1|9]\d{5}X$|^LYBLA[2|3|4|9]\d{6}$|^TMP\d{9}$/i.test(value);
					}, "Card Number is invalid. It should be xyz, 6 digits ending with X, or 7 digits");
		rules: {
			cardno: {cardno:true,
		messages: {
			cardno: "Card no must be in the format xyz followed by 6 digits ending in X, or 7 digits.",


<!--Ajax call-->
<script type="text/javascript">
$("#cardno").change(function() {
var cardno = $('#cardno').val();
type: "POST",
url: "match-cardno.php",
data: "cardno="+ cardno + "&companyid=xyz",
success: function(html){
return false;

Open in new window

<?php session_start();

include 'config.php';


//check card exists in list of possible cards
$sql="select cardno from tblcardpossibles where cardno =? and companyid=?";
$params=array($cardno, $companyid);
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$result=sqlsrv_query($conn, $sql, $params, $options);
include "error.php";

$row_count = sqlsrv_num_rows($result);
echo "<p class='error'>You have entered an invalid card number. Please check your entry. </p>";}

//Check if card has already been issued
$sql="select title, firstname, surname, postcode from tblcardsissued where cardno =? and companyid=?";
$params=array($cardno, $companyid);
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$result=sqlsrv_query($conn, $sql, $params, $options);
include "error.php";

$row_count = sqlsrv_num_rows($result);
$row= sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC);

echo "<p class='error'>Card already issued to $title $firstname $surname, $postcode</p>";}


Open in new window

Watch Question
Most Valuable Expert 2013
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE