Input hidden doesn't work properly - php

I'm developing a simple PHP page, this is a small part of my code that doesn't work properly.
I want to use DB and print some products and for each product, I want to show a "buy" button.
If I press this button, a hidden input must be set with the product id(which later has to be sent to another page).
But, if I use var_dump to control(if my data was correct), I can see that the ID is wrong (I see that its always shown in the last)
<form id="products_list" method="post" action="step2.php">
<table>
<tr>
<td align="center">Immagine</td>
<td align="center">Nome prodotto</td>
<td align="center">Descrizione prodotto</td>
<td align="center">Prezzo unitario</td>
<td align="center">Taglie disponibili</td>
<td align="center">Colori disponibili</td>
<td align="center">Nickname disponibili</td>
</tr>
<?php
$product = mysqli_query($mysqli, "SELECT * FROM products");
while ($row = mysqli_fetch_array($product)) {
$id = $row[id_products];
print("
<tr>
<td align=\"center\">".$row[img_products]."</td>
<td align=\"center\">".$row[name_products]."</td>
<td align=\"center\">".$row[description_products]."</td>
<td align=\"center\">".$row[price_products]."</td>
<td align=\"center\">".$row[size_products]."</td>
<td align=\"center\">".$row[color_products]."</td>
<td align=\"center\">".$row[nick_products]."</td>
<input type=\"hidden\" name=\"id_products\" value=\"".$id."\"/>
<td><input type=\"submit\" name=\"buy\" value=\"Acquista\"/></td>
</tr>");
}
?>
</table>
</form>

You should open and close your form within the while-loop.
EDIT
A little bit more explanation why you should open and close from within the while loop:
If the while-loop is inside the form tags, that means the hidden field is outputted multiple times within the same form. Since they are all named the same, you're only retrieving one value after the submit (the value of the last hidden input).
If you open and close the form within the while-loop, each hidden input and button are in their own form. Which means, when that form gets submitted, you're only retrieving the value of that specific hidden field. :-)

Related

When submitting this button inside a datatable doesnt submit the right row id

I have a dynamic table which is set inside a foreach, so for each item of the array fetched create a new row. I have in the last column a button for each row. When clicking that submit button I am suppose to receive the id of that in PHP. Submission is being done correctly, but I am receiving the wrong id in PHP. Its basically taking the last id of the array when submitting. Any idea why?
Here is the table:
<form method="post" id="frm-example" action="<?php echo $_SERVER["PHP_SELF"] . '?' . e(http_build_query($_GET)); ?>">
<table id="example" class="display compact">
<thead>
<th>Device</th>
<th>Sales date</th>
<th>Client comments</th>
<th>Breakage count</th>
</thead>
<tbody>
<?php foreach ($arr_cases_devices as $cases) { ?>
<tr>
<td>
<?php echo $cases['name']; ?>
</td>
<td>
<?php echo $cases["sales_date"]; ?>
</td>
<td>
<?php echo $cases["dev_comment"]; ?>
</td>
<td>
<input type="hidden" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>" />
<button type="submit" name="see_rma">See RMA</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
When clicking on see_rma this is what I receive in PHP:
if (isset($_POST['see_rma'])) {
$selected_dev = e($_POST['device_id_breakage']);
print_r($selected_dev); // prints the "Dev_Id" of the last row, not of the row clicked
}
If I try printing $cases["Dev_Id"]; inside loop in the table, it prints perfectly fine, so it prints the Dev_Id of each row correctly. So, that means there is nothing wrong with the array or data. I don't why is this happening but it's for sure the first time I am having this issue.
I do this in many other tables but for some reasons in this one its not working properly.
You have multiple <input> elements with the same name within your form, and all of them are going to be submitted when you submit the form, but PHP can only get one of them. That's why you end up with only the last one in $_POST.
It looks like you should be able to fix this by just moving some attributes from the hidden input into the button (replacing the hidden input).
<button type="submit" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>">
See RMA
</button>
Only the button that was clicked will be submitted. Note that after changing the name of the button, you won't have see_rma in $_POST any more, so if you have any code that depends on that you'll need to change it to look for the other name instead.

Issue passing data from view to controller in codeigniter

I am using the CodeIgniter framework to build a site.
I currently have a view which prints all the unverified members into a table and also puts a button for each member which can be clicked to Verify that account.
Here is the code for the view
<h2>Admin Control Panel</h2>
<?php
/*
* First Output Members to be verified in tables
*/
//check if there is actually any data to print first.
echo ("<h3> Members to be Verified</h3>");
if(empty($unverified_members))
{
echo "<h4>There are currently no members to be verified </h4>";
}
else
{
$count_unverified_members=count($unverified_members);
echo "<table border='1'>";
//print table headers
echo <<<HTML
<th><div align ="center">ID Number</div></th>
<th><div align ="center">Username</div></th>
<th><div align ="center">E-Mail Address</div></th>
<th><div align ="center">Verify Account</div></th>
HTML;
for($arr_counter=0;$arr_counter<$count_unverified_members;$arr_counter++)
{
$current_member_id=$unverified_members[$arr_counter]->member_id;
$current_username=$unverified_members[$arr_counter]->username;
$current_email=$unverified_members[$arr_counter]->email;
//now print values
echo<<< HTML
<tr>
<td><div align ="center">$current_member_id</div></td>
<td><div align ="center">$current_username</div></td>
<td><div align ="center">$current_email</div></td>
<td><form action="Verify" method="post"><div align ="center"><input type="submit" value="Verify" /></div></form></td>
</tr>
HTML;
}
echo "</table><br><br>";
}
?>
I am trying to pass to my controller the user to be verified.
In the second heredoc I have this bit of code
<td><form action="Verify" method="post"><div align ="center"><input type="submit" value="Verify" /></div></form></td>
I want to for that user submit the member_id of that user and also a flag of some description which would mean that the user with that member_id needs to be verified.
So in essence
Print users needing verfication into table->click verify for particular user to be verified->pass to controller the user selected and a flag to indicate that user can be verified-> controller passes info to model-> model sets flag in the Database saying that user can be verified->model moves all users with that flag set to the verified_users table.
I can do all the controller/model bits myself. It's just getting the data from the view to the controller I am struggling with.
Any help would me much appreciated! If any of it makes sense!
you just need a hidden form field to send the ID -- using your code
<td><form action="members/verify" method="post">
<input type="hidden" name="memberid" value="<?php echo $current_member_id ?>" />
<div align ="center"><input type="submit" value="Verify" /></div>
</form>
</td>
in your controller you can then get the value of member id from the form like
$memberid = $this->input->post( 'memberid', TRUE ) ;
important point -- note that for this example i wrote the form action as "members/verify" . that means it will go to a controller named members, and the method named verify.
when you have time check out codeigniters table class - it can really help http://ellislab.com/codeigniter/user-guide/libraries/table.html
and the tutorial is a good reference for common tasks
http://ellislab.com/codeigniter/user-guide/tutorial/index.html

HTML form to update Mysql with PHP (and HTML)

I've been trying to develop a real estate page where people can add listings. I am new to the world of php mysql. I have been over this problem for over a day and can't figure out where the problem is.
I have a form where people can add data. That's good and working. Now I am starting to have a place where people can add / delete / update their info. I am trying to build this step by step.
This is where a user could pull the information. My problem is with the piece of the code:
edit_form.php?idBAR=$row[id].
Full code below.
<table>
<tr>
<td align="center">EDIT DATA</td>
</tr>
<tr>
<td>
<table border="1">
<?php
include"configS_OH.php";//database connection
$order = "SELECT * FROM braaasil_brokerstour.property";
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
echo ("<tr><td>$row[id]</td>");
echo ("<td>$row[address]</td>");
echo ("<td>$row[day]</td>");
echo ("<td>$row[hours]</td>");
echo ("<td>Edit</td></tr>");
}
?>
</table>
</td>
</tr>
</table>
Then this tutorial try to pass id through the address bar (I don't know much about php to actually say much)
It tries to upload the data into a new form where a person could edit info.
But I can't load the data into the new form. If I use where id=7, I get the info into the form. But this method of passing the info in the address bar like ?idBAR=8... and then try to catch it in the other code (where id=$idBAR), is not working.
Here is the code:
<table border=1>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<tr>
<td>
<table>
<?php
include "configS_OH.php";//database connection
print $database;
$order = "SELECT * FROM braaasil_brokerstour.property
WHERE id='$idBAR'";
print $idBAR;
$result = mysql_query($order) or die( mysql_error() );
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="idBAR" value="<?php echo "$row[id]"?>">
<tr>
<td>Address</td>
<td>
<input type="text" name="address"
size="20" value="<?php echo "$row[address]"?>">
</td>
</tr>
<tr>
<td>Date</td>
<td>
<input type="text" name="day" size="40"
value="<?php echo "$row[day]"?>">
</td>
</tr>
<tr>
<td>Time</td>
<td>
<input type="text" name="time" size="40"
value="<?php echo "$row[hours]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
I tried an tried and tried..
Thank you for your time in advance.
WHERE id='$idBAR'
You haven't assigned $idBAR any value. You need to read it from the $_GET array first:
$idBAR = $_GET['idBAR'];
You should, of course, check that this value exists first, and is acceptable.
I don't see anywhere you have actually used the GET data, just the reference name which is used in GET.
If you first query is working and is getting the $row['id'] value ok - you can verify this when you go to edit_form.php, in your browser URL bar at the top, does it say this:
edit_form.php?idBAR=7
(or whatever number should be there)
If so, then you just need to use the PHP GET. Your data is stored in $_GET[], and in this case, the reference name is idBAR. So your id from your previous page query is sent through the link into your URL, and on your edit_form.php page, you'd use that data as:
$_GET['idBAR']
You can use that, but personally I assign the data to a variable, such as:
$strGetId = $_GET['idBAR'];
Then you can use $strGetId throughout your code.
Also, check things like isset(), empty() etc, just so you know you are working with A) something is actually there, and B) it's not empty etc
if you are putting a variable directly in a string without concatenating, it can't be an array variable; you must concatenate those you also need to surr. so this
echo ("<td>Edit</td></tr>");
should be this
echo ("<td>Edit</td></tr>");
also, it looks like your form is sending data with POST. When you pass form data in the url string after the question mark, that is passing with get.
so...in your form where you want to use that variable, you set it up like this
$idBAR=$_GET['idBAR']; //to get the variable if it was part of the URL
$idBAR=$_POST['idBAR']; //if it was sent with post, as is the case with your form
also, request contains both get and post, so
$idBAR=$_REQUEST['idBAR'];
will work in either case.
The problem is the $row[id] is seen as text just like everything else. You want the value of $row[id]. Instead of
echo ("<td>Edit</td></tr>");
try
echo ("<td>Edit</td></tr>");

Form element closing too soon

I have a PHP script which generates a table with each row (supposedly) as a form. The table is generating very nicely, but for some reason the form tag is closing immediately. I have no idea why and it's making my cranky. Any ideas?
echo '
<form method="post" action="#" id="status_row_'.$row["name"].'">
<tr class="even">
<input type="hidden" name="category" id="category" value="'.$category[0]['category'].'">
<input type="hidden" name="error_level" id="error_level" value="'.$row['level'].'">
<input type="hidden" name="service_name" id="service_name" value="'.$row['name'].'">
<td class="status-icons" name="error_circle"><div class="'.$error_circle.'"></div></td>
//rows deleted here for clarity's sake
</tr></form>'
So the Inspect Element on the generated form shows: <form method="post" action="#" id="status_row_blahblah"></form> followed by all the rows.
Your html is broken. You're trying to open a form immediately after the opening <tr>, then closing it AFTER the closing <tr>. That's illegal html. <table> structures cannot be built like that. It should probably be something more like
<form>
<table>
<tr>
<td>...</td>
</tr>
</table>
</form>
Essentially you're trying to stuff your form into the void that exists between <tr> and <td> tags. Nothing should be there except the table structure:
<tr><p>hi there</p><td>go away</td></tr>
is illegal, most browsers will render the <p> OUTSIDE the table, usually before the opening <table> tag. That's why your form is showing up where it is - the browser is attempting to do the best it can with your broken html.
Are the forms different? Or linked together in some way (i.e. is each table row it's own UNIQUE form with, or is it one giant form with different fields in every row?
If it's the former, then create a form inside each ROW element like so:
<table>
<tr>
<td><form>FORM STUFF GOES HERE</FORM></td>
</tr>
<tr>
<td><form>FORM STUFF GOES HERE</FORM></td>
</tr>
<tr>
<td><form>FORM STUFF GOES HERE</FORM></td>
</tr>
</table>
If it's the latter, or if you only want one form submitted overall, then create the form tags outside the table, and have the form fields within the data cells:
<form>
<table>
<tr>
<td>FORM FIELD E.G. TEXT FIELD</td>
</tr>
<tr>
<td>FORM FIELD E.G. TEXT FIELD</td>
</tr>
<tr>
<td>FORM FIELD E.G. TEXT FIELD</td>
</tr>
</table>
</form>
It looks like you are closing the <tr> tag before closing the <form> tag.
The form element was closing incorrectly because, as some people pointed out, you apparently can't have <form> tags in between <tr> and <td> tags (I don't understand why, but it is what it is).
I found this solution as a workaround:
http://federmanscripts.com/2010/01/12/form-and-table-row-nesting-workaround/

IE-text input fields "clickable" region is only 1 pixel wide

I have a simple input form with input fields that are 3 characters wide. To gain focus to these fields however, I must click the very, very LEFT of the field, about 1 pixel or cursor width wide. The form works fine in Firefox and Safari, where you can click anywhere in the text input field to gain focus.
The form:
<form name=game_edit method=post action="?includepage=game_results.php&gameID=<?=$gameID?>" onsubmit="return validate_form();">
<table border="0" id=gameResults>
<tr><th colspan="4">RESULTS FOR: <?=$gamedate?> HOSTED BY <?=$hostName?></th></tr>
<tr>
<td colspan="2">PLAYERS: <input type=text name=playercount value="<?=$playercount?>" size="3"></td>
<td colspan="2">Championship money colleted: $<?=$champamount?></td>
</tr>
<tr><th>PLAYER</th><th>Place</th><th>Champ Discount</th><th>Didn't play</th></tr>
<? Player_list($gameID); ?>
<tr><td colspan="2"><input type="text" name="manualAdd" size="17" /></td><td colspan="2">Add a username who didn't RSVP</td></tr>
<input type=hidden name=gameID value="<?=$gameID?>">
<tr>
<td colspan="2"><input type=submit name='saveGameResults' value='SAVE CHANGES' style='width:100px;font-size:11px;'></td>
<td colspan="2" align="right"><input type=submit name='saveGameResults' value='LOCK GAME' style='width:100px;font-size:11px;'></td>
</tr>
</table>
The function that writes the fields:
function player_list($gameID)
//////////////////////////////////////////////
// Creates player list for specified //
// game. Provides input for game results. //
//////////////////////////////////////////////
{
//*****Get list of players for gameID*****
$sql = "SELECT rsvpid,rsvp.playerid,concat(fname,' ',lname) as playerName,place,points,freeChamp FROM rsvp LEFT JOIN players ON rsvp.playerid=players.id WHERE rsvp.gameID=$gameID ORDER BY place,lname";
$playerlist = mysql_query($sql);
if ($listrow = mysql_fetch_array($playerlist))
{
#--Get number of players who signed up on the website. This may differ from the actual player count --#
#--entered by the host. A player may have played but not be signed up on the website. --#
echo "<input type=hidden name='recordedPlayerCount' value='".mysql_num_rows($playerlist)."'>";
$i = 0; // index for form rows
do
{
foreach($listrow as $key=>$value) $$key = $value;
($freeChamp) ? $freeChampChecked = "checked" : $freeChampChecked = ""; //check the freechamp box if appropriate.
if ($place==100) $place="";
?>
<tr>
<td><?=$playerName?>:</td>
<td style="text-align:center"><input type=text size="2" name="place<?=$i?>" value="<?=$place?>"></td>
<td style="text-align:center"><input type=checkbox name="freeChamp<?=$i?>" value='true' <?=$freeChampChecked?>></td>
<td style="text-align:center"><input type=checkbox name="noShow<?=$i?>" value='true'></td>
<input type=hidden name="rsvpid<?=$i?>" value="<?=$rsvpid?>">
</tr>
<?
$i++; //increment index for next row
}while ($listrow = mysql_fetch_array($playerlist));
}else
echo "<tr><td colspan='4'>NO RECORDS FOUND FOR THIS GAME!</td></tr>";
}
?>
The offending form field is the "place" field...it is clickable only on the very left of the form field instead of the entire form field, in IE only. (IE 7, that I know of).
Help?
I am willing to bet that the culprit is setting the size of the input element to 2. Give this a shot and let me know if it works any better:
<td style="text-align:center"><input type=text size="3" maxlength="2" name="place<?=$i?>" value="<?=$place?>"></td>
There are other things that I notice in your code:
<tr><th>PLAYER</th><th>Place</th><th>Champ
Discount</th><th>Didn't play</th></tr>
<? Player_list($gameID); ?>
You call the function "Player_list()", but your function is named "player_list()".
foreach($listrow as $key=>$value) $$key = $value;
is the same as:
extract($listrow);
Thanks for the tips, Michael. Found the issue. I have a css-driven horizontal dropdown menu (http://www.udm4.com/) in a header div that drops down over the content div's. Above that, I have a javascript-driven login panel that can drop down over the header div. In playing around with the z-indexes so that everything played nice, I set the content div that holds the form to a z-index of -1. Even after taking out everything except that one div and that one form, IE doesn't like negative z-indexes. I don't know if negative z-indexes constitute valid CSS and IE is just being a tard again, or if other browsers are just too lenient.

Categories