Title: Use Search Query as Javascript variable
Last modified: August 30, 2016

---

# Use Search Query as Javascript variable

 *  Resolved [diswell](https://wordpress.org/support/users/diswell/)
 * (@diswell)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/use-search-query-as-javascript-variable/)
 * Hi,
 * I’m currently testing a new Amazon Ad Search unit on my WordPress site. It uses
   javascript to display relevant products on the WordPress search page, based on
   the search term used.
 * I need to programmatically define the search term to be used each time the user
   searches. – it can be a variable. How do I transfer the search query into a variable
   that can be used in Javascript?
 * The accompanying doc just says:
 * amzn_assoc_default_search_phrase = “searchterm”; //programmatically specify this
 * Any help gratefully received

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [Deepak](https://wordpress.org/support/users/deepakstoic/)
 * (@deepakstoic)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/use-search-query-as-javascript-variable/#post-6316664)
 * You can use wp_localize_script to transfer data to Jacascript.
 * wp_register_script( ‘some_handle’, ‘path/to/myscript.js’ );
 * // Localize the script with new data
    $data_array = array( ‘amzn_assoc_default_search_phrase’
   => $php_variable ); wp_localize_script( ‘some_handle’, ‘object_name’, $data_array);
 * // Enqueued script with localized data.
    wp_enqueue_script( ‘some_handle’ );
 * [https://codex.wordpress.org/Function_Reference/wp_localize_script](https://codex.wordpress.org/Function_Reference/wp_localize_script)
 *  Thread Starter [diswell](https://wordpress.org/support/users/diswell/)
 * (@diswell)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/use-search-query-as-javascript-variable/#post-6316668)
 * Thanks for the reply.
 * However, the Javascript isn’t going to be a separate .js file – it’s just a small
   snippet on the embedded on the search.php itself. How would this alter the instructions?
 *  [Deepak](https://wordpress.org/support/users/deepakstoic/)
 * (@deepakstoic)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/use-search-query-as-javascript-variable/#post-6316669)
 * use $_GET[‘s’] to get search query term.
 *  Thread Starter [diswell](https://wordpress.org/support/users/diswell/)
 * (@diswell)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/use-search-query-as-javascript-variable/#post-6316670)
 * Great, that makes sense. So, the javascript snippet is below. The key line is:
 * amzn_assoc_default_search_phrase = “searchterm”; //programmatically specify this
 * I need to take the results of the use $_GET[‘s’] command you mention and embed
   them into “searchterm”. How would I do this?
 * Thanks again.
 * —-
 * <script charset=”utf-8″ type=”text/javascript”>
    amzn_assoc_ad_type = “smart”;
   amzn_assoc_ad_mode = “search”; amzn_assoc_marketplace = “amazon”; amzn_assoc_region
   = “US”; amzn_assoc_tracking_id = “mytag”; amzn_assoc_default_search_phrase = “
   searchterm”; //programmatically specify this </script> <script src=”//z-na.amazon-
   adsystem.com/widgets/onejs?MarketPlace=US”></script>
 *  [Deepak](https://wordpress.org/support/users/deepakstoic/)
 * (@deepakstoic)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/use-search-query-as-javascript-variable/#post-6316671)
 * It will be like
 * <script charset=”utf-8″ type=”text/javascript”>
    amzn_assoc_ad_type = “smart”;
   amzn_assoc_ad_mode = “search”; amzn_assoc_marketplace = “amazon”; amzn_assoc_region
   = “US”; amzn_assoc_tracking_id = “mytag”; amzn_assoc_default_search_phrase = 
   <?php echo $_GET[‘s’]; ?> ; //programmatically specify this </script> <script
   src=”//z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US”></script>
 *  Thread Starter [diswell](https://wordpress.org/support/users/diswell/)
 * (@diswell)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/use-search-query-as-javascript-variable/#post-6316672)
 * Works very well – thanks so much, Deepak – I truly appreciate you assistance.
 * All the best
    Terry
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/use-search-query-as-javascript-variable/#post-6316673)
 * Never, _**EVER**_ use user input in a PHP script without first sanitizing the
   input. The above code would allow someone to DDoS a site with a specially-crafted
   search term.
 * In this specific case, that means escaping all double-quote characters in the
   $_GET superglobal. Something like this:
 *     ```
       amzn_assoc_default_search_phrase = "<?php echo str_replace('"', '\"', $_GET['s']); ?>";
       ```
   
 *  Thread Starter [diswell](https://wordpress.org/support/users/diswell/)
 * (@diswell)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/use-search-query-as-javascript-variable/#post-6316674)
 * Noted – thanks; I appreciate the warning. I updated the command and all looks
   great.
 *  [Deepak](https://wordpress.org/support/users/deepakstoic/)
 * (@deepakstoic)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/use-search-query-as-javascript-variable/#post-6316675)
 * WordPress provide ‘sanitize_text_field’ function to sanitize the input data. 
   It does more than removing the double quotes.
 * Behinds the scenes, the function does the following:
 *  Checks for invalid UTF-8 (uses wp_check_invalid_utf8())
    Converts single < characters
   to entity Strips all tags Remove line breaks, tabs and extra white space Strip
   octets
 * In your code it will be like
    $searchterm = sanitize_text_field($_GET[‘s’]); 
   amzn_assoc_default_search_phrase = <?php echo $searchterm ; ?> ; //programmatically
   specify this

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Use Search Query as Javascript variable’ is closed to new replies.

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 9 replies
 * 3 participants
 * Last reply from: [Deepak](https://wordpress.org/support/users/deepakstoic/)
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/use-search-query-as-javascript-variable/#post-6316675)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
