I want to fire Jquery to execute php code inside different file. However I cannot find a way to get the Jquery command to execute.
I am using wordpress.
My code inside functions.php
add_action( 'wp_footer', 'mycustom_wp_footer' ); //adding funtion for each form buttom to fire when pressed
function mycustom_wp_footer() { //function form each form button to fire when button pressed
?>
<script type="text/javascript">
var wpcf7Elm = document.querySelectorAll( '.wpcf7' );
wpcf7Elm.forEach(function(formr){
formr.addEventListener( 'wpcf7submit', function( event ) {
//if ('wpcf7-f101-p97-o1' == event.detail.unitTag) { //when button inside form with this unitTag is pressed do code bellow
var gen3_name = "25";
//tis code is not working
$(document).ready(function(){ //adding ajax, Jquery to send data to .php file for php function to execute
$.post("gen3.php", name: gen3_name);
});
//tis code is not working
var idform = event.detail.unitTag;
alert (idform);
}, false ); })
</script>
My code inside gen3.php
global $wpdb;
if (isset($_POST['name'])) {
$name = $_POST['name'];
if ($name == "25") {
$wpdb->update('Gen3', array('tvalue' => 1,), array('id' => 25), array('%d'), array('%d'));
}
}
Can you tell me what output do you get in your console? Any error or warning? I think your code will be placed before attaching the JQuery file. Put the priority of the hook to a high number which means low priority and it will be placed after attaching the jquery.
add_action( 'wp_footer', 'mycustom_wp_footer', 0, 999999 );
Also because of loading JQuery NoConflict or some other plugins, You might need to use JQuery.method() instead of $.method.