Communication between javascript and PHP [closed]

-1

Hello, I'm a beginner in php and javascript and I'm having a hard time creating iterative tables. In my case, I have an initial table:

WhenIselectthelinethatIwanttochecktheaccidentsinmoredetail(incasetheoptionisSãoPaulo),anewtableisopenedinadialogbox:

Sofarsogood,I'vebeenabletoidentifywhichlineI'mclickingonbutIcannotpassthisinformationontothephpcode,sinceitwouldhavetoaccessthedatabaseandbringinformationabouttheselectedcity.

Anytips?

<script>
	$(document).ready(function(){
    $("tr[rel=modal]").click( function(ev){
    // Efeitos de transição para aparecer a janela semelhante um alerta
      ev.preventDefault();
		
		var cidade = $(this).attr("id"); //Essa variavel carrega a informação da linha selecionada
      }
                             </script>
<tr href="#janela1" rel="modal" class="read checked" id="<?=$linha['cidade']?>">
									<a id="<?=$linha['cidade']?>"></a>
							  
									
								<td><?=$linha['cidade']?></td>
								<td><?=$linha['acidentes']?></td>
								<td><?=$linha['fatais']?></td>
								<td><?=$linha['ano']?></td>
								<td><?=$linha['meta']?></td>
								<td><?=$linha['farol']?></td>
  
  							  </tr>

I have edited the question because I believe that it is easier to understand my doubt.

I tried here in a similar way, however, you need to give f5 so that the selected option is activated, if not f5, remains in the selected option before refreshing the page. $ (document) .ready (function () {

$ ("tr [rel = modal]"). click (function (ev) {ev.preventDefault ();

var indicator = $ (this) .attr ("id"); $ ("# window12"). load ('valida2.php', {acc: pointer});  No, I put it. $ _SESSION [prompt] = $ _ POST ['acc'];

Do you have any other tips? I'm breaking my head here.

I was able to solve this by putting the modal window to open an external page. Now I have problem in this modal window, the first time it opens its navigation tabs do not work, the second time they work normally. That repeats itself, odd try does not open and pair opens. Another detail is in the mascara, that after 4 clicks it gives a bugada, getting fading and appearing sometimes. I'll edit the topic for better visualization of the code.

$(document).ready(function(){
			
	$("tr[rel=modal]").click( function(ev){
		
        ev.preventDefault();
		
		var indicador = $(this).attr("id");
		$("#janela12").load('tabelas.php', {acc:indicador});
		
		
        var id = $(this).attr("href");
 
        var alturaTela = $(document).height();
        var larguraTela = $(window).width();
     
        //colocando o fundo preto
        $('#mascara').css({'width':larguraTela,'height':alturaTela});
        $('#mascara').fadeIn(300); 
        $('#mascara').fadeTo(300,0.8);
 
        var left = ($(window).width() /2) - ( $(id).width() / 2 );
        var top = ($(window).height() / 2) - ( $(id).height() / 2 );
     
        $(id).css({'top':top,'left':left});
        $(id).show();  
		
		
		
		
		
		
    });
 
    $('#mascara, .fechar').click(function(e){
		
			if( e.target !== this ) 
       			return;
			$('#mascara').fadeOut(30);
			$('.window').fadeOut(30);
			$('#mascara').hide();
			$('.window').hide();
			
		});
	});
<div id="mascara"></div>
							<div class="window" id="janela1">
							 						 
							 <div id="janela12"></div>
    
asked by anonymous 17.10.2016 / 03:46

2 answers

2

My answer does not fully answer your question but can somehow help you. I use in some proprietary applications datatables.net. It is a framework for dealing with data capture and formatting of tables, is done in JavaScript and adapts very well with php (with other CSS frameworks also if applicable) You will see on the website that there are examples showing this "exchange" of information between php and JavaScript. As others put it, it is not possible to pass data directly from php to JavaScript, but it is possible to synthesize this by making a call to php using ajax and javascript and processing / handling the return.

    
17.10.2016 / 16:46
1

It is not possible to "pass" Javascript variables to PHP, since each one works in different places, the reverse can only occur when the document is rendered.

Javascript (pure, as in this case, because there are variations that run backend) runs on the client, that is, on the computer of who is accessing.

PHP is a back-end language, ie it runs on the server running the application.

In this way, to have this interaction it is necessary to use an HTTP request (Post, Get, Push, etc ...).

For this request to occur without you switching pages, it is necessary to make an asynchronous call (because if it was a synchronous call, your front would be directed to that url).

To do this you can use AJAX .

Example:

  function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "ajax_info.txt", true);
  xhttp.send();
}

I think using Ajax Jquery makes it friendlier to anyone who is starting.

    
17.10.2016 / 15:03