POST not passing the selected data when button is clicked
I have simplified the code to try figure this one out. Ive got a form/table that displays a list of items added to a users cart with buttons to remove each corresponding item however when any button is clicked the last item in the list is being passed to the $itemid variable instead of the selected item.
The $itemid is correctly displaying a diffrent itemid on each button in the table and ive got it to echo above the button as well but it just wont pass the selected itemid to the POST function when the button is clicked and i cant for the life of me figure out why.
The form code..
<form method="post" action="remove.php">
<table border="0" width="100%" class="main" cellpadding="4" cellspacing="0" align="center">
<tr> <td class="colhead" width="1%" align="left">Item</td> <td class="colhead" width="1%"> <center>Delete</center></td> </tr>
<?php while ($row = mysql_fetch_row($res)):
$itemid = $row[0];
$dispname = $row[1]; ?>
<tr align="center">
<?php print("<td align='left' class='table_col1'>$dispname</td>"); ?>
<td class="table_col2" align="center">
<?php echo $itemid; ?>
<input type="submit" class="button" name="remove" value="Delete
(<?php echo $itemid;?>)"
</td></tr>
<?php endwhile; ?>
</table></form>
POST form code..
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
}
begin_frame();
echo $itemid;
end_frame();
}
If i move the brace after $itemid = $row[0] to below the last brace in the above section of the code it then passes the itemid of the first listed item of the table instead.
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
begin_frame();
echo $itemid;
end_frame();
}
}
remove.php page code..
<?php
require_once("include/functions.php");
dbconn();
loggedinorreturn();
stdhead();
begin_frame();
$userid = ( int ) $USER['id'];
$qry = ("SELECT
carts.itemid,
items.name
FROM carts
INNER JOIN users ON carts.userid = users.id
INNER JOIN items ON carts.itemid = items.id
WHERE users.status = 'yes'
AND carts.userid = '$userid'
ORDER BY name DESC") or sqlerr(__FILE__, __LINE__);
$res = mysql_query($qry);
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
}
begin_frame();
echo $itemid;
end_frame();
}
if (mysql_num_rows($res) > 0):
?>
<style type="text/css">
<!--
.button {
width: 220px;
height: 20px;
text-align: center;
background-image: url(images/btn.jpg);
background-color: #000000;
border-color: 3E3C32;
border-width: 1;
color: FFFFFF;
font-size: 8pt;
cursor: pointer;
}
-->
</style>
<form method="post" action="remove.php">
<table border="0" width="100%" class="main" cellpadding="4" cellspacing="0" align="center">
<tr>
<td class="colhead" width="1%" align="left">Item</td>
<td class="colhead" width="1%">
<center>Delete</center></td>
</tr>
<?php
while ($row = mysql_fetch_row($res)):
$itemid = $row[0];
$dispname = $row[1];
?>
<tr align="center">
<?php
print("<td align='left' class='table_col1'>$dispname</td>"); ?>
<td class="table_col2" align="center">
<?php echo $itemid; ?>
<input type="submit" class="button" name="remove" value="Delete
(<?php echo $itemid;?>)"
</td>
</tr>
<?php endwhile; ?>
</table>
</form>
<?php
echo "<br />";
endif;
end_frame();
stdfoot();
die;
{
stdhead("Nothing Found");
begin_frame("Nothing Found");
echo '<div style="margin-top:10px; margin-bottom:10px" align="center"><font size="2">Sorry, nothing found!</font></div>';
end_frame();
stdfoot();
}
?>
What would be advised to have the button pass in POST the corresponding $itemid of each row correctly I dont think POST is getting the $itemid and instead is using the last $itemid in the table via the while part of the code which is odd as it works to display each item in the table.
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
php forms variables post
add a comment |
I have simplified the code to try figure this one out. Ive got a form/table that displays a list of items added to a users cart with buttons to remove each corresponding item however when any button is clicked the last item in the list is being passed to the $itemid variable instead of the selected item.
The $itemid is correctly displaying a diffrent itemid on each button in the table and ive got it to echo above the button as well but it just wont pass the selected itemid to the POST function when the button is clicked and i cant for the life of me figure out why.
The form code..
<form method="post" action="remove.php">
<table border="0" width="100%" class="main" cellpadding="4" cellspacing="0" align="center">
<tr> <td class="colhead" width="1%" align="left">Item</td> <td class="colhead" width="1%"> <center>Delete</center></td> </tr>
<?php while ($row = mysql_fetch_row($res)):
$itemid = $row[0];
$dispname = $row[1]; ?>
<tr align="center">
<?php print("<td align='left' class='table_col1'>$dispname</td>"); ?>
<td class="table_col2" align="center">
<?php echo $itemid; ?>
<input type="submit" class="button" name="remove" value="Delete
(<?php echo $itemid;?>)"
</td></tr>
<?php endwhile; ?>
</table></form>
POST form code..
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
}
begin_frame();
echo $itemid;
end_frame();
}
If i move the brace after $itemid = $row[0] to below the last brace in the above section of the code it then passes the itemid of the first listed item of the table instead.
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
begin_frame();
echo $itemid;
end_frame();
}
}
remove.php page code..
<?php
require_once("include/functions.php");
dbconn();
loggedinorreturn();
stdhead();
begin_frame();
$userid = ( int ) $USER['id'];
$qry = ("SELECT
carts.itemid,
items.name
FROM carts
INNER JOIN users ON carts.userid = users.id
INNER JOIN items ON carts.itemid = items.id
WHERE users.status = 'yes'
AND carts.userid = '$userid'
ORDER BY name DESC") or sqlerr(__FILE__, __LINE__);
$res = mysql_query($qry);
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
}
begin_frame();
echo $itemid;
end_frame();
}
if (mysql_num_rows($res) > 0):
?>
<style type="text/css">
<!--
.button {
width: 220px;
height: 20px;
text-align: center;
background-image: url(images/btn.jpg);
background-color: #000000;
border-color: 3E3C32;
border-width: 1;
color: FFFFFF;
font-size: 8pt;
cursor: pointer;
}
-->
</style>
<form method="post" action="remove.php">
<table border="0" width="100%" class="main" cellpadding="4" cellspacing="0" align="center">
<tr>
<td class="colhead" width="1%" align="left">Item</td>
<td class="colhead" width="1%">
<center>Delete</center></td>
</tr>
<?php
while ($row = mysql_fetch_row($res)):
$itemid = $row[0];
$dispname = $row[1];
?>
<tr align="center">
<?php
print("<td align='left' class='table_col1'>$dispname</td>"); ?>
<td class="table_col2" align="center">
<?php echo $itemid; ?>
<input type="submit" class="button" name="remove" value="Delete
(<?php echo $itemid;?>)"
</td>
</tr>
<?php endwhile; ?>
</table>
</form>
<?php
echo "<br />";
endif;
end_frame();
stdfoot();
die;
{
stdhead("Nothing Found");
begin_frame("Nothing Found");
echo '<div style="margin-top:10px; margin-bottom:10px" align="center"><font size="2">Sorry, nothing found!</font></div>';
end_frame();
stdfoot();
}
?>
What would be advised to have the button pass in POST the corresponding $itemid of each row correctly I dont think POST is getting the $itemid and instead is using the last $itemid in the table via the while part of the code which is odd as it works to display each item in the table.
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
php forms variables post
1
Please stop usingmysql_*functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy.
– Jay Blanchard
Nov 20 '18 at 12:58
1
Little Bobby says your script is at risk for SQL Injection Attacks.. Even escaping the string is not safe!
– Jay Blanchard
Nov 20 '18 at 12:58
You don't seem to have any input field (nameditemid) in your form with the value of$itemid. How/where do you check that$_POSTdoesn't contain$itemid?
– kerbholz
Nov 20 '18 at 13:01
I will upgrade the scripts to MySQLi, im still learning but thanks for the links Jay. What would be advised to have the button POST the corresponding $itemid of each row correctly ?
– Abarratt
Nov 20 '18 at 23:50
add a comment |
I have simplified the code to try figure this one out. Ive got a form/table that displays a list of items added to a users cart with buttons to remove each corresponding item however when any button is clicked the last item in the list is being passed to the $itemid variable instead of the selected item.
The $itemid is correctly displaying a diffrent itemid on each button in the table and ive got it to echo above the button as well but it just wont pass the selected itemid to the POST function when the button is clicked and i cant for the life of me figure out why.
The form code..
<form method="post" action="remove.php">
<table border="0" width="100%" class="main" cellpadding="4" cellspacing="0" align="center">
<tr> <td class="colhead" width="1%" align="left">Item</td> <td class="colhead" width="1%"> <center>Delete</center></td> </tr>
<?php while ($row = mysql_fetch_row($res)):
$itemid = $row[0];
$dispname = $row[1]; ?>
<tr align="center">
<?php print("<td align='left' class='table_col1'>$dispname</td>"); ?>
<td class="table_col2" align="center">
<?php echo $itemid; ?>
<input type="submit" class="button" name="remove" value="Delete
(<?php echo $itemid;?>)"
</td></tr>
<?php endwhile; ?>
</table></form>
POST form code..
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
}
begin_frame();
echo $itemid;
end_frame();
}
If i move the brace after $itemid = $row[0] to below the last brace in the above section of the code it then passes the itemid of the first listed item of the table instead.
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
begin_frame();
echo $itemid;
end_frame();
}
}
remove.php page code..
<?php
require_once("include/functions.php");
dbconn();
loggedinorreturn();
stdhead();
begin_frame();
$userid = ( int ) $USER['id'];
$qry = ("SELECT
carts.itemid,
items.name
FROM carts
INNER JOIN users ON carts.userid = users.id
INNER JOIN items ON carts.itemid = items.id
WHERE users.status = 'yes'
AND carts.userid = '$userid'
ORDER BY name DESC") or sqlerr(__FILE__, __LINE__);
$res = mysql_query($qry);
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
}
begin_frame();
echo $itemid;
end_frame();
}
if (mysql_num_rows($res) > 0):
?>
<style type="text/css">
<!--
.button {
width: 220px;
height: 20px;
text-align: center;
background-image: url(images/btn.jpg);
background-color: #000000;
border-color: 3E3C32;
border-width: 1;
color: FFFFFF;
font-size: 8pt;
cursor: pointer;
}
-->
</style>
<form method="post" action="remove.php">
<table border="0" width="100%" class="main" cellpadding="4" cellspacing="0" align="center">
<tr>
<td class="colhead" width="1%" align="left">Item</td>
<td class="colhead" width="1%">
<center>Delete</center></td>
</tr>
<?php
while ($row = mysql_fetch_row($res)):
$itemid = $row[0];
$dispname = $row[1];
?>
<tr align="center">
<?php
print("<td align='left' class='table_col1'>$dispname</td>"); ?>
<td class="table_col2" align="center">
<?php echo $itemid; ?>
<input type="submit" class="button" name="remove" value="Delete
(<?php echo $itemid;?>)"
</td>
</tr>
<?php endwhile; ?>
</table>
</form>
<?php
echo "<br />";
endif;
end_frame();
stdfoot();
die;
{
stdhead("Nothing Found");
begin_frame("Nothing Found");
echo '<div style="margin-top:10px; margin-bottom:10px" align="center"><font size="2">Sorry, nothing found!</font></div>';
end_frame();
stdfoot();
}
?>
What would be advised to have the button pass in POST the corresponding $itemid of each row correctly I dont think POST is getting the $itemid and instead is using the last $itemid in the table via the while part of the code which is odd as it works to display each item in the table.
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
php forms variables post
I have simplified the code to try figure this one out. Ive got a form/table that displays a list of items added to a users cart with buttons to remove each corresponding item however when any button is clicked the last item in the list is being passed to the $itemid variable instead of the selected item.
The $itemid is correctly displaying a diffrent itemid on each button in the table and ive got it to echo above the button as well but it just wont pass the selected itemid to the POST function when the button is clicked and i cant for the life of me figure out why.
The form code..
<form method="post" action="remove.php">
<table border="0" width="100%" class="main" cellpadding="4" cellspacing="0" align="center">
<tr> <td class="colhead" width="1%" align="left">Item</td> <td class="colhead" width="1%"> <center>Delete</center></td> </tr>
<?php while ($row = mysql_fetch_row($res)):
$itemid = $row[0];
$dispname = $row[1]; ?>
<tr align="center">
<?php print("<td align='left' class='table_col1'>$dispname</td>"); ?>
<td class="table_col2" align="center">
<?php echo $itemid; ?>
<input type="submit" class="button" name="remove" value="Delete
(<?php echo $itemid;?>)"
</td></tr>
<?php endwhile; ?>
</table></form>
POST form code..
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
}
begin_frame();
echo $itemid;
end_frame();
}
If i move the brace after $itemid = $row[0] to below the last brace in the above section of the code it then passes the itemid of the first listed item of the table instead.
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
begin_frame();
echo $itemid;
end_frame();
}
}
remove.php page code..
<?php
require_once("include/functions.php");
dbconn();
loggedinorreturn();
stdhead();
begin_frame();
$userid = ( int ) $USER['id'];
$qry = ("SELECT
carts.itemid,
items.name
FROM carts
INNER JOIN users ON carts.userid = users.id
INNER JOIN items ON carts.itemid = items.id
WHERE users.status = 'yes'
AND carts.userid = '$userid'
ORDER BY name DESC") or sqlerr(__FILE__, __LINE__);
$res = mysql_query($qry);
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
}
begin_frame();
echo $itemid;
end_frame();
}
if (mysql_num_rows($res) > 0):
?>
<style type="text/css">
<!--
.button {
width: 220px;
height: 20px;
text-align: center;
background-image: url(images/btn.jpg);
background-color: #000000;
border-color: 3E3C32;
border-width: 1;
color: FFFFFF;
font-size: 8pt;
cursor: pointer;
}
-->
</style>
<form method="post" action="remove.php">
<table border="0" width="100%" class="main" cellpadding="4" cellspacing="0" align="center">
<tr>
<td class="colhead" width="1%" align="left">Item</td>
<td class="colhead" width="1%">
<center>Delete</center></td>
</tr>
<?php
while ($row = mysql_fetch_row($res)):
$itemid = $row[0];
$dispname = $row[1];
?>
<tr align="center">
<?php
print("<td align='left' class='table_col1'>$dispname</td>"); ?>
<td class="table_col2" align="center">
<?php echo $itemid; ?>
<input type="submit" class="button" name="remove" value="Delete
(<?php echo $itemid;?>)"
</td>
</tr>
<?php endwhile; ?>
</table>
</form>
<?php
echo "<br />";
endif;
end_frame();
stdfoot();
die;
{
stdhead("Nothing Found");
begin_frame("Nothing Found");
echo '<div style="margin-top:10px; margin-bottom:10px" align="center"><font size="2">Sorry, nothing found!</font></div>';
end_frame();
stdfoot();
}
?>
What would be advised to have the button pass in POST the corresponding $itemid of each row correctly I dont think POST is getting the $itemid and instead is using the last $itemid in the table via the while part of the code which is odd as it works to display each item in the table.
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
php forms variables post
php forms variables post
edited Nov 21 '18 at 0:05
Abarratt
asked Nov 20 '18 at 12:45
AbarrattAbarratt
11
11
1
Please stop usingmysql_*functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy.
– Jay Blanchard
Nov 20 '18 at 12:58
1
Little Bobby says your script is at risk for SQL Injection Attacks.. Even escaping the string is not safe!
– Jay Blanchard
Nov 20 '18 at 12:58
You don't seem to have any input field (nameditemid) in your form with the value of$itemid. How/where do you check that$_POSTdoesn't contain$itemid?
– kerbholz
Nov 20 '18 at 13:01
I will upgrade the scripts to MySQLi, im still learning but thanks for the links Jay. What would be advised to have the button POST the corresponding $itemid of each row correctly ?
– Abarratt
Nov 20 '18 at 23:50
add a comment |
1
Please stop usingmysql_*functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy.
– Jay Blanchard
Nov 20 '18 at 12:58
1
Little Bobby says your script is at risk for SQL Injection Attacks.. Even escaping the string is not safe!
– Jay Blanchard
Nov 20 '18 at 12:58
You don't seem to have any input field (nameditemid) in your form with the value of$itemid. How/where do you check that$_POSTdoesn't contain$itemid?
– kerbholz
Nov 20 '18 at 13:01
I will upgrade the scripts to MySQLi, im still learning but thanks for the links Jay. What would be advised to have the button POST the corresponding $itemid of each row correctly ?
– Abarratt
Nov 20 '18 at 23:50
1
1
Please stop using
mysql_* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy.– Jay Blanchard
Nov 20 '18 at 12:58
Please stop using
mysql_* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy.– Jay Blanchard
Nov 20 '18 at 12:58
1
1
Little Bobby says your script is at risk for SQL Injection Attacks.. Even escaping the string is not safe!
– Jay Blanchard
Nov 20 '18 at 12:58
Little Bobby says your script is at risk for SQL Injection Attacks.. Even escaping the string is not safe!
– Jay Blanchard
Nov 20 '18 at 12:58
You don't seem to have any input field (named
itemid) in your form with the value of $itemid. How/where do you check that $_POST doesn't contain $itemid?– kerbholz
Nov 20 '18 at 13:01
You don't seem to have any input field (named
itemid) in your form with the value of $itemid. How/where do you check that $_POST doesn't contain $itemid?– kerbholz
Nov 20 '18 at 13:01
I will upgrade the scripts to MySQLi, im still learning but thanks for the links Jay. What would be advised to have the button POST the corresponding $itemid of each row correctly ?
– Abarratt
Nov 20 '18 at 23:50
I will upgrade the scripts to MySQLi, im still learning but thanks for the links Jay. What would be advised to have the button POST the corresponding $itemid of each row correctly ?
– Abarratt
Nov 20 '18 at 23:50
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393280%2fpost-not-passing-the-selected-data-when-button-is-clicked%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393280%2fpost-not-passing-the-selected-data-when-button-is-clicked%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please stop using
mysql_*functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy.– Jay Blanchard
Nov 20 '18 at 12:58
1
Little Bobby says your script is at risk for SQL Injection Attacks.. Even escaping the string is not safe!
– Jay Blanchard
Nov 20 '18 at 12:58
You don't seem to have any input field (named
itemid) in your form with the value of$itemid. How/where do you check that$_POSTdoesn't contain$itemid?– kerbholz
Nov 20 '18 at 13:01
I will upgrade the scripts to MySQLi, im still learning but thanks for the links Jay. What would be advised to have the button POST the corresponding $itemid of each row correctly ?
– Abarratt
Nov 20 '18 at 23:50