How to get the exact post ID and pass it as json(.data)?

For programming and general questions on Zend Framework

How to get the exact post ID and pass it as json(.data)?

Postby rand1026 on Tue May 16, 2017 10:30 pm

Need help, please! I work with Zendframework 3. In a phtml view file there's a "foreach" which gives a stack of posts from DB on the page. Each post has a "add-to-Favorites" button(link) It looks like this
Code: Select all
<table class="table table-striped"><tr><th>Избранное</th><th>ID</th><th>Заголовок</th><th>Дата создания</th><th>Статус</th><th>Действия</th></tr>
<?php foreach ($posts as $post): ?>

<tr><td><!--HERE IS THE "ADD-TO-FAVORITES" BUTTON-->
<a class="btnf" style="color: #B5B5B5;" data-id="<? echo $this->escapeHtml($post->getId()); ?>">

<span class="glyphicon glyphicon-star" ></span></a></td>

<td><?= $this->escapeHtml($post->getId()); ?></td>
<td><a href="<?= $this->url('posts', ['action'=>'view', 'id'=>$post->getId()]); ?>">
<?= $this->escapeHtml($post->getTitle()); ?></a></td>
<td><?= $this->escapeHtml($post->getDateCreated()); ?></td>
<td><?= $this->escapeHtml($postManager->getPostStatusAsString($post)); ?> </td>
<td> <div class="a" data-id="1">
<a class="btn btn-info" href="<?= $this->url('posts',['action'=>'edit', 'id'=>$post->getId()]); ?>">
<span class="glyphicon glyphicon-pencil" ></span> Изменить</a>
<a class="btn btn-danger" href="<?= $this->url('posts',['action'=>'delete', 'id'=>$post->getId()]); ?>">
<span class="glyphicon glyphicon-remove"></span> Стереть</a></div></td></tr>
<?php endforeach; ?> </table>


through the attribute "data-id" i try to pass the post's ID поста which is the parameter given on the phtml-view through the "foreach"-cycle
but when i try to pass it to the Controller's action via $.ajax({}) (in json data) - then i can have only fisrt ID (the 1st post in a column of them). How can i get the exact post ID?
rand1026
 
Posts: 2
Joined: Tue May 16, 2017 10:11 pm

Re: How to get the exact post ID and pass it as json(.data)?

Postby rand1026 on Tue May 16, 2017 11:46 pm

i already found out what was wrong. That was how i passed the data-id param to json. And when in the js script (before ajax) i used var id = $(this).data("id"); -I got what i wanted. Now i can pass the post ID in json as {id: id, ..., ...,} to my controller action. works fine.
rand1026
 
Posts: 2
Joined: Tue May 16, 2017 10:11 pm


Return to Zend Framework

Who is online

Users browsing this forum: No registered users and 4 guests