Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
want to create a javascript function which on clicking button call php function which create a select option
  1. following is the code with start button and its function which is not working but when im trying to echo hello in place of button tag then the function is working well

      <button onclick="startexam()" id="btn" class="btn btn-primary btn-sm" title="Start 
       exam">Start Exam</button>
      <?php
      function php_func(){  
      echo  '<select class="form-select" aria-label="Default select example"><option > 
      asdf</option></select>'; 
      }
    
      ?>
       <script>
      //function called when user click on start exam button
      function startexam() {
      var result = "<?php php_func(); ?>"
      document.write(result);
      }
      </script>  
    
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First, you need to understand what the technical difference between PHP and JavaScript is:

JavaScript executes scripts directly in your browser, e. g. Firefox. So, when JavaScript is executed, the page is already loaded.

PHP executes scripts on the server before the page is even in the browser.

So, if you want JavaScript to communicate with the server (the PHP script), you have to make a request to the server. To do so, use AJAX.

How does AJAX work?

From W3Schools:

  1. An event occurs in a web page (the page is loaded, a button is clicked)
  2. An XMLHttpRequest object is created by JavaScript
  3. The XMLHttpRequest object sends a request to a web server
  4. The server processes the request
  5. The server sends a response back to the web page
  6. The response is read by JavaScript
  7. Proper action (like page update) is performed by JavaScript

How to use AJAX?

You can find a complete and clear introduction on W3Schools.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda