Sorry if it's a dumb question. I'm really new in Ajax. I'm trying to change a Wordpress plugin code that validates a voucher code. The code is loaded in javascript calling its id from the input html field.
The html looks like this:
<input type="text" id="woo_vou_voucher_code" name="woo_vou_voucher_code" value="" />
And the js
jQuery( document ).ready( function( $ ) {
// Check Voucher code is valid or not
$( document ).on( 'click', '#woo_vou_check_voucher_code', function() {
//Voucher Code
var voucode = $( '#woo_vou_voucher_code' ).val();
if( voucode == '' || voucode == 'undefine' ) {
//hide submit row
$( '.woo-vou-voucher-code-submit-wrap' ).fadeOut();
$( '.woo-vou-voucher-code-msg' ).removeClass( 'woo-vou-voucher-code-success' ).addClass( 'woo-vou-voucher-code-error' ).html( WooVouCheck.check_code_error ).show();
} else {
//show loader
$( '.woo-vou-check-voucher-code-loader' ).css( 'display', 'inline' );
//hide error message
$( '.woo-vou-voucher-code-msg' ).hide();
var data = {
action : 'woo_vou_check_voucher_code',
voucode : voucode
};
//call ajax to chcek voucher code
jQuery.post( WooVouCheck.ajaxurl, data, function( response ) {
//alert( response );
if( response == 'success' ) {
//show submit row
$( '.woo-vou-voucher-code-submit-wrap' ).fadeIn();
$( '.woo-vou-voucher-code-msg' ).removeClass( 'woo-vou-voucher-code-error' ).addClass( 'woo-vou-voucher-code-success' ).html( WooVouCheck.code_valid ).show();
}
It is working very well, but what I want now is that inside the div '.woo-vou-voucher-code-submit-wrap' I can echo the voucode var used in the js file. Something like:
<div class="woo-vou-voucher-code-submit-wrap"><?php echo $voucode; ?><div>
I have no idea how to achieve that...
Edit: Sorry, I used just the echo for example. What I need is to use the $voucode inside other php function.
<?php
global $wpdb;
$querystr = "SELECT post_id
FROM $wpdb->postmeta
WHERE
(meta_key = '_woo_vou_purchased_codes' AND meta_value = '$voucode')";
$postid = $wpdb->get_var($wpdb->prepare($querystr));?><div class="woo-vou-voucher-code-submit-wrap"><?php echo $postid; ?><div>