how to correctly write php inside javascript

Asked

Viewed 419 times

-1

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’t spell this line correctly:

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

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

2 answers

3

PHP does not communicate directly with the front-end, index is a variable of Javascript and not PHP, so PHP has already been processed and has already been executed, PHP will search index as if it were a constant in PHP.

In other words, there is no way to do this, similar to what I have 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 processed in the browser

  • back-end

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

HTTP is about this:

http

You will need to change the approach

  • 1

    Good afternoon. Thank you for your attention :). I would like to point out that with the "direction" of our colleague Sergio achieve the desired result. Hugs :)

  • @Ricardom.Souza blz, mine is a complement to understand how web applications work (HTTP communication)

  • 1

    Absolutely.. I’ve read the two links you mentioned. Perfect :)

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 used these language mixes myself but this generates a huge amount of bugs and is difficult to read and maintain.

If you need to pass PHP information to Javascript, create as few contact points as possible. In this case you would pass this array at the beginning.

In PHP:

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

In Javascript (with php in a single line, at the beginning):

$(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);

      }
    });
  });
});
  • ♦ Good afternoon. Thanks for your attention :). With your light I was able to get the desired result with json_enconde. When you talk about mixing languages this is referring to what I was trying to do? php type inside javascript or DO NOT put javascript/ jquery inside the php system??

  • @Ricardom.Souza there is N ways to do this. Sharing data in JSON or other strings can be very useful, and avoids ajax. If it is possible it is good to use. The problem is when you "scatter" PHP within Javascript logic. My suggestion is to "use sparingly", clearly separating the logic. The same goes for doing echo of Javascript chunks in PHP. It is difficult to maintain and generates bugs. (I answered your question?)

  • 1

    I did.. Once again.. Thank you for your attention.

Browser other questions tagged

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