I’m just beginning to learn PHP and I am developing a WordPress plugin. But I have a problem. When I press a button, I want it to call a PHP script where I have a different SQL request to execute and others things without changing page. I know I need to use Ajax but I don’t understand how use it and where I write it. Could you help me please?
index.php is where I have a function that prints a list with checkbox and the button to execute the script createInsert.php
PS: Sorry for my bad English.
index.php
<?php
public function pref_liste_checkbox(){
global $wpdb;
print '<h3>Composition de la liste synthetique</h3>';
print '<form method="POST" action=" "><table border="1"><tr><th>Attributs</th><th>Choix</th></tr>';
$resultat= $wpdb->get_results("SELECT * from wp_frm_fields ",ARRAY_N);
foreach ($resultat as $post) {
print'<tr>';
echo '<td>'.$post[2].'</td>';
echo"<td><center><input type='checkbox' name='choixP[]'value='".$post[0]."'></center></td>";
}
print '</tr><input type="submit" name ="execute"value="execute"></table></form>';
}
?>
createInsert.php
<?php
//if the table exist =1
if(FrmStatisticsController::table_ok('testWP','wp_users')==1){
?>
<script type="text/javascript">
var r = confirm("Voulez-vous vraiment mettre à jour les attributs de la liste synthétique?");
if (r == true) {
<?php
///////////CLEAN LA TABLE//////////
////////////INSERT INTO/////////
?>
var f = alert("Mà J réussie !");
}else {
///////////Aucune ActionNo Action////////
}
</script>
<?php
} else (FrmStatisticsController::table_ok('testWP','wp_users')==0){
?>
<script type="text/javascript">
var r = confirm("Voulez-vous sauvegarder les attributs de la liste synthétique?");
if (r == true) {
<?php
///////////CREATE TABLE//////////
global $wpdb;
$nom_table = $wpdb->prefix .'choix_attributs_liste';
$sql = "CREATE TABLE $nom_table ( mytable_id bigint(20) unsigned NOT NULL auto_increment, att_name varchar(255), att_id bigint(20),PRIMARY KEY(mytable_id));";
dbDelta($sql);
////////////INSERT INTO/////////
?>
var f = alert("Création et insertion des n-uplets réussie!");
}else {
///////////Aucune ActionNo Action////////
}
</script>
<?php
}
I know your Question is for the button but there is these point you should look at before .
to create plugin there is standards … see this tool will help you get the files … and set the basic plugin ready
http://wppb.me/
check some tutorials
http://www.smashingmagazine.com/2011/09/how-to-create-a-wordpress-plugin/
https://codex.wordpress.org/Writing_a_Plugin
https://wordpress.org/plugins/