Jump to content

Recommended Posts

HI All,

 

I would like to ask a question about ajax and form validation

 

I am using some ajax code as follows ...

 

function showHint(str)

{

xmlHttp = GetXmlHttpObject();

 

if (xmlHttp == null)

  {

  alert ("Browser does not support Ajax HTTP Requests");

  return;

  }

 

// get ID of destination object

results = validation_fields.split("|");

destination_id = results[2];

 

if(str.length==0)

  {

    document.getElementById(destination_id).innerHTML="";

    return;

  }

 

// assemble URL string

var url = "../global_php/ajax_validation.php";

url = url + "?value=" + str.value + validation_fields;

url = url + "&sid=" + Math.random();

 

// The onreadystatechange property stores the function that will process the response from the server

// Each time the readyState changes, the onreadystatechange function will be executed.

xmlHttp.onreadystatechange = stateChanged;

xmlHttp.open("GET", url, true);

xmlHttp.send(null);

}

 

 

// FUNCTION: Checks server state and returns data to calling form

// PARAMETERS: NONE

// RETURNS: void (populates form object with returned data)

function stateChanged()

{

// The readyState property holds the status of the server's response

if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")

  {

    // capture result

var response = xmlHttp.responseText;

 

    // The data sent back from the server is assigned to submitted destination

    document.getElementById(destination_id).innerHTML = response;

  }

}

 

OK. The xmlHttp.responseText will show on the form whatever my php script echoes - in this case error

messages for incorrect input.

 

I need to be able to process those ajax generated messages client side in order to prevent  my form from

being submitted. At the moment the messages are displaying OK but the user can choose to ignore them

and submit erroneous data anyway.

 

How can ajax and javascriipt be used to actually enforce client side validation ?

I have thought about disabling the submit button but how ??

 

regards,

 

Steven M

 

Link to comment
https://forums.phpfreaks.com/topic/105940-ajax-form-validation/
Share on other sites

you're best off using validation in PHP on the server, in addition to or instead of client-side validation. anyone can turn off Javascript and bypass Javascript validation (and nuke ajax as well). If you don't also validate on the server, you leave yourself open to invalid submissions.

 

imo, ajax is good for data-intensive operations where there are many values to choose from and sending them all to the browser at once wouldn't work well. for instance, ajax is a good choice to provide a list of possible zip codes for a selected state. you don't want to list every single US zip code for the user to pick from, so you could use ajax to reduce the selection based on the user's state selection.

 

but, if you also want to validate client-side, add an onsubmit() handler to the form, returning true (will submit) or false (won't submit) from a javascript validation function.

 

http://www.w3schools.com/jsref/jsref_onsubmit.asp

 

Link to comment
https://forums.phpfreaks.com/topic/105940-ajax-form-validation/#findComment-542914
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

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

×   Your previous content has been restored.   Clear editor

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.