1

Changes the error messages from remotely.Please help me.

Here is the sample code in jquery.

<script id="demo" type="text/javascript">
$(document).ready(function() {
    // validate signup form on keyup and submit
    var validator = $("#signupform").validate({
        rules: {

            mobile:
            {
                required: true,         
                minlength: 10,
                maxlength: 12,
                remote:"mobile.php"

            }   
        },
        messages: {

            mobile: {   
                minlength: "Mobile No. must be 10 digits",
                maxlength:  "Mobile No. must be 10 digits",
                required: " Please enter Mobile Number",
                remote: "Mobile No. already exists"         
            }
        },
        // the errorPlacement has to take the table layout into account
        errorPlacement: function(error, element) {
            error.prependTo( element.parent().next() );
        },
        // specifying a submitHandler prevents the default submit, good for the demo

        // set this class to error-labels to indicate valid fields
        success: function(label) {
            // set &nbsp; as text for IE
            label.html("&nbsp;").addClass("checked");
        }
    }); 

});
</script>

And remote php

<?php
   include("dbconnect.php");  
   $mobile=$_REQUEST["mobile"];
   $sql = "SELECT * FROM user_info where contact_no='$mobile'";
   $result = mysql_query($sql,$conn);  

    $string = $mobile;
if(mysql_num_rows($result))
{ 
   echo 'false';
}  
else if(($string[0] == 0)||($string[0] == 1)||($string[0] == 2)||($string[0] == 3)||($string[0] == 4)||($string[0] == 5)||($string[0] == 6))
{
    echo 'false';
}
else
{
  echo 'true';          
}
?>

In this php we are send only the true and flase.But i need the error messages. Remote error message pass form php script to the jquery.

Sparky
  • 98,165
  • 25
  • 199
  • 285
  • I'm not sure what your problem is, can't you just echo the error message in question instead of 'true' and 'false'? Also: You are in for some serious sql injections here: contact_no='$mobile' – colburton Jun 13 '14 at 07:26
  • Please give me the example @colburton – vamsikrishnareddy Jun 13 '14 at 08:05
  • if(mysql_num_rows($result)) { echo 'This phone number is already registered'; } – colburton Jun 13 '14 at 08:08
  • If you are trying to show sql errors, use echo mysql_error(); – colburton Jun 13 '14 at 08:09
  • By directly echo the error message the message moved to jquery. if(mysql_num_rows($result)) { echo 'This phone number is already registered'; }. changes this message directly right---remote: "Mobile No. already exists" – vamsikrishnareddy Jun 13 '14 at 08:22
  • OK, now I understand your problem. I don't think you can send the error message from php in that case. You need to change it in your js code to something else. – colburton Jun 13 '14 at 08:30
  • Can you please give me the example how to do this one @colburton – vamsikrishnareddy Jun 13 '14 at 10:11
  • You just change the line with remote: "Mobile No. already exists" to the message you like to be displayed. – colburton Jun 13 '14 at 10:12
  • if(mysql_num_rows($result)) { echo 'message 1'; } else if(($string[0] == 0)||($string[0] == 1)||($string[0] == 2)||($string[0] == 3)||($string[0] == 4)||($string[0] == 5)||($string[0] == 6)) { echo 'message 2'; } inplace of Mobile No. already exists how to show Message1 and message 2 – vamsikrishnareddy Jun 13 '14 at 10:36
  • As I understand the validator plugin, you can't do that. It expects just a trueish or falsy answer from the remote check. – colburton Jun 13 '14 at 11:03
  • Is there any alternative solution for this @colburton – vamsikrishnareddy Jun 13 '14 at 12:15
  • OP, to get custom error messages for `remote` method, simply `echo` a JSON string from the server-side code and that will become your error message. – Sparky Jun 14 '14 at 00:05
  • @colburton, Please read the documentation before giving advice: _"The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message; or a string, eg. 'That name is already taken, try peter123 instead' to display as the error message."_ – Sparky Jun 14 '14 at 00:22

0 Answers0