javascript - 将 php 数组传递给 javascript 变量,然后传递给服务器

标签 javascript php jquery variables

我意识到这个问题已经被问过很多次了,但我就是不明白。我确信这很简单,但我有点摸不着头脑。我的搜索结果是 php 数组,显示在 jquery 窗口中。我最终需要将它们返回到服务器,但首先需要将它们传递给 jquery 变量。这就是我被困住的地方。

你会看到我尝试通过 $book_title,尝试将其传递给 jquery 变量“title”

<script>
      $(function() { 
      $( "#dialog" ).dialog({
        height: 550, width: 450});
        $( ".btn" ).click(function(){
        //assign values to each variable
        //var title = $("#returnvalues").val();
        var title = <?php echo json_encode($book_title);?>;
        var author = $("#returnvalues").val();
        var published = $("#returnvalues").val();
        var description = $("#returnvalues").val();
        var pages = $("#returnvalues").val();
        var publisher = $("#returnvalues").val();
        var ISBN = $("#returnvalues").val();
       //book_title = $item['volumeInfo']['title'];
       $.ajax({
         type: "POST",
         url: 'book-meta.php',
         dataType: 'json',
         //assign values to the variables to be passed to the server via data
         data: { title : title, author : author, published : published, 
         description : description, pages : pages, publisher : publisher, ISBN : ISBN},
         success: function(data)
             {
             //alert(data);
             //identify the variables for unique handing on the server side
             $("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);
             //$("#displayResults").html(data);
             },
             error: function(errorThrown){
             alert('error');
             }
             });
      $( "#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 $i=0; foreach ($data['items'] as $item) { $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" /></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>
            <?php $book_title=$item['volumeInfo']['title'];
            //$book_meta=array($item['volumeInfo']['title']=>"$book_title",$item['volumeInfo']['authors'][0]=>"$book_author");
            //print(json_encode($book_meta));

感谢任何帮助。如果这被标记为重复问题,也请给这个新手一些具体的指导。谢谢。

最佳答案

我明白了!感谢 XQDev 和 Uttara 的帮助!我必须在显示 php 结果的循环中创建一个单独的脚本。在这个单独的脚本中,我仅声明我的 javascript 变量。

<script>
var title = <?php echo json_encode($book_title); ?>;
</script> 

问题已解决!希望这个帖子能够帮助将来遇到类似问题的人。

关于javascript - 将 php 数组传递给 javascript 变量,然后传递给服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23653198/

相关文章:

带工具提示的 jquery ui slider ?

javascript - 从回发响应更新 View 状态

javascript - 第一次按下回车键时如何停止具有要触发的第一个 onclick 事件的元素

php - 从 php 数组中复制一个元素

javascript - 如何在不创建实例的情况下获取类扩展的第一个类

php - 错误处理: set_error_handler() expects the argument (errorHandler) to be a valid callback

php - Yii url 区分大小写

javascript - 如何重置 ace 主题上的滚动条?

Javascript:如何确定链接是否指向与其所在页面相同的域?

javascript - 无法使用工厂创建对象