skip to Main Content

I have a problem I can’t understand. I did many test but the result just disturb me. I have an ajax request from where I get my data like that :

$check = 0;
$output = '';

while($data = mysqli_fetch_assoc($result1)){
    if($check == 0){
        $output = $data['license'];
        $check = 1;
    }else{
        $output .= ','.$data['license'];
    }
}
echo $output;

the output is “2020,2021” at the end. now I have my alert in js to test the result :

success: function(data) {
          alert(data); //get "2020,2021" from this alert
          var tab = data.split(',');
          alert(tab[0]+','+tab[1]); //get "2020,2021" from this alert
          if(tab[0]=='2020'){
            alert('yes1');
          }else{
            alert('no1');
          }
          if(tab[1]=='2021'){
            alert('yes2');
          }else{
            alert('no2');
          }
}

But now the problem is : the two other alerts I have are “yes1” and “no2” …. how can my 2020 equal to 2020 and 2021 not equal to 2021.
I just can’t understand, if someone can help.

Update : when I alert tab[1]+’,’+tab[0] I have :

“2021

,2020″

I don’t get when it is possible to append (my “SELECT * FROM my_table WHERE license=’2021′ ” get all the results, so it’s not in my database)

2

Answers


  1. As you can see if data is correct script work:

              var data='2020,2021';
              var tab = data.split(',');
              alert(tab[0]+','+tab[1]); //get "2020,2021" from this alert
              if(tab[0]=='2020'){
                alert('yes1');
              }else{
                alert('no1');
              }
              if(tab[1]=='2021'){
                alert('yes2');
              }else{
                alert('no2');
              }
    Login or Signup to reply.
  2. Try this (change success = to success : in your code

    success = function(data) {
      console.log(data); //get "2020,2021" from this alert
      const [tab0, tab1] = data.split(',');
      console.log(tab0 === '2020' ? 'yes0' : 'no0');
      console.log(tab1 === '2021' ? 'yes1' : 'no1');
    }
    
      
    // testing 
    success("2020,2021")
    success("2020, 2021")

    Then try this:

    success = function(data) {
      console.log(data); //get "2020,2021" from this alert
      const [tab0, tab1] = data.match(/d{4}/g);
      console.log(tab0 === '2020' ? 'yes0' : 'no0');
      console.log(tab1 === '2021' ? 'yes1' : 'no1');
    }
    
    // testing 
    success("2020,2021")
    success("2020, 2021")
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search