0

On clicking the text, JS function should get executed.

In HTML:

<html>
<body>
<a href="#" onclick="clickthis();">Table 1</a>
</body>
</html>

In PHP:

<?php
require 'dbconnect.php';

function clickthis(){
$search_table = "SELECT `table1` FROM `booktable` WHERE `table1`='booked';"
$query = mysqli_query($dbconnect,$search_table);
  if(mysqli_num_rows($query)==0){
  "INSERT INTO `booktable` (table1`) VALUES(`booked`)";
  echo'Table booked successfully.';
?>


Have I placed the onclick function correctly in HTML?
Whenever I click on the text Table 1, clickthis() function should get executed but how can I achieve the result using PHP, JavaScript & HTML.

P.S. I don't want to use button click & dbconnect.php is external php file.

007mrviper
  • 459
  • 4
  • 20

1 Answers1

0

You need to execute the PHP script through an AJAX call. The way you are approaching the structure is wrong.

You can take the following steps.

  1. Create a PHP script and serve it on say /booktable
  2. Have a js script to fetch a the above url through an AJAX call.
  3. Bind js function to onClick in your HTML markup.
nilobarp
  • 3,806
  • 2
  • 29
  • 37