/    Sign up×
Community /Pin to ProfileBookmark

How to pass SESSION values to table ?

I am under developing PHP add to cart without DB, so that i am using SESSION, actually get data from fetch_data.php data to my_cart.php using the POST method, successfully retun the values,

After receiving the post data how can i convert to display like a table.

workout:

fetch_data.php return values.

https://snag.gy/IASCMZ.jpg

& values received

https://snag.gy/ojWxHe.jpg

Here is my my_cart.php 😀

“`
// FYI -> Here i am using only two fields : voice_sku & voice_name

<table width=”100%” cellpadding=”6″ cellspacing=”0″>
<thead>
<tr>
<th>Voice Sku</th>
<th>Voice Name</th>
<th>Remove</th>
</tr>
</thead>

<tbody>

<?php
session_start();
$voice_sku = ”;
$voice_name = ”;

if(isset($_POST[‘voice_sku’])&& isset($_POST[‘voice_name’]))
{
// print_r($_POST);
// die();

$voice_sku = $_POST[‘voice_sku’];
$voice_name = $_POST[‘voice_name’];
$_SESSION[‘voice_sku’] = $voice_sku;
$_SESSION[‘voice_name’] = $voice_name;

}
var_dump($_SESSION);
?>

<tr>
<td colspan=”5″>
<span style=”float:right;text-align: right;”>
<!– –>
</span>
</td>
</tr>
<tr>
<td colspan=”5″>
<a href=”index.php” class=”button”>Add More Items</a>
<button type=”submit”>Update</button>
</td>
</tr>

</tbody>
</table>
“`

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJul 10.2019 — You are so difficult to understand. But I will ask this. YOu are showing us an html table layout but you only have one td tag in it. Why not two more td tags showing the two inputs you received? Isn't that what you are asking about???

PLEASE - stop mixing your html code in with your php code. It makes no sense to keep doing this even after being told how inappropriate it is.
Copy linkTweet thisAlerts:
@Aveeva7authorJul 10.2019 — @ginerjm#1606113 Let me try you to understand, my fetch_data.php pass the values vaoice_sku and voice_name to my_cart.php, here i am not using mysql so that i am using session, here i jusr trying to get values using SESSION,

``<i>
</i>if(isset($_POST['voice_sku'])&amp;&amp; isset($_POST['voice_name']))
{
// print_r($_POST);
// die();

$voice_sku = $_POST['voice_sku'];
$voice_name = $_POST['voice_name'];
$_SESSION['voice_sku'] = $voice_sku;
$_SESSION['voice_name'] = $voice_name;


}
var_dump($_SESSION); <i>
</i>
`</CODE>

The values also get from fetch_data.php successfully, my question is how to pass my session values to the table and look like,

<CODE>
`<i>
</i>Voice_sku voice_name edit
data data data<i>
</i>
``
Copy linkTweet thisAlerts:
@ginerjmJul 10.2019 — See my post in the other forum you used.
Copy linkTweet thisAlerts:
@siddhi_patelJul 11.2019 — Hello, try like this may help you:



if(isset($_POST['voice_sku'])&& isset($_POST['voice_name']))

{


$voice_sku = $_POST['voice_sku'];
$voice_name = $_POST['voice_name'];
$_SESSION['voice_sku'] = $voice_sku;
$_SESSION['voice_name'] = $voice_name;


}
var_dump($_SESSION);
Copy linkTweet thisAlerts:
@Aveeva7authorJul 11.2019 — @siddhi_patel#1606155 Yes, its working, but how can i display values like table with remove option? FYI -> https://snag.gy/Qsfm18.jpg
Copy linkTweet thisAlerts:
@ginerjmJul 11.2019 — Are you TRYING to say that you want to have a button on each row to remove that row of data from the table it came from?
Copy linkTweet thisAlerts:
@Aveeva7authorJul 15.2019 — @ginerjm#1606197 Yes, you are right? How to delete each row, my code

eg: https://jsfiddle.net/annoyingmouse/7c0v1ra3/

workout :

**manage_cart.php**
``<i>
</i>&lt;?php
session_start();

if ($_SERVER['REQUEST_METHOD']=='POST') {
$voice_sku = $_POST['voice_sku'] ?? 0;
$voice_name = $_POST['voice_name'] ?? '';

if ($voice_sku &amp;&amp; $voice_name) {
$_SESSION['playlist'][$voice_sku] = $voice_name;
exit(json_encode($_SESSION['playlist'])) ;

}
}
exit("ERROR") ;
?&gt;<i>
</i>
`</CODE>
<STRONG>**my_cart.php**</STRONG>

<CODE>
`<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script src="https://code.jquery.com/jquery-3.3.1.js"&gt;&lt;/script&gt;
&lt;script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"&gt;&lt;/script&gt;
&lt;script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"&gt;&lt;/script&gt;

&lt;!-- start codepen resource --&gt;

&lt;!-- &lt;script src="https://cdn.datatables.net/plug-ins/1.10.7/integration/bootstrap/3/dataTables.bootstrap.js"&gt;&lt;/script&gt;
&lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"&gt;&lt;/script&gt; --&gt;
&lt;script src="https://cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"&gt;&lt;/script&gt;
&lt;!-- end codepen resource --&gt;

&lt;link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css"/&gt;
&lt;link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css"/&gt;

&lt;!-- start codepen resource --&gt;
&lt;!-- &lt;link rel="stylesheet" href="https://cdn.datatables.net/plug-ins/1.10.7/integration/bootstrap/3/dataTables.bootstrap.css"/&gt;
&lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/&gt; --&gt;
&lt;!-- end codepen resource --&gt;



&lt;script&gt;

var table = $('#example').DataTable({
"columns": [
null,
null,
null,
{
"sortable": false
}
]
});

$('#example').on("click", "button", function(){
console.log($(this).parent());
table.row($(this).parents('tr')).remove().draw(false);
});

$(document).ready(function() {
$('#example').DataTable();
} );

&lt;/script&gt;

&lt;style type="text/css"&gt;
table {
border-collapse: collapse;
border: 1px solid black;
}
table td,th {
border: 1px solid black;
}
td {
text-align: center;
}
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;


&lt;h2&gt;Play Lists&lt;/h2&gt;

&lt;table id="example" class="table table-striped table-bordered" style="width:80%" align="center"&gt;

&lt;tr&gt;
&lt;th&gt;Voice SKU&lt;/th&gt;
&lt;th&gt;Voice Name&lt;/th&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;/tr&gt;

&lt;?php
session_start();
foreach ($_SESSION['playlist'] as $key =&gt; $value) {
echo "&lt;tr&gt;";
echo "&lt;td&gt;" . $key . "&lt;/td&gt;n&lt;td&gt;" . $value . "&lt;/td&gt;n&lt;td&gt;". $action ."&lt;/td&gt;";
echo "&lt;/tr&gt;";
}

?&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@ginerjmJul 15.2019 — Didn't bother to read thru ALL THAT CODE but....

I build my html table as part of my query results loop and add a last column to the row to hold the button(s) for that row. Each row (<tr>....</tr>) is wrapped by a form tags so that the buttons act upon the current row of data only. BTW as I a build that table I am NOT OUTPUTTING THE RESULTS. I merely assign then to one big variable and then I output that variable along with ALL THE REST OF MY HTML so that I keep my logic (php) separated from the presentation (html) code.
Copy linkTweet thisAlerts:
@Aveeva7authorJul 19.2019 — Anyone help me with my workout :

my_cart.php:

&lt;?php<br/>
header('Content-Type: text/html; charset=utf-8');<br/>
session_start();<br/>
session_regenerate_id();

<i> </i><CODE>echo '
<i> </i>&lt;!DOCTYPE html&gt;
<i> </i>&lt;html lang="en"&gt;
<i> </i> &lt;head&gt;
<i> </i> &lt;meta charset="utf-8"&gt;
<i> </i> &lt;metaname="viewport"content="width=device-width,height=device-height,initial-scale=1"&gt;
<i> </i> &lt;title&gt;Playlist&lt;/title&gt;
<i> </i> &lt;/head&gt;

<i> </i> &lt;body&gt;
<i> </i> &lt;form id="playlist" action="playlist_action.php" method="post"&gt;
<i> </i> &lt;table id="playlist"&gt;
<i> </i> &lt;caption&gt;Play List&lt;/caption&gt;
<i> </i> &lt;thead&gt;
<i> </i> &lt;tr&gt;
<i> </i> &lt;th scope="col"&gt;Voice SKU&lt;/th&gt;
<i> </i> &lt;th scope="col"&gt;Voice Name&lt;/th&gt;
<i> </i> &lt;th scope="col"&gt;Action&lt;/th&gt;
<i> </i> &lt;/tr&gt;
<i> </i> &lt;/thead&gt;

<i> </i> &lt;tbody&gt;';
<i> </i> foreach ($_SESSION['playlist'] as $key =&gt; $value)
<i> </i> {
<i> </i> $key = htmlspecialchars($key);
<i> </i> echo '
<i> </i> &lt;tr&gt;
<i> </i> &lt;th scope="row"&gt;', $key, '&lt;/th&gt;
<i> </i> &lt;td&gt;', htmlspecialchars($value), '&lt;/td&gt;
<i> </i> &lt;td&gt;&lt;button name="delete" value="', $key, '"&gt;Delete&lt;/button&gt;&lt;/td&gt;
<i> </i> &lt;/tr&gt;';
<i> </i> }
<i> </i> echo '
<i> </i> &lt;/tbody&gt;
<i> </i> &lt;/table&gt;
<i> </i> &lt;/form&gt;
<i> </i> &lt;/body&gt;
<i> </i>&lt;/html&gt;';


and

playlist_action.php

<br/>
&lt;?php<br/>
header('Content-Type: text/html; charset=utf-8');<br/>
session_start();<br/>
session_regenerate_id();

<i> </i><CODE>if (array_key_exists('playlist_action', $_POST)) {
<i> </i> switch ($_POST['playlist_action']) {
<i> </i> case 'delete':
<i> </i> if (array_key_exists($_POST['delete'], $_SESSION['playlist']) {
<i> </i> unset($_SESSION['playlist'][$_POST['delete']]);
<i> </i> // notify user of successful delete
<i> </i> } else {
<i> </i> // notify user of non-existent key in the playlist
<i> </i> }
<i> </i> default:
<i> </i> // handle invalid/unknown action here.
<i> </i> }
<i> </i>}
<i> </i>?&gt;


my output:

**|Parse error: syntax error, unexpected 'unset' (T_UNSET) in C:wampwwwvoice_bankplaylist_action.php on line *10*|**
×

Success!

Help @Aveeva7 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...