仅在编辑功能上获取部分文本 php

Only getting a portion of text on edit function php

首先是我的代码

// Initiate curl session in a variable (resource)
    $curl_handle = curl_init();

    $url = "xxxxxxxxxx";
    
    // Set the curl URL option
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    
    // This option will return data as a string instead of direct output
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
    
    // Execute curl & store data in a variable
    $curl_data = curl_exec($curl_handle);
    
    curl_close($curl_handle);
    
    // Decode JSON into PHP array
    $response_data = json_decode($curl_data);
    
    // Print all data if needed
    // print_r($response_data);
    // die();
    
    // All user data exists in 'data' object
    $user_data = $response_data;
    $counter = 0;

在我的 HTML

<table id="example2" class="table table-bordered table-hover">
  <thead>
    <tr>
      <th>#</th>
      <th>Quotes Lists</th>
      <th>Actions</th>
    </tr>
  </thead>
  <tbody>
    <?php if(!empty($user_data)) { ?>
    <?php foreach($user_data as $user){ ?>
    <tr>
      <td>
        <?php echo ++$counter; ?>
      </td>
      <td>
        <?php echo $user->name; ?>
      </td>
      <td>
        <div class="row">
          <div class="col-md-6">
            <button type='button' class='btn btn-primary addAttr' data-toggle='modal' data-target='#modal-edit' data-id=<?php echo $user->id; ?> data-value=<?php echo $user->name; ?>>Edit</button>
          </div>

          <div class="col-md-6">
            <button type='button' class='btn btn-danger addAttr' data-toggle='modal' data-target='#modal-delete' data-id=<?php echo $user->id; ?>>Delete</button>
          </div>
        </div>
        <!-- Modal Start-->
        <div class="modal fade" id="modal-edit">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <h4 class="modal-title">Edit Item</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                      <span aria-hidden="true">&times;</span>
                                                    </button>
              </div>
              <div class="modal-body">
                <form>
                  <textarea id="name" name="name" class="form-control" rows="5" spellcheck="false">
                                                        
                                                      </textarea>
                </form>
              </div>
              <div class="modal-footer justify-content-between">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
              </div>
            </div>
            <!-- /.modal-content -->
          </div>
          <!-- /.modal-dialog -->
        </div>
        <!-- /.modal -->
      </td>
    </tr>
    <?php } ?>
    <?php } ?>
  </tbody>
</table>

在我的 JS 上

 $('.addAttr').on('click',function() {
    var id = $(this).attr('data-id');    
    var name = $(this).attr('data-value');
    $('#name').val(name);
    } );

我有这个数据

现在的问题是,每当我尝试编辑数据时,它只会得到这样的文本部分

我很困惑为什么我只收到了文本的一部分。哪位大佬赐教啊

看来您需要在 HTML 中引用 $user->name 的输出。

例如

<!-- What you have: -->
<button
    type='button'
    class='btn btn-primary addAttr'
    data-toggle='modal'
    data-target='#modal-edit'
    data-id=<?php echo $user->id; ?>
    data-value=<?php echo $user->name; ?>
>Edit</button>

<!-- With quotes: -->
<button
    type='button'
    class='btn btn-primary addAttr'
    data-toggle='modal'
    data-target='#modal-edit'
    data-id="<?php echo $user->id; ?>"
    data-value="<?php echo $user->name; ?>"
>Edit</button>