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>
​
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’);
​
​
[ad_2]
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.
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??