无法将javascript变量(整数)传递给php

时间:2014-05-19 03:00:57

标签: javascript php variables integer client-server

我知道php在服务器上,而javascript在客户端上。我学会了如何将变量从服务器传递到客户端,然后再传递回服务器。我现在遇到的问题特别令人沮丧。我试图从javascript传递变量。这个特殊变量是计数的结果。这是一个编辑 - 我相信我已经将我的问题缩小到了javascript部分和php文件。

仅供参考:这是做什么的;用户在搜索窗口中输入书名,谷歌api用于在jquery窗口中返回结果。然后,用户将选择他/她想要的书,并且该数据将自动填充一些窗口。我在那里95% - 只需要获得用户选择的特定行。

  <script>
  //var bookSelect;
  $(function() {
  $( "#dialog" ).dialog({
    height: 550, width: 450});
    $( ".submit" ).click(function(){
    if(this.id.indexOf('select')>-1) { var id = (this.id.split("_"))[1]; console.log(id); }
    bookSelect = id;
    //$("#select").val(bookSelect);
   $.ajax({
     type: "POST",
     url: 'book-meta.php',
     async:true,
     dataType: 'json',
     //assign values to the variables to be passed to the server via data
     data: { cover : cover, title : title, author : author, published : published, ISBN : ISBN, 
     description : description, pages : pages, publisher : publisher, 'bookSelect' : bookSelect},
     success: function(data)
         {
         //identify the variables for unique handing on the server side, this is
         //how javascript variables (client side) are passed to the server
         $("input[name='bbp_extra_fieldc']").val(data.cover);
         $("input[name='bbp_topic_title']").val(data.title);
         $("input[name='bbp_extra_field1']").val(data.author);
         $("input[name='bbp_extra_field2']").val(data.published);
         $("input[name='bbp_extra_field3']").val(data.description);
         $("input[name='bbp_extra_field4']").val(data.pages);
         $("input[name='bbp_extra_field5']").val(data.publisher);
         $("input[name='bbp_extra_field6']").val(data.ISBN);
         //$("input[name='bbp_extra_fieldi']").val(data.bookSelect);
         //$('#select').val(data.bookSelect);
         //alert(data);
         alert(bookSelect);
         },
         error: function(errorThrown){
         alert('error');
         }
         });
  $( "#dialog" ).dialog( "close" );  
  });
  });
  </script> 

这是book-meta.php代码:

     if ( isset( $_POST['bookSelect'] )) {
        $int = $_POST['bookSelect'];
        //echo $int;
        }
     if ( isset( $_POST['cover'] )) {
        $cover = $_POST['cover'];
      }
     if ( isset( $_POST['title'] )) {
//      echo $_SESSION['book_title'];
//        $book_title = $_REQUEST['book_title'];
        $title = $_POST['title'];
      }
     if ( isset( $_POST['author'] )) {
        $author = $_POST['author'];
      }
     if ( isset( $_POST['published'] )) {
        $published = $_POST['published'];
      }
     if ( isset( $_POST['description'] )) {
        $description = $_POST['description'];
      }
     if ( isset( $_POST['pages'] )) {
        $pages = $_POST['pages'];
      }
     if ( isset( $_POST['publisher'] )) {
        $publisher = $_POST['publisher'];
      }
     if ( isset( $_POST['ISBN'] )) {
        $ISBN = $_POST['ISBN'];
      }

      $r=array("cover"=>$cover,"title"=>$title,"author"=>$author,"published"=>$published,"description"=>$description,
      "pages"=>$pages,"publisher"=>$publisher,"ISBN"=>$ISBN,"bookSelect"=>$int);
      print(json_encode($r));

从javascript传递的变量是bookSelect = id;。我得到了我应该进入警报的值(bookSelect),这是一个数字。我想知道我是否在php文件中处理错误。我是否需要将其声明为整数然后使用print json_encode或其他东西?我已经在这2天了。我很绝望!

以下是jquery窗口的输出:

<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 $i=0; foreach ($data['items'] as $item) { $i++; ?>     
                  <tr>
            <td>
                       <strong><u><div style="font-size: 14px";><?php printf($item['volumeInfo']['title']);
               $b_title[$i] = $item['volumeInfo']['title']?></u></div></strong>
                         <strong>Author: </strong><?php printf( $item['volumeInfo']['authors'][0]);
               $b_author[$i] = $item['volumeInfo']['authors'][0]?><br />
                         <strong>Published: </strong><?php printf( $item['volumeInfo']['publishedDate']);
               $b_published[$i] = $item['volumeInfo']['publishedDate'] ?><br />                      
               <strong>Page(s): </strong><?php printf( $item['volumeInfo']['pageCount']);
               $b_pages[$i] = $item['volumeInfo']['pageCount'] ?><br />
                         <strong>Publisher: </strong><?php printf( $item['volumeInfo']['publisher']);
               $b_publisher[$i] = $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']);
               $b_ISBN[$i] = $item['volumeInfo']['industryIdentifiers'][0]['identifier'] ?></td>
            <td><p><?php echo "<input type=\"submit\" method=\"post\" name=\"submit\" value=\"Select\" class=\"submit\" id=\"select_$i\" />"; ?></p>
            <img src="<?php printf( rawurldecode($item['volumeInfo']['imageLinks']['smallThumbnail']));
            $b_cover[$i] = $item['volumeInfo']['imageLinks']['smallThumbnail'] ?>" />
                    </td>
            <tr><td style="width:420px"><p><strong>Description: </strong><?php printf( $item['volumeInfo']['description']);
            $b_description[$i] = $item['volumeInfo']['description'] ?><br /></p></td>
            </tr>
            </tr>

以下是变量声明:

              $book_cover = $b_cover[$b];
              $book_title = $b_title[$b];
              $book_author = $b_author[$b];
              $book_published = $b_published[$b];
              $book_description = $b_description[$b];
              $book_pages = $b_pages[$b];
              $book_publisher = $b_publisher[$b];
              $book_ISBN = $b_ISBN[$b];
              //}
             ?>
              <script>
              //assign values to each javascript variable with results on the server
              //this is how php variables are passed from server to the client - php to javascript
              var cover = <?php echo json_encode($book_cover); ?>;
              var title = <?php echo json_encode($book_title); ?>;
              var author = <?php echo json_encode($book_author); ?>;
              var published = <?php echo json_encode($book_published); ?>;
              var description = <?php echo json_encode($book_description); ?>;
              var pages = <?php echo json_encode($book_pages); ?>;
              var publisher = <?php echo json_encode($book_publisher); ?>;
              var ISBN = <?php echo json_encode($book_ISBN); ?>;
              </script>       

0 个答案:

没有答案