1

I am trying to get the user input into a Wordpress query array. I can get the input with my jquery code but i want to add it to the 'meta_value' => '123456', in the php array. How can i change the'meta_value' => '123456', from '123456' to the var x from the jquery script?

If this is not possible can someone suggest another way to get the 'meta_value' => '123456', from an input field?

<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#bt").click(function(){
            var x = jQuery("#txt_name").val();
            alert(x);
        });
    });

</script>

<input type="text" id="txt_name"  />
<input type="button" id="bt"  value="click here" />




<?php

 $args = array(
        'meta_key' => 'cardnum',
        'meta_value' => '123456',
        'meta_compare' => '='
    );

$user_query = new WP_User_Query( $args );

// User Loop
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        echo '<p>' . $user->cardnum ." ". $user->nickname . '</p>';
    }
} else {
    echo 'Not available number.';
}
?>
2
  • 2
    You need to send that data to server using ajax or in a form. Php doesn't run in the browser. There are lots and lots of tutorials on the web about using ajax as well as ajax with wordpress Commented Dec 18, 2016 at 1:24
  • Thank you for your help. Commented Dec 20, 2016 at 9:54

1 Answer 1

1

You don't need to use a form or Ajax - you can use the URL query string instead.

When the jquery function fires, reload the page URL, but this time including a query string which contains the variable.

Then you can extract the variable via PHP.

(This is the fastest and simplest way to enable server-side processing of a variable which originated client-side.)

HTML:

<input type="text" id="txt_name"  />
<input type="button" id="bt"  value="click here" />

jQuery:

$(document).ready(function(){
    $("#bt").click(function(){
        var x = $("#txt_name").val();
        window.location.href += '?x=' + x;
    });
});

PHP:

<?php
parse_str($_SERVER["QUERY_STRING"]);
echo '<p>The value of <strong>x</strong> is... '.$x.'</p>'; 
?>
Sign up to request clarification or add additional context in comments.

1 Comment

this is describing GET ... so answer is conflicting and also not very complete

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.