0

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>
2
  • 2
    Why do you need php? It seems all you need is javascript to return the voucode value. Commented Aug 13, 2014 at 21:03
  • Sorry, I updated the question. You're right. If I just needed to echo the $voucode I wouldn't need php. Commented Aug 13, 2014 at 21:44

3 Answers 3

3
$('.woo-vou-voucher-code-submit-wrap').text(voucode);
Sign up to request clarification or add additional context in comments.

4 Comments

.woo-vou-voucher-code-submit-wrap is a div not an input, you want .html or .text
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>'
You could submit 'voucode' via POST using AJAX to a separate php file containing the logic you pasted in the comment above.
Yes @hofan41, that was the answer. Actually I used your first answer to display the result of the php function, in a second ajax call.
0

Since you post the voucode to the php script your code will be in $_POST['voucode'], use that however you wish. And then echo out the $postid along with the success message

PHP

<?php
$voucode = $_POST['voucode'];
//do whatever processing
//..

if($validCode){
    echo "success,".$postid;
}

JS

//check that success is the first part of the response
if( response.indexOf('success') === 0 ) {
    var postid = response.split(",")[1];
    $( '.woo-vou-voucher-code-submit-wrap' ).text(postid).fadeIn();
}

This is just a quick example. You could use other response types, like JSON, so you wouldn't have to do string splitting or other manipulations.

1 Comment

Worked perfectly for me. Thank you!
0

PHP renders the page after receiving a request. PHP runs at the server, thus once the page is loaded at your users' browser, you cannot execute PHP code. You would need to use JavaScript and in this case jQuery.

Revise the code and whenever you want to add the value of the variable, just add it using the jQuery function.

$(".woo-vou-voucher-code-submit-wrap").text(voucode);

If it is a input field from a form, then use the function val.

$(".woo-vou-voucher-code-submit-wrap").val(voucode);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.