PHP/JS - How to display an Alert() with the value of $_SESSION

Asked

Viewed 944 times

0

Good evening guys ! I needed some help with the Alert(); , I’m trying to show off on the page index.html, one Alert(); showing that the data was inserted correctly, reading some posts on the forum, gave the idea of on the page index php. (where the data is processed) save the message to a $_SESSION and pull with JS la no HTML that value of $_SESSION. I tested the idea and the result was this:

PHP code:

$_SESSION['registro'] = "Seu gasto foi inserido corretamente!!";
header("location: index.html");

Code in the HTML

<script>alert(".$_SESSION['registro'];.");</script>

Remembering that I tried to change the "" to '', both from $_SESSION and Alert();

The result was :

inserir a descrição da imagem aqui

The question is ... Where is the mistake? If there is no error, what alternative can I use to display this alert after the Insert made by index.php?

[RESOLVED]

I changed my index.html for index php. then added the following line of code :

<script>alert('<?php session_start();
 if($_SESSION['registro']!=null)
    {echo $_SESSION['registro'];}
 ?>');</script>

Important to point out that I was not putting the session_start();

And on my data loading page I added the value of $_SESSION

session_start();
           if($_SESSION['registro'] == null){
                $_SESSION['registro'] = "Registro cadastro com sucesso !";
           }

           header("location: index.php");

2 answers

3


I didn’t get full knowledge of PHP, but from what I’ve learned, I can say that the problem lies exactly in the fact that you’re trying to get the value in an HTML file, which you can’t "access" PHP’s "objects". Therefore, you would have to turn this HTML file into PHP, and in case, I think you could do so:

<script>alert("<?php echo $_SESSION['registro']; ?>");</script>

[Edited]

Since you are now working with two PHP files, in which one assigns the value, and the other accesses the value, you should use the session_start() in both, and in what accesses the value, you should do so:

<script>
    alert("<?php echo isset($_SESSION['registro'])?$_SESSION['registro']:null; ?>");
</script>

That is, if the $_SESSION['registro'] exists, it puts its value, otherwise the value will be null.

I hope I’ve helped!

  • Talks Ustavo !! I did the test and it still didn’t work, even changing the extension to PHP and adding the php code

  • Are you using a personal server or are you hosting the web page? Whatever, there is PHP support, correct?

  • I’m using xampp to use on localhost

  • What value is coming out?

  • No value is coming out (printing nothing on the screen), I did the test on the same page, and it worked, however, I can not use the header to go to the home page, because if I use it Alert does not appear (visibly)

  • I added more information to the reply

  • Thanks !! worked out Gustavo !!

Show 2 more comments

-1

<?php if (isset($_SESSION['registro'])) { ?>
    <script>alert('<?= $_SESSION['registro'] ?>');</script>
<?php } ?>

Browser other questions tagged

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