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.

1

6 Answers 6

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>
0
0
success: function(data) {
    alert(data);
}
1
  • 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
    Commented May 21, 2014 at 7: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);           
         }      
     });
     });
2
  • This code is just the part of my page.. alert(data) will give me complete alert of my page... I need only Valid IP or Invalid IP. Thats it.. Commented May 21, 2014 at 6:53
  • Do necessary checking and show the appropriate message in the alert Commented May 21, 2014 at 6:58
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

2
  • Thanks.. But how do i pass the parameter? Commented May 21, 2014 at 6:56
  • Whatever will be print in php file as it is will show in the alert box. Example : According to your script result can be 'Valid IP' or 'Invalid IP'
    – user5296864
    Commented May 21, 2014 at 7:02
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');                   
}
1
  • I tried this. It is working... But i need a alert which display either valid or invalid only... i dont need complete data Commented May 21, 2014 at 6:52
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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.