drny60365 2014-05-12 12:15
浏览 73
已采纳

jquery ajax input [name = somename] val(data) - 如何识别多个变量

With help from this site, so far I have figured out how to get a value to an input field through ajax. Now I need to get specific values to separate input fields (user form). Overview; the user is selecting data from jquery results to propagate to the form so the user does not have to manually enter that data.

Here is my script code:

  <script>
  var book_title;
  var author;
  var published;
  var description;
  var pages;
  var Publisher;
  var ISBN;
  $(function() { 
  $( "#dialog" ).dialog({
    height: 550, width: 450});
    $( ".btn" ).click(function(){ 
   //var book_title = $item['volumeInfo']['title'];
   $.ajax({
     type: "POST",
     url: 'book-meta.php',
     //dataType: 'json',
     //data: { book_title : $("#returnvalues").val()  },
     data: { book_title : '$title', author: "Author"},
     success: function(data)
         {
         //alert(data);
         //console.log(data);
         $("input[name='bbp_extra_field1']").val(data);
         $("input[name='bbp_extra_field2']").val(data);
         //$("#displayResults").html(data);
         },
         error: function(errorThrown){
         alert('error');
         }
         });
  $( "#dialog" ).dialog( "close" );  
  });
  });
  </script>

Here is the book-meta.php code:

     if ( isset( $_POST['book_title'] )) {
//      echo $_SESSION['book_title'];
//        $book_title = $_REQUEST['book_title'];
        $book_title = $_POST['book_title'];
        echo $book_title;

      }
     if ( isset( $_POST['author'] )) {
        $author = $_POST['author'];
        echo $author;

      }

Here is the code for data entry. So far, I can get data to the $value part, but it is the same data going to both input fields. In short, I am trying to get book_title to bbp_extra_field1, and I'm trying to get author to bbp_extra_field2.

add_action ( 'bbp_theme_before_topic_form_content', 'bbp_extra_fields');
function bbp_extra_fields() {
//  include 'book-meta.php';

  //This is where the cover will go, from google book search
   $value = get_post_meta( bbp_get_topic_id(), 'bbp_extra_fieldc', true);
//   echo '<label for="bbp_extra_fieldc">Cover</label><br>';
   echo "<p><input type='image' name='bbp_extra_fieldc' value='".$value."'></p>";
  //Input field for the Author
   $value = get_post_meta( bbp_get_topic_id(), 'bbp_extra_field1', true);
   echo '<label for="bbp_extra_field1">Author</label>';
   echo "<p><input type='text' name='bbp_extra_field1' value='".$value."'></p>";

   $value = get_post_meta( bbp_get_topic_id(), 'bbp_extra_field2', true);
   echo '<label for="bbp_extra_field2">Published</label><br>';
   echo "<p><input type='date' name='bbp_extra_field2' value='".$value."'></p>";

Any help would be appreciated. Thanks.

  • 写回答

1条回答 默认 最新

  • doutenglou6588 2014-05-12 12:36
    关注

    You can use json:

     if ( isset( $_POST['book_title'] )) {
        $book_title = $_POST['book_title'];
      }
     if ( isset( $_POST['author'] )) {
        $author = $_POST['author'];
      }
    
      $r=array("title"=>$book_title,"author"=>$author);
      print(json_encode($r));
    

    I didn't check whether $book_title and $author exist in the above code. You probably need to check that.

    In jquery.ajax adding dataType and changing success:

    dataType: 'json',
    success: function(data) {
         $("input[name='bbp_extra_field1']").val(data.title);
         $("input[name='bbp_extra_field2']").val(data.author);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line