Doubt Syntax error in PHP / jQuery function

Asked

Viewed 22 times

1

I’m consuming a Webservice on a client’s website. i get the url return, filter the way I need, to display only the championships on condition:
"SEX : M "
"MODALITY : 2"
"CATEGORY : 4"
Being all true, returns me the name of the correct championship, only that is returning the following error:

Uncaught SyntaxError: Unexpected token <

In the following line:
"var html = '<a class="list-group-item list-group-item-action active" data-toggle="list" href="#home" role="tab">'+<td>1ª Fase</td><td>CIRCUITO ESCOLAR - SÉRIE OURO</td>+'</a>';"

I’ve tried writing in other ways, concatenate, and none of the error go away, please could help me?

php test.

<?php 
$api_request = 'https://sportsmanager.com.br/api/[email protected]&token=2HSDE78623GVS7234GNMSKL';
$api_response = wp_remote_get( $api_request );
$api_data = json_decode( wp_remote_retrieve_body( $api_response ), true );
$retorno = '';

if($api_data){
    foreach($api_data as $row){
        if(!is_array($row)){
            //$retorno = $retorno.'<td>'.$row.'</td>';
        }else{
            if($row['sexo'] == 'M' && $row['modalidade'] == 2 && $row['categoria'] == 4){
                $retorno = $retorno.'<td>'.$row['nome'].'</td>';
            }   
        }

    }
}

jQuery

$(function(){
		
		var html = '<a class="list-group-item list-group-item-action active" data-toggle="list" href="#home" role="tab">'+<?php echo $retorno;?>+'</a>';
	  
	  	$('#myList').html(html);
	  	
	});

  • You are creating the wrong html, it has to be all within a single quote ''.

1 answer

1


The right thing would be:

$(function(){

    var html = '<a class="list-group-item list-group-item-action active" data-toggle="list" href="#home" role="tab"><?php echo $retorno;?></a>';

    $('#myList').html(html);

});

No need to concatenate the javascript string because php acts on the server and already renders html right there.

  • Man, it worked out! , thanks for the help, I don’t have much experience with this kind of function, I would like each of the championships to be shown in a different div, you could explain to me how to do this?

  • I can try to help yes @Miguelcampos. I advise to open another question by specifying your problem better, and there I try to answer it again.

  • right, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.