"if and else" condition for sending Ajax, taking text from span tags?

1

It's kind of hard to explain by title, but it's the following, I have a foreach that prints in a <ul> tags list, and inside it I have the following code:

<?php if ($row['Status'] == 'Em Processo'):?>                                   
   <span class="process badge" style="background-color:#67A6DF">Em Processo</span>                                  
<?php endif; ?>
<?php if ($row['Status'] == 'Teste'): ?>
   <span class="test badge" style="background-color:#FCB529">Em Teste</span>
<?php endif;?>
<?php if ($row['Status'] == 'Aprovada'):?>
   <span class="approve badge" style="background-color:#43A995">Aprovada</span>                                                         
<?php endif;?>

The idea is if it has a defined status it prints what it is, but I have the following button:

<a href="javascript:void(0);" class="flex-icon approved" data-toggle="tooltip" data-placement="top" title="Aprovar" data="5"><i class="fa fa-check-square-o fa-2x" aria-hidden="true"></i></a>

This changes the definition of status, in this case clicking on it will change the status to Approved, then within the list it changes the <li> along with the list.

But the question is, what has not been tested yet, can not be approved, but I can not just get the class process of the first <span> and send in Ajax to not change.

Script:

$(document).ready(function() {
    $('.approved').on('click', function(){
        var process = $('.process').text();
            if(process != "Em Processo"){
                var hrefdata = $(this).attr("data");
                 var taskid = $(this).parent().parent().attr('id'); 
                    $.ajax({ 
                        url: './task.php', 
                        type: 'POST', 
                        data: { id: taskid, approved: hrefdata},
                        success: function(data) { 
                            window.location.reload();
                        }               
                    });//Ajax
            }else{
                alert('Está em processo não pode ser aprovado antes de enviar para teste');
            };//If
    });//Onclick
});

How do I get it to just read <span> with the process class and not put the same condition to the others?

Well with the response from @KirmayrTomaz I managed to solve my problem, but I had to make some changes to the code.

If I use:

    $(document).ready(function() {
    $('.approved').on('click', function(){
        var father = $(this).parent().parent();
        var taskid = $(father).attr('id');          
        var process = document.querySelectorAll("#" + taskid + " .process");    
            if($(process).text() != "Em Processo"){                 
                var hrefdata = $(this).attr("data");
                    $.ajax({ 
                        url: './task.php', 
                        type: 'POST', 
                        data: { id: taskid, approved: hrefdata},
                        success: function(data) { 
                            window.location.reload();
                        }               
                    });//Ajax
            }else{
                alert('Está em processo não pode ser aprovado antes de enviar para teste');
            };//If
    });//Onclick
});

I had to make the following change to the ID:

<li class="list-group-item id-task" id="<?php echo $row['TasksId'];?>"> ...

</li>

To:

<li class="list-group-item id-task" id="task<?php echo $row['TasksId'];?>"> ...

</li>

The following error is displayed:

Uncaught SyntaxError: Failed to execute querySelectorAll on Document: '# 1 .process' is not a valid selector.

To remove the word "task" from the ID I had to put the following code

$idtaskapproved = strip_tags($_POST['id']);    
$substrid = substr ($idtaskapproved, 4);

That takes the first 4 POST strings I'm getting.

Looks like it was ugly but it worked.

If anyone had any idea to improve this ugly thing.

    
asked by anonymous 31.08.2016 / 19:40

1 answer

2

Based on what you said to change the span. First define a parent that is both approved and process . In the example I made put the div with class = taskid it works as follows

  • When the user clicks .approved you will have to look for the parent of $(this).parent().parent();
  • Once you have the father, just look for the child inside the branches. document.querySelectorAll("#" + taskid + " .process")
  • Once you've got the child, you can only make the change in it.
  • $(document).ready(function() {
      $('.approved').on('click', function() {
        //pegando o pai certo
        var father = $(this).parent().parent();
        var taskid = $(father).attr('id');
        
        //pegando o processo certo 
        var process = document.querySelectorAll("#" + taskid + " .process")
          
        if ($(process).text() != "Em Processo") {
         
          var hrefdata = $(this).attr("data");
         //Após sucess da função ajax
              //alterando de processo para approve
              $(process).removeClass("process")
              $(process).addClass("approve").css({
                "background": "#43A995"
              }).text("Aprovada")
     
        } else {
          alert('Está em processo não pode ser aprovado antes de enviar para teste');
        }; //If
      }); //Onclick
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="taskid" id="atividade1">
      <div class="task">
        <span class="process badge" style="background-color:#FCB529F">Em Teste</span> 
    
        <a href="javascript:void(0);" class="flex-icon approved" data-toggle="tooltip" data-placement="top" title="Aprovar" data="5"><i class="fa fa-check-square-o fa-2x" aria-hidden="true">Aprovar</i></a>
      </div>
    
    </div>
    
    <div class="taskid" id="atividade2">
      <div class="task">
        <span class="process badge" style="background-color:#67A6DF">Em Processo</span> 
    
        <a href="javascript:void(0);" class="flex-icon approved" data-toggle="tooltip" data-placement="top" title="Aprovar" data="5"><i class="fa fa-check-square-o fa-2x" aria-hidden="true">Aprovar</i></a>
      </div>
    
    </div>
        
    31.08.2016 / 20:34