Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!