Skip to:
Content
Pages
Categories
Search
Top
Bottom

Google Books API Function triggers unwanted topic submission


  • Jerry
    Participant

    @jerrysc

    If this challenge I am having is beyond the scope of help in the bbpress forums, then I understand. The below code is a function that allows the user to submit a search request for a book title via the google books api. When the user clicks “search”, the function works great and returns results in a Jquery window. The problem I am having; it triggers a submit of the bbpress topic, and I get the errors “cannot submit a topic with the subject line blank.”

    If there is a simple fix I am not seeing, can someone please help me?

    function book_search_google () {    
      //set API version for Google Book Search API
      $v = isset($_POST['v']) ? $_POST['v'] : '1';
      //set user API key for Google Book Search API
      $key = isset($_POST['key']) ? $_POST['key'] : 'key goes here';
      //set user IP for Google Book Search API
      //$ip = isset($_POST['ip']) ? $_POST['ip'] : $_SERVER['REMOTE_ADDR'];
      //set default value for query to Google Book Search API 0307387941
      $query = isset($_POST['q']) ? $_POST['q'] : '0307387941';
      //set default value for search type to Google Book Search API
      $type = isset($_POST['type']) ? $_POST['type'] : 'all';
      //check and assign page of search results - are we on the first page?
      $start = isset($_POST['start']) ? $_POST['start'] : 1;
      //set default value for number of results
      $limit = isset($_POST['limit']) ? $_POST['limit'] : '10';
    
      switch ($type) {
    
        case 'all':
          $params = 'q='.urlencode($query).'&startIndex='.$start.'&maxResults='.$limit;
        break;
    
        case 'isbn':
                $params = 'q=isbn:'.urlencode($query).'';
        break;
    
        case 'lccn':
                $params ='q=lccn:'.urlencode($query).'';
        break;
    
        case 'oclc':
                $params = 'q=oclc:'.urlencode($query).'';
        break;
    
        default:
          echo '<p>You must specify a search type such as "all" or "book". Check the url to make sure "type=" has a value.</p>';
          exit;
          }
      //set URL for the Google Book Search API
      $url = 'https://www.googleapis.com/books/v'.$v.'/volumes?key='.$key.'&'.$params.''; 
    
      if(isset($_POST['q'])):
    
      //build request and send to Google Ajax Search API
      $request = file_get_contents($url);
      //decode json object(s) out of response from Google Ajax Search API
      $data = json_decode($request,true);
      $totalItems = $data['totalItems']; 
    
     pagination  mainHeading
        if ($totalItems > 0) {
      ?>
    
          <!doctype html>
          <html lang="en">
          <head>
          <meta charset="utf-8">
          <title>jQuery UI Dialog - Default functionality</title>
          <link rel="stylesheet" href="//whosgotbooks.com/jquery/jquery-ui-1.10.4.custom.css">
          <script src="//whosgotbooks.com/jquery/jquery-1.10.2.js"></script>
          <script src="//whosgotbooks.com/jquery/jquery-ui-1.10.4.custom.js"></script>
          </head>
          <body>
          <div id="dialog" title="Google Books Search Results" style="display:none;">
          <script>
          $(function() {
          $( "#dialog" ).dialog({
            height: 550, width: 450});
            $( "#returnvalues" ).click(function(){
            $( "#dialog" ).dialog( "close" ); 
            });
            });
          </script>     
                <strong><p style="font-size: 16px; text-align: center";>Top 10 Results for &quot;<?php echo @$_POST['q']; ?>&quot;</p></strong> 
            <strong><p style="font-size: 14px; text-align: center";>choose a book to select as your topic</p></strong>&nbsp;
            <table style="width:400px">
            <col width="325">
            <col width="75">
                <?php foreach ($data['items'] as $item) { ?>    
                      <tr>
                <td>
                           <strong><u><div style="font-size: 14px";><?php printf($item['volumeInfo']['title'])?></u></div></strong>
                             <strong>Author: </strong><?php printf( $item['volumeInfo']['authors'][0]); ?><br />
                             <strong>Published: </strong><?php printf( $item['volumeInfo']['publishedDate']); ?><br />                       
                   <strong>Page(s): </strong><?php printf( $item['volumeInfo']['pageCount']); ?><br />
                             <strong>Publisher: </strong><?php printf( $item['volumeInfo']['publisher']); ?><br />
                             <strong>Category: </strong><?php printf( strtolower($item['volumeInfo']['printType']).', '.strtolower($item['volumeInfo']['categories'][0])); ?>&nbsp;&nbsp;
                   <strong>ISBN: </strong><?php printf( $item['volumeInfo']['industryIdentifiers'][0]['identifier']); ?></td>
                <td><p><input type="submit" name="submit" value="Select" id="returnvalues"/></p>
                    <img src="<?php printf( rawurldecode($item['volumeInfo']['imageLinks']['smallThumbnail'])); ?>" />
                        </td>
                <tr><td style="width:420px"><p><strong>Description: </strong><?php printf( $item['volumeInfo']['description']); ?><br /></p></td>
                </tr>
                </tr>
    
                <?php } }
                else {
                    ?>
                       <p><strong>Sorry, there were no results</strong></p>
                    <?php  } 
                /* for testing purposes show actual request to API - REMOVE when finished
                $apiRequest = $url;
                echo '<p>API request: '.$apiRequest.'</p>'; */ ?>
            </table>
          </div>
          </body>
          </html>
    
              <?php
              else: //show form and allow the user to check for Google Book search results
              ?>
    
          <p><form id="searchForm" name="searchForm" method="post"> 
            <fieldset id="searchBox">
                <label>Search for a Book:</label>
                <input class="text" id="q" name="q" type="text" value="Powered by Google" onfocus="this.value=''; this.onfocus=null;" />
                <select id="type" name="type" size="1">
                    <option selected value="all">Book Title</option>
                    <option value="isbn">Books by ISBN</option>
                    <option value="lccn">Books by LCCN #</option>
                    <option value="oclc">Books by OCLC #</option>                
                </select>
                <input class="submit" id="submit" name="submit" type="submit" value="Search"  />
            </fieldset>
          </form></p>
          <?php
          //end submit isset if statement on line 73
          endif;
          }
Viewing 4 replies - 1 through 4 (of 4 total)

  • tharsheblows
    Participant

    @tharsheblows

    Did you ever get this sorted? I looked at it but couldn’t figure it out. My first guess is that there’s something named incorrectly and it triggers an event it’s not meant to – this is what happens to me more often than not. But I couldn’t figure out how to reproduce it on your site.


    Jerry
    Participant

    @jerrysc

    @tharsheblows, thanks for inquiring about this. No, I have not figured it out. Sorry I did not get back to you; I was in Europe traveling on business.

    As a temporary fix I had removed the function call to my google books search function from form-topic.php. But I prefer to keep that function call in there. When you were trying to replicate this, I was calling my function via a shortcode.

    If you go to my site, and got to “Generations”, then click on “add a book”, you will see the google books search window. Just search for a book in that window, then watch what happens.

    Any insight you can give me would be greatly appreciated. There are still many other things I am trying to figure out, but this would be a great step.

    Thanks,
    Jerry


    tharsheblows
    Participant

    @tharsheblows

    Ah – I think it’s because you have the Google form (and its submit button) in your topic form and bbPress doesn’t check to see that the submit being clicked is its submit. I’m not completely sure on that, so it’s just a guess.

    Unfortunately, the hook bbp_theme_before_topic_form doesn’t actually go before the topic form, it’s just after the opening form tag so you shouldn’t use that. But I think you’re editing the template? If that’s the case, just add the Google book search before the opening form tag eg :
    <form id=”new-post” name=”new-post” method=”post” action=”<?php the_permalink(); ?>”>

    You might need to re-style but I think that might work.


    Jerry
    Participant

    @jerrysc

    You solved it – thanks much! Now I am on to more difficult issues. For example, how to select one of the books and get that information to populate into the fields I created. I have threads open on Stack Overflow, but still haven’t found a solution. Trying to learn how to implement Ajax within jQuery and return results to a php file.

    Thanks again for the help. We can close this thread now.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar