how to correctly write the php inside the javascript [closed]

-2

Good afternoon ..
I have the following situation:

<?php

    $array_dias_da_semana = array("domingo", "segunda", "terca","quarta","quinta","sexta","sabado"); 
    $zebra = 'domingo';
?>

    $(function()
        {
            $('.button').click(function()
            {
                bb = $(this);
                $(".tab-content div").each(function( index ) 
                {
                    if($( this ).attr('class').indexOf('active') != -1)
                    {               
                        var vdia =  "<?php echo $array_dias_da_semana[index]?>";


                        alert(" B Indice: " + index+ ' value..: '+ $(bb).val()+ ' - vdia ' + vdia);

                    }
                });
            });
        });

I can not type this line correctly:

var vdia =  "<?php echo $array_dias_da_semana[index]?>";

How should I write ????? I have ARRAY in PHP that contains 6 dates and the function in javascript that returns the index of the tab. In the above line how can you realize the index is the return of the function.
With the index I would like to get the date in the corresponding ARRAY index of PHP $ array_dias_da_semana

    
asked by anonymous 25.09.2017 / 20:07

2 answers

3

The right way to write PHP within JavaScript is to avoid doing so. I know it sounds like a joke, but it's true. I myself have used these language blends but this generates a huge amount of bugs and is difficult to read and maintain.

If you need to pass information from PHP to JavaScript it creates as few contact points as possible. In this case you would pass this array at first.

In PHP:

<?php
    $array_dias_da_semana = array("domingo", "segunda", "terca","quarta","quinta","sexta","sabado"); 
    $zebra = 'domingo';
?>

In JavaScript (with php in a line only, at start):

$(function() {
  var diasDaSemana = <?php echo json_encode($array_dias_da_semana); ?>;
  // assim a tua variável diasDaSemana fica ["domingo","segunda","terca","quarta","quinta","sexta","sabado"]
  $('.button').click(function() {
    $(".tab-content div").each(function(index) {
      if (this.classList.contains('active')) {
        var vdia = diasDaSemana[i];

        alert(" B Indice: " + index + ' value..: ' + this.value + ' - vdia ' + vdia);

      }
    });
  });
});
    
25.09.2017 / 20:15
3

PHP does not communicate directly with the front end, index is a variable of JavaScript rather than PHP, so PHP has already been processed and has already been run, PHP will look for index as if it were a constant in PHP.

In other words, you can not do this, similar to what I already answered in:

You have to understand some things first:

  • front-end

    The front end is a relative term, but in practice it is usually used to refer to what will be rendered in the browser

  • back-end

    The back-end is also relative, but in practice it is generally used to refer to general server-side technologies such as database, program that processes HTTP (like Apache and IIS), and dynamic language and frameworks

HTTP is more or less this:

You will need to change the approach

    
25.09.2017 / 20:11