Button running one function in js and another in php

Asked

Viewed 126 times

0

Good afternoon guys, I’m having a doubt would have as soon as I clicked on the button it called a function in js and another in php?

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

<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!";
		}
	
	 ?>

1 answer

0

So Samuel, I think that the way you want it is not possible, because Javascript and PHP will try to run at the same time, the easiest to do is to do the verification you want via Javascript.

Allocate this PHP code to another file and call it via AJAX within Javascript after doing the verification you want, here’s a tutorial to help you with AJAX:

http://api.jquery.com/jquery.ajax/

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

If it is not clear, just warn :) Hugs!

  • I passed to the php file, but at the time of ajax I am very difficult, this file is returning in echo, I do not know if it is possible to return the values in a grid or table, from what I understood in ajax, have to pass the values to an id in html, as it would be in a table or grid the values returned?

  • Ajax sends an object, then you put what you want on that object, and you read it in PHP as you wish too, rs, I only recommend decoding it from JSON to PHP with some native PHP function that should exist.

Browser other questions tagged

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