1

Here i am validating the IP address entered in a textbox through PHP.

 $('#check_ip').click(function() {
 var iptext = $('#Ip_Txt').val();   
 $.ajax({
     type : "POST",
     url : "mypage.php",
     data : { iptext : iptext , testconn : "yes" },
     success : function(data) {                     
     }      
 });
 });

And my PHP

if(isset($_REQUEST['testconn'])){
if ( $_SERVER['REQUEST_METHOD'] == "POST" )
{
    $input = $_POST['iptext '];
    if (preg_match( "/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $input))
    {
        echo "Valid IP";
    }
    else
    {
        echo "Invalid IP";
    }
}   
}

Everything is working fine. But i need to display echo Valid IP or Invalid IP in javascript alert after clicking the check_ip button.

6 Answers6

0

You can catch what ever response in sent from the server after completion of the request sent via ajax in success property. So inside your ajax success add this

success : function(data) { 
   alert(data);                    
}

Inside alert you can give what ever text you want. If you want to check what message needs to be displayed then do necessary checking's inside the success and then give the necessary messages

success : function(data) { 
   if(condition)
       alert('Some text');     
   else
       alert('Some other text');                   
}
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
  • I tried this. It is working... But i need a alert which display either valid or invalid only... i dont need complete data – user3651330 May 21 '14 at 06:52
0
success: function(data) {
    alert(data);
}
  • alert() function takes string as arguements. data received on success callback is an object. your alert would display Object object or something like that – PPrasai May 21 '14 at 07:02
0

The data is handling the echo from the backend so:

 $('#check_ip').click(function() {
     var iptext = $('#Ip_Txt').val();   
     $.ajax({
         type : "POST",
         url : "mypage.php",
         data : { iptext : iptext , testconn : "yes" },
         success : function(data) { 
             alert(data);           
         }      
     });
     });
kimbarcelona
  • 1,136
  • 2
  • 8
  • 19
0
$.ajax({
         type : "POST",
         url : "mypage.php",
         data : { iptext : iptext , testconn : "yes" },
         success : function(data) { 
             alert(data);           
         }      
     });

Just use alert method, pass the parameter into alert box

Rohit Batham
  • 1,238
  • 1
  • 9
  • 13
0
success : function(data) { 
   var response_string = JSON.stringify( data );
   // do your substr operations here
   alert( your_substring );                    
}

JSON.stringify converts your response to string. you can then perform any string operations you like on it.

Happy coding :)

PPrasai
  • 1,186
  • 13
  • 36
0

No need to reinvent the wheel. You can use filter_var() on this one. Consider this example:

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $input_ip = $_POST['ip'];
    echo filter_var($input_ip, FILTER_VALIDATE_IP) ? 'Valid IP' : 'Not a valid IP';
    exit;
}

?>

<input type="text" id="ip_address" />
<button id="validate" type="button">Validate</button>

<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#validate').click(function(){
        var ip = $('#ip_address').val();
        $.ajax({
            url: 'index.php', // just same page sample
            type: 'POST',
            data: {ip: ip},
            success: function(response) {
                alert(response);
            }
        });
    });
});
</script>
user1978142
  • 7,946
  • 3
  • 17
  • 20