0

I am trying to create a Wordpress webpage that has an input text box and a button. what I want to do is take the input text from the text box and use it as a parameter to search a table I put into the website database and print the results on the screen. I tried using PHP to no avail. what is the best to do this? here is the code

Search the Film Database <input id="input" name="input" type="text" /> [php] global $wpdb; echo '<button type="button" onclick="myFunction()">Search</button>'; $text = sanitize_text_field($_POST['input']); echo $text; $wpdb->query('USE vcuw2938212664'); function myFunction(){ $txt = sanitize_text_field($_POST['input']); echo 'text is "$txt; $query = 'SELECT * FROM documentaries WHERE English_Title = \'%Li%\';'; echo $query; $results = $wpdb->get_results($query); if ($results != Null){ echo "{$results}"; } else { echo 'no results returned'; } } [/php] 
5
  • 1
    Can you show what you have tried in your question please? Oct 27, 2016 at 22:58
  • sure! here is my code! Nov 4, 2016 at 13:56
  • Search the Film Database <input id="input" name="input" type="text" /> [php] global $wpdb; echo '<button type="button" onclick="myFunction()">Search</button>'; $text = sanitize_text_field($_POST['input']); echo $text; $wpdb->query('USE vcuw2938212664'); function myFunction(){ $txt = sanitize_text_field($_POST['input']); echo 'text is "$txt; $query = 'SELECT * FROM documentaries WHERE English_Title = \'%Li%\';'; echo $query; $results = $wpdb->get_results($query); if ($results != Null){ echo "{$results}"; } else { echo 'no results returned'; } } [/php] Nov 4, 2016 at 13:57
  • Please put the code in your question and format it. Nov 4, 2016 at 14:17
  • 1
    You shouldn't be using plugins that embed PHP code in [php] shortcodes, they're a major security risk
    – Tom J Nowell
    Mar 5, 2021 at 22:10

1 Answer 1

0

you can use WP_Query(); to query the results.

it has an argument 's' => $search_query,

create an input, get the input value, store it in $search_query, then create a query,

$query = new WP_Query( array( 'post_type' => 'post', 's' => $search_query ) );
if ( $query->have_posts() ) : 
     while ( $query->have_posts() ) : $query->the_post();

     // do something...

     endwhile;
     wp_reset_postdata();
endif;
9
  • i use ajax to create custom search forms for advance property search you can see an example here : 216.120.250.9/~kathleenkellett/local-info/businesses Nov 8, 2016 at 16:19
  • I know that I want to search a table named "documentaries" that I already inputted and and to find row where the the column "English_Title" has the input String in it. How do I do that? Nov 8, 2016 at 18:48
  • by the way, I am having a problem looking for a way to get a button to run it! Nov 8, 2016 at 19:13
  • the table name is "documentaries" ? what are you creating? Nov 8, 2016 at 22:33
  • I am creating a database SQL search engine to search the table "documentaries" based on a user inputted string from the input called "input" and prints the results of that search to the screen Nov 15, 2016 at 18:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.