How to do SQL insert from a button submit.

[ad_1]

Hello redditors,

How to do SQL insert (PHP) after a button click (HTML or Javascript)? Not using third party plugins. So only coding advise please.

I have done this from scratch without WordPress. But how to do it within the architecture of WordPress? I dip my smallest toe in: shortcode, plugin, functions.php and hooks.

​

Extra information

Page code:

<script src=”\\\\\\\[https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js\\\\\\\]”></script>

<h5>Registratie</h5>

<form class=”FormPersonalInfo” autocomplete=”off” method=”POST”>
<input class=”input” name=”name” type=”text” placeholder=”Name”>
<input class=”input” name=”email” type=”email” placeholder=”E-mail”>
<input class=”input” name=”address” type=”text” placeholder=”Adres”>
<input class=”input” name=”contactnummer” type=”number” placeholder=”Nummer”>
<input class=”input” name=”huisdieren” type=”number” placeholder=”Huisdieren”>
<input class=”input” name=”disclaimer” type=”checkbox”><label>Akkoord</label>
<input class=”input” type=”submit”>
</form>

<script>
jQuery(document).on(‘submit’, function(){
if($(“[name=’disclaimer’]”).is(‘:checked’)) { What Code Here?
} else {
alert(“Geen akkoord.”)}; });
</script>

&#x200B;

PHP code within function.php

function savePersonalInfo(){
global $wpdb;
$wpdb->insert( ‘petowners’,
array(
‘Name’ => $_POST[‘name’],
‘Email’ => $_POST[’email’],
‘Address’ => $_POST[‘address’],
‘Contactnumber’ => $_POST[‘contactnummer’],
‘PetAmount’ => $_POST[‘huisdieren’]),
array(
‘%s’, ‘%s’, ‘%s’, ‘%d’, ‘%d’)
); }
add_shortcode(‘csc_savepersonalinfo’, ‘savePersonalInfo’);

&#x200B;

&#x200B;

[ad_2]
2 Comments
  1. On the PHP side, define a REST endpoint to hit, then send a request there on the front end with JavaScript. Be sure to properly sanitize any user supplied data and properly authenticate if required before executing the insert.

  2. Hmmm I’m not sure this is the best way to go about it, but I’ll explain a way I can think of..

    So if you were to create a custom post type called Registrants (or whatever)

    Then when they complete the form, use an Ajax call to send the data to the function as a set of parameters, that function will use wp_insert_post to add each parameter as data in each field in a new post to the new type.
    You will have to create a series of custom fields (you can do this without ACF but it’s the easiest way), or as the content of the post as plain text.
    Is that the end goal, to store a set of people and there details??

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer