Richie1209 Posted December 19, 2020 Share Posted December 19, 2020 Which languages do I need to make this happen? And I am unsure of how to incorporate PHP within a javascript query and what jQuery functions I will need. Quote Link to comment https://forums.phpfreaks.com/topic/311894-how-to-get-a-checkbox-to-update-a-database-onclick-without-a-submit-button/ Share on other sites More sharing options...
Barand Posted December 19, 2020 Share Posted December 19, 2020 (edited) Javascript runs on the client, PHP on the server, so you can't incorporate PHP code inside JS. What you can do is submit an AJAX request from JS to the server, process it with PHP and send back the results as a response. In JQuery, look at $.ajax() $.get() $.post() Here's a very basic example <?php if (isset($_GET['ajax'])) { // I like to tell my script it's reciving AJAX $x = $_GET['x'] ?? 0; exit("$x squared is " . ($x**2)); // when process an AJAX request, anything that would normally be sent to the screen } // is sent back in a response message // rest of php code here ?> <html> <head> <title>Example</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> <script type='text/javascript'> $().ready( function() { $("#btnSub").click( function() { $.get ( "", // target script is "self" {"ajax":1, "x":$("#x").val() }, // data to send function(resp) { // process response $("#result").html(resp); }, "TEXT" // response type ) }) }) </script> </head> <body> Input a number <input type='text' id='x' value='0'> <br> <button id='btnSub'>Get Square</button> <hr> <div id='result'></div> </body> </html> Edited December 19, 2020 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/311894-how-to-get-a-checkbox-to-update-a-database-onclick-without-a-submit-button/#findComment-1583266 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.