Button executing a function in js and another in php

0

Good afternoon, I'm having a question about how I would click on the button to call a function in js and another in php?

I have a problem, I want a variable in the $ name case to receive the value that you type in an input, and when I click the button, check with db and return the results on the same page

<script type="text/javascript">
  	function fnome() {
  	$nome = document.getElementById('nome').value;
  }
  	
  	
</script>
<button onclick="fnome()">Pega</button>

 <?php 

		
		$sql = mysql_query("SELECT * FROM pedidos WHERE nome LIKE '%".$nome."%'");
		$row = mysql_num_rows($sql);
		if ($row > 0) {
			while ( $linha = mysql_fetch_array($sql)){
				$id = $linha['id'];
				$nome = $linha['nome'];
				$pedido = $linha['pedido'];
				$valor = $linha['valor'];
				
				echo "<br><br>ID: ". @$id;
				echo "<br><strong>Nome: </strong>". @$nome;
				echo "<br>Pedido: ". @$pedido;
				echo "<br>Valor: ". @$valor	;
				
			}
		}
		else {
			echo "Desculpe nenhum pedido foi encontrado!";
		}
	
	 ?>
    
asked by anonymous 20.02.2017 / 20:21

1 answer

0

So Samuel, I guess the way you want is not possible, because JavaScript and PHP will try to run at the same time, the easiest thing to do is to do the verification you want via JavaScript.

Allocate this PHP code to another file and call it via AJAX inside JavaScript after doing the check you want, here is a tutorial to help you with AJAX:

link

The summary of what I recommend you do is: Allocate the function of PHP in another file, do in the index.html or index.alguma_coisa and call the PHP file via AJAX after you perform the validation you want with the JS.

If it was not clear, just let me know :) Hugs!

    
20.02.2017 / 21:36