简体   繁体   中英

How to alert PHP echo in Javascript

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.

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');                   
}
success: function(data) {
    alert(data);
}

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);           
         }      
     });
     });
$.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

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 :)

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM