How to create the Javascript function of Alert for a specific page menu?

Asked

Viewed 1,547 times

0

I have a page in PHP where I want to implement a Javascript alert for when the user clicks on the "Create a Call" menu to return a message to the user. I did the implementation using Alert(), but the alert returns when the page is updated and that’s not quite what I want Alert() to do on the page.

This is my PHP.

//  Create ticket
      if (Session::haveRight("ticket", CREATE)) {
         $menu['create_ticket']['id']      = "menu2";
         $menu['create_ticket']['default'] = '/front/helpdesk.public.php?create_ticket=1';
echo '<script type="text/javascript">alert("Retorne uma mensagem para o usuário ao clicar!");</script>';
         $menu['create_ticket']['title']   = __s('Create a ticket')
         $menu['create_ticket']['content'] = array(true);
      }

Because the code exceeds the character limit I will post below the full code of the page.

Link: Complete Code

Page source code generated in HTML.

<!DOCTYPE html>
<html>

</script>
<a href="#" onclick="$('#loadbookmark').dialog('open');">
  <img src="/glpi/pics/bookmark.png" title="Carregar um favorito" alt="Carregar um favorito" class="button-icon">
</a>
</li>
<li id="help_link">
  <a href="http://glpi-project.org/help-helpdesk" target="_blank" title="Ajuda">
    <img src="/glpi/pics/help.png" title="Ajuda" alt="Ajuda" class="button-icon">
  </a>
</li>
</ul>
</div>
<div id="c_recherche"></div>
</div>
<div id="c_menu">
  <script type="text/javascript">
    alert("Retorne uma mensagem para o usuário ao clicar!");
  </script>
  <ul id="menu">
    <li id="menu1"><a href="/glpi/front/helpdesk.public.php" title="Home" class="itemP">Home</a>
    </li>
    <li id="menu2"><a href="/glpi/front/helpdesk.public.php?create_ticket=1" title="Cria um chamado" class="itemP">Cria um chamado</a>
    </li>
    <li id="menu3"><a href="/glpi/front/ticket.php" title="Chamados" class="itemP">Chamados</a>
    </li>
    <li id="menu4"><a href="/glpi/front/reservationitem.php" title="Reservas" class="itemP">Reservas</a>
    </li>
    <li id="menu5"><a href="/glpi/front/helpdesk.faq.php" title="FAQ" class="itemP">FAQ</a>
    </li>
  </ul>
  <script type="text/javascript">
    < /html>

inserir a descrição da imagem aqui

But I am not able to implement the function that calls the alert for only the item in this menu and not for the whole page. Could someone help?

  • What is the HTML of this "item" what do you mean? you can explain the question better?

  • Sergio! I added my HTML code to the question.

  • Renato, reopened. You can explain better what the user should do/click and what you would like to happen?

  • I want to click the menu2 on "Create called' the user to receive a text box message I used Alert() to try this procedure, but it returns to every page.

3 answers

2

It would be something like that?

<script type="text/javascript">
    function mostraAlerta() {
        alert("Retorne uma mensagem para o usuário ao clicar!");
    }
</script>
<ul id="menu">
    <li id="menu1"><a href="/glpi/front/helpdesk.public.php" title="Home" class="itemP">Home</a>
    </li>
    <li id="menu2"><a href="/glpi/front/helpdesk.public.php?create_ticket=1" title="Cria um chamado" class="itemP" onclick="mostraAlerta();">Cria um chamado</a>
    </li>
    <li id="menu3"><a href="/glpi/front/ticket.php" title="Chamados" class="itemP">Chamados</a>
    </li>
    <li id="menu4"><a href="/glpi/front/reservationitem.php" title="Reservas" class="itemP">Reservas</a>
    </li>
    <li id="menu5"><a href="/glpi/front/helpdesk.faq.php" title="FAQ" class="itemP">FAQ</a>
    </li>
</ul>
  • I tried to do it this way Michellle, but how do I call Alert only for the menu2 and not for every page? Take a look at my source code of the page.

  • In this case it is in HTML, if I understood what you want correctly. It is already in the answer.

  • echo <script type="text/javascript"> Function mostraAlerta() { Alert("Return a message to the user by clicking!"); } </script> Parse error: syntax error, Unexpected '<' in

  • Ah, that detail was missing from your question. Explain better up there that someone can help you. We can’t guess that you want to change the code that generates the HTML you posted.

  • What would be ideal to post my PHP source code or page source code in HTML?

  • It would be ideal to post the link that sent Github, with the line marked that is this function that generates the code. It’s not such a simple change, for someone to suggest the answer you need to see more than a snippet of code that you’ve currently posted.

  • The problem Michelle that the code is too long is it exceeds the character limit, but however I will do the following post a part of the code with the link of the complete code.

  • Still unable to implement Alert with the above answer information.

Show 4 more comments

0

0

From the source code of the HTML generated page you showed, it looks like you use jQuery on the page (I believe):

<a href="#" onclick="$('#loadbookmark').dialog('open');">

if applicable and you want to run an Alert() for the field:

<li id="menu2"><a href="/glpi/front/helpdesk.public.php?create_ticket=1" title="Cria um chamado" class="itemP">Cria um chamado</a>
</li>

you can add in the header or footer of the page the following code:

<script type="text/javascript">
$(document).ready(function(){
  $('#menu2').click(function(){
    alert("Retorne uma mensagem para o usuário ao clicar!");
  }) 
});
</script>

if you don’t use jQuery I believe that Michelle’s example would work perfectly.

But you have to take into account if you want to show the alert and redirect the user, if you’re going to show information of what that menu link would be, maybe implement something like the Tooltip of Bootstrap would be a good idea.

Obs.:

If printing the code directly from php, don’t forget to put the code in simple quotes '' if the javascript or html code has double quotes ""

  • Herbert-Hudson! I executed as directed in my PHP code, but returns error on line 39. Parse error: syntax error, Unexpected '<' in online 39. Take a look at the header of my PHP https://gist.github.com/Nerato/fc5eccbf4febca4ea5215224c8e66760

  • Renato, what is missing there that you forgot to use the echo, and if this php file would render html then you would have to do so:

  • $script = <<<EOF <scritp type="text/javascript"> $(Document). ready(Function(){ $('#menu2').click(Function(){ Alert("Return a message to the user by clicking!"); }) }); </script> EOF; echo $script; is an example...

  • the mistake Parse error: syntax error, unexpected '<' in on line 39 refers that it cannot recognize the beginning of the code as php: <script type="text/javascript"> because it is javascript and you need to print it for the browser to run...

  • now that I realize that you are wanting to edit a part of a project, Glpi, only that this way that you want there will not work, first you need to find the file that generates the page, simply adding it there in the files you passed will not work

  • This file I passed on to you generating the page https://gist.github.com/Nerato/fc5eccbf4febca4ea5215224c8e66760

  • Herbert Hudson! It worked here putting in another PHP page, but the alert appears and disappears alone I want it to stay on the screen by clicking on mine, as I do?

Show 2 more comments

Browser other questions tagged

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