Update php variable from one page without reloading it through another page form

Asked

Viewed 547 times

1

I am making a password system in which the attendant can enter the password number and call a customer through a panel. The code you sent me works but not as I expected.. I need to update a page that will be working through the other page, I need the form I sent to himself and run an event on the other page that will be running

The code is working but I need to give a F5 in the whole page for the variable to update. I need to send some event to update and execute a javascript code that is a sound.

Form in which I update the variable:

<?php
     session_start();
	 
      if(!empty($_POST['senha'])){ 
	   $senha = $_POST['senha'];
	   $_SESSION['senha'] = $senha;
	  }
	  else 
		  $senha = 0000; 
?>
<!DOCTYPE html>
<html>
   <body>
      <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
         <input type=text name="senha"/>
         <button>Enviar</button>
      </form>
   </body>
</html>

Page that receives the variable:

<?php

session_start();

$senha = $_SESSION['senha'];
?>
<html>
<?php echo $senha; ?>
</html>

code sound:

	<script>
	function Senha(){
    var audio1 = new Audio();
    audio1.src = "sound/senha.mp3";
    audio1.play();
	}
	</script>

Thanks in advance..

2 answers

0


You need to give echo in $senha on the page that receives the variable? I do not know what you intend to do with it, but perhaps it would be better to give more details, the code below processes the form and displays the value of $senha on the same page of the form, with Ajax.

php file that receives the variable:

<?php
session_start();
$senha = $_SESSION['senha'];
echo json_encode(['senha' => $senha]);
?>

ajax:

<?php
 session_start(); 
  if(!empty($_POST['senha'])){ 
   $senha = $_POST['senha'];
   $_SESSION['senha'] = $senha;
  }
  else 
      $senha = 0000; 
?>
<html>
   <body>
      <form class="form_class" action="url do arquivo" method="post">
         <input type=text name="senha"/>
         <button class="button_class">Enviar</button>
      </form>
   <span class="senha"></span>
   </body>

<script>
$(".button_class").on("click", function(e){
e.preventDefault(),
    $.ajax({
    type:"post",
    url: "aqui vc coloca o url da arquivo que recebe a variabel $senha",
    data: $(".form_class").serialize(),
    success:function(data){
        var jsonSenha =JSON.parse(data);
        if($.trim(jsonSenha.senha)){
            $(".senha").html(jsonSenha.senha)
        }
    }
    })
})
</script>
</html>
  • Friend I am making a password system in which the attendant can enter the password number and call a customer through a panel. The code you sent me works but not the way I expected.. I need to update a page that will be running through the other page, I need the form I sent to itself and run an event on the other page that will be running. It is possible ?

  • Yes, but I’m not familiar mt. You have to create a var that would be: var senha = $("aqui vc coloca a classe do input da senha q ta no form").val() that one var will take the password that the person typed in the form, but you have to put it inside the function on click to be able to get the value.

  • dps of this you create another var to send the value with ajax: var senhaJson = {senha: senha} (https://www.w3schools.com/js/js_json_stringify.asp) and place this senhaJson in place of $(".form_class").serialize(). Here are some examples: https://stackoverflow.com/questions/3168401/json-formatting-sending-json-via-jquery-ajax-post-to-java-wicket-server

  • and on the page that will receive the variable you use the function json_decode() php to use the values that were received from the form, and use jquery to put the value on the screen without having to update the page. This part is better you give a search I n manjo mt of that, I only did it in websocket and it was with a framework. There must be other ways to do this, but it’s beyond my knowledge

0

Only with php, you will not be able to do this. It is not possible for one page to communicate with another directly while on different machines. Even on the same machine, it is not possible through php, only through Javascript.

One of the ways you do this is, the form that updates the password stores it in a file or something like that which has been informed. The page representing the panel make an ajax request from time to time to a third page that returns the current value stored in the file.

Simplifying would have the following files:

form_nova_password.php -> It is basically your page that will update the password, but it should store it somewhere that is accessible by other requests, such as a file

panel.php -> This is the page that will display the password that was stored by the previous page. Here must come the logic to trigger the ajax from time to time for the current password.php. If the password has changed, play the sound.

current password.php -> Returns the password that was stored.

Browser other questions tagged

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