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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算