PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 9, 2022

[FIXED] How to check if ajax data has some specific value

 September 09, 2022     ajax, jquery     No comments   

Issue

I need to check my data.accesslocations contains keyword "Test". Are there any built in function to use?

$(document).on('click', '.update_data', function(){ 
               var row_id = $(this).attr("id");  
               
               $.ajax({  
                    url:"./project/userdetail.php",  
                    method:"POST",  
                    data:{row_id:row_id},  
                    dataType:"json", 
                    success:function(data){  
                         
                        if(data.accesslocations.contains("test")){
                                //do something but this doesn't seem to work ...
                           }
         
    
                    
                        
                         $('#employee_id_update').val(data.row_id);  
                         $('#insert').val("Update");
                         $('#new_data_Modal').modal('show'); 
                        
                            
                          
                    }, 
                    error: function(req, status, error) {
                   alert(req.responseText);      
                    }   
               });  
      }); 

data.accesslocations contains some strings such as

ABC
ABC,DEF
ABC,DEF,GHI 

I need to check that data.accesslocations contains those strings and do something.


Solution

There at least 2 solutions:

$.ajax({
   ...
    success:function(data) {
        var accesslocations = data.accesslocations;

        // using Regex (case insensitive, only words)

        if (accesslocations.match(/\bTest\b/i)) {
            processResult(accesslocations);
        }
        
        // or using array prototype includes

        var words = accesslocations.split(' ');
        if (words.includes("Test")) {
            processResult(accesslocations);
        }
    }
});


Answered By - Richard DobroĊˆ
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing