Here’s my code first
JavaScript
x
28
28
1
// Initiate curl session in a variable (resource)
2
$curl_handle = curl_init();
3
4
$url = "xxxxxxxxxx";
5
6
// Set the curl URL option
7
curl_setopt($curl_handle, CURLOPT_URL, $url);
8
9
// This option will return data as a string instead of direct output
10
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
11
12
// Execute curl & store data in a variable
13
$curl_data = curl_exec($curl_handle);
14
15
curl_close($curl_handle);
16
17
// Decode JSON into PHP array
18
$response_data = json_decode($curl_data);
19
20
// Print all data if needed
21
// print_r($response_data);
22
// die();
23
24
// All user data exists in 'data' object
25
$user_data = $response_data;
26
$counter = 0;
27
28
on my HTML
JavaScript
1
61
61
1
<table id="example2" class="table table-bordered table-hover">
2
<thead>
3
<tr>
4
<th>#</th>
5
<th>Quotes Lists</th>
6
<th>Actions</th>
7
</tr>
8
</thead>
9
<tbody>
10
<?php if(!empty($user_data)) { ?>
11
<?php foreach($user_data as $user){ ?>
12
<tr>
13
<td>
14
<?php echo ++$counter; ?>
15
</td>
16
<td>
17
<?php echo $user->name; ?>
18
</td>
19
<td>
20
<div class="row">
21
<div class="col-md-6">
22
<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>
23
</div>
24
25
<div class="col-md-6">
26
<button type='button' class='btn btn-danger addAttr' data-toggle='modal' data-target='#modal-delete' data-id=<?php echo $user->id; ?>>Delete</button>
27
</div>
28
</div>
29
<!-- Modal Start-->
30
<div class="modal fade" id="modal-edit">
31
<div class="modal-dialog">
32
<div class="modal-content">
33
<div class="modal-header">
34
<h4 class="modal-title">Edit Item</h4>
35
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
36
<span aria-hidden="true">×</span>
37
</button>
38
</div>
39
<div class="modal-body">
40
<form>
41
<textarea id="name" name="name" class="form-control" rows="5" spellcheck="false">
42
43
</textarea>
44
</form>
45
</div>
46
<div class="modal-footer justify-content-between">
47
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
48
<button type="button" class="btn btn-primary">Save changes</button>
49
</div>
50
</div>
51
<!-- /.modal-content -->
52
</div>
53
<!-- /.modal-dialog -->
54
</div>
55
<!-- /.modal -->
56
</td>
57
</tr>
58
<?php } ?>
59
<?php } ?>
60
</tbody>
61
</table>
and on my JS
JavaScript
1
5
1
$('.addAttr').on('click',function() {
2
var id = $(this).attr('data-id');
3
var name = $(this).attr('data-value');
4
$('#name').val(name);
5
} );
I have this data
Now the problem is that whenever I tried to edit data it’s only getting the portion of a text like this
I am stuck to why I am only getting a portion of a text. Someone, please enlighten me.
Advertisement
Answer
Looks like you need to quote the output of $user->name
in the HTML.
e.g
JavaScript
1
20
20
1
<!-- What you have: -->
2
<button
3
type='button'
4
class='btn btn-primary addAttr'
5
data-toggle='modal'
6
data-target='#modal-edit'
7
data-id=<?php echo $user->id; ?>
8
data-value=<?php echo $user->name; ?>
9
>Edit</button>
10
11
<!-- With quotes: -->
12
<button
13
type='button'
14
class='btn btn-primary addAttr'
15
data-toggle='modal'
16
data-target='#modal-edit'
17
data-id="<?php echo $user->id; ?>"
18
data-value="<?php echo $user->name; ?>"
19
>Edit</button>
20