php jquery对话框为按钮关闭时的变量赋值

时间:2014-04-18 14:07:10

标签: javascript php jquery variable-assignment

我需要帮助分配一个变量以便在php文件中使用。我有一个功能,在jquery对话框窗口中显示谷歌图书搜索结果。这工作正常(有一个小故障,我正在单独工作)。我现在想要做的是为变量赋值,以便在php文件中进一步使用。例如,分配书名,以便我可以在单独的表格中显示它,以便在php文件中进行书评。

这是我的代码:

  <!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>
  //$("#searchForm").on("submit", function() { return false; });
  $(function() { 
  $( "#dialog" ).dialog({
    height: 550, width: 450});
    $("input[type='btn']").click(function(){
    var book_title = '';
    switch(this.returnvalues$i) {
     case 'btn':
        book_title = "$item['volumeInfo']['title']";
         }
         $('.$book_title').html(book_title);
        });
   $( ".btn" ).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) { ?>
    <?php for($i =1; $i <11; $i++) { ?>     
              <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" method="post" name="selectbook" value="Select" class="btn" id="returnvalues$i"/></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="searchForm" name="submit" type="submit" value="Search"  />
    </fieldset>
  </form></p>

这是我尝试为一个变量赋值:

      <script>
      //$("#searchForm").on("submit", function() { return false; });
      $(function() { 
      $( "#dialog" ).dialog({
        height: 550, width: 450});
        $("input[type='btn']").click(function(){
        var book_title = '';
        switch(this.returnvalues$i) {
         case 'btn':
            book_title = "$item['volumeInfo']['title']";
             }
             $('.$book_title').html(book_title);
            });
       $( ".btn" ).click(function(){
       $( "#dialog" ).dialog( "close" ); 
       }); 
      });
      </script> 

这是我尝试使用ajax:

  $(function() { 
  $( "#dialog" ).dialog({
    height: 550, width: 450});
   $( ".btn" ).click(function(){
   var book_title = $(this).attr('returnvalues$i');
      $.ajax({
          type: "POST",
          url: 'book-search-google.php',
          data: "book_title=" + book_title,
          success: function(data)
          {
            alert("success!");
            }
            });
   $( "#dialog" ).dialog( "close" ); 
   }); 
  });    

0 个答案:

没有答案