Issue passing data from view to controller in codeigniter - php

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

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.

Send "Button"-ID on Submit

I have an PHP page, which contains a form with some different input fields, e. g. day, month, year etc.. The form method is POST, only one non-editable field (The user ID) is sent via GET.
Of course, there is a "Submit"-Button, which triggers the form Action (PHP Script on Server).
The form tags contain a table with empty cells too. Now comes my question:
If the user clicks into one of the table cells, the form should be submitted, but additional to the regular form data the ID of the table cell should be transmitted too (If via POST or GET doesn't matter to me). How can I do that?
//Edit 2:
...
<form method="post" action="<?= DOMAIN?>/.../addUserTimetable.php?uid=<?= $user->getUserID() ?>">
<select id="day" name="day">
...
</select>
...
<input name="yearend" id="yearend" ...>
<button type="submit">...</button>
<table class="bordered">
<tr>
<th>Std.</th>
<th>Montag</th>
<th>Dienstag</th>
<th>Mittwoch</th>
<th>Donnerstag</th>
<th>Freitag</th>
</tr>
<?php
for($i=1; $i<13;$i++) {
echo "<tr>";
echo "<th>".$i. "</th>";
for($j=1;$j<6;$j++) {
echo "<td id='h".$i. "d".$j. "' onclick='???'></td>";
}
echo "</tr>";
}
?>
</table>
</form>
...
The server sided procession is fine, but I haven't got any ideas - even after two hours google - how I could transmit the cell id additionally.
That shouldn't be to hard. Have a look at the following example:
<form>
<input type="text" name="something">
<table>
<tr>
<td><input type="submit" name="cel1">
</tr>
<tr>
<td><input type="submit" name="cel2">
</tr>
<tr>
<td><input type="submit" name="cel13">
</tr>
</table>
<input type="submit" value="save">
</form>
By giving the submit buttons in the table cells a name attribute, that name will also be present as a key on the $_REQUEST. Go ahead and var_dump the $_REQUEST and you'll see you can find out in the backend which button got pushed by checking which key exists.
Note that POST / GET is completely irrelevant here, both will work just the same. And obviously you could apply some css to those buttons to make them transparent and lay them on top of the table cells, so they don't look like buttons, but just "capture" the user's click.
One last side note, are you sure you want to send the userID as a GET parameter? That would be very easy for someone with bad intentions to manipulate. Consider not sending the ID at all, but keeping it in the session on the server.

Input hidden doesn't work properly

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. :-)

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>");

How to generate a json array from a table data inside a <td> tag

I am trying to get those data in the JSON format. Would you please help me to generate that? Please help I am not very familiar with jquery and php.
html code looks like this
<form>
<table>
<tr>
<td>//some code for other table elements</td><tr>
<tr>
<td>Critical Times (Coverage): </td>
<td rowspan="3" colspan="2">
<div>
<table width="100%" bgcolor="white" border="0px" name="criticalTime" id="criticalTime">
</table>
</div>
</td>
<td>
<input type="button" id="timeAdd" value="+"/><br>
<input type="button" id="timeRemove" value="-"/>
</td>
</tr>
<tr>
<td>//some code for other table elements</td>
</tr>
</table>
</form>
my .js file has the following to handle the addition of rows to the table when click the + button.
var rowCountr = 0;
var timeTable="<thead><tr bgcolor='E1ECFF'><th></th><th align='left'>Start Time</th><th align='left'>End Time</th></tr></thead>";
/* insert the html string*/
$("#criticalTime").html( timeTable );
/*Event for the +(Add) button in critical time table*/
var timeRwcntr=0;
timeRow="<tr><td><input type='checkbox' id='row'</td><td><input type='text' id='start' /></td><td><input type='text' id='end'/></td></tr>";
$('#timeAdd').click(function(){
$('#criticalTime').append(timeRow);
rowCountr++;
timeRwcntr++;
});
$('#timeRemove').click(function(){
$('#criticalTime tr:last-child').remove();
});
When I click the submit button from main form I need to get those values entered in the input fields and generate an JSON array to store those in database. Please help.
For this problem, I think that JSON may be overkill. You're just trying to send your table's input fields back to the server on a postback right? When you create your table rows, add a uniquely identifying number in the "name" attribute of your form controls <input name="'.$id.'_field" /> for example if you are rendering the table server-side; the code would be similar for javascript. Then, when you post back, you can loop through each row by counting and then access the POST variable you got back by referencing the name. Example:
for ($i = 0; $i < $rowcount; $i++) {
$field_value = $_POST[$i.'_field'];
...
}
JSON is great for more complex data sets, but in your case it sounds like you have structured two-dimensional rows and columns.

Categories