Pass user_id in a table to another page - php

I have a list of users that I'm reporting in a table. The first column contains the user_id, and I want to pass that to another page that displays the users details. I don't want to pass it through the URL with $_GET. How do I do this? I can use only HTML & PHP. One post I saw seemed like it was on the right track, but I can't get it to work. Here's what I have:
<h1>Maintain Users</h1>
<div id="ir-report">
<table class="table">
<tr class = "table tr">
<th class="table th">User Id</th>
<th>Username</th>
<th>Name</th>
<th>Email</th>
<th>Activated?</th>
<th>Type</th>
<th>Account Nbr</th>
</tr>
<?php while ($row = mysqli_fetch_assoc($query))
{?>
<tr class="table tr">
<td class ="table td" onclick="submit_id($row['user_id'])"><img src="images\editpensil.jpg" alt="UserId" width="25" height="25"></td>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['first_name'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['active'] ?></td>
<td><?php echo $row['type'] ?></td>
<td><?php echo $row['acct_nbr'] ?></td>
</tr>
<?php } ?>
</table>
</div>
What I was expecting to happen is that "on click" the function submit_id($var) would be called. To test that this is working, my function is very simple for the moment:
function submit_id($var) {
header('Location: index.php');
exit();
}
But nothing happens when I clime on the edit icon (editpensil.jpg). The application doesn't appear to try and execute anything. What am I missing? -- Thanks.

You can't call a PHP function from an onclick attribute, or any other on attribute. Those functions must be Javascript functions.
If you really need/want to not pass the id through GET, you can wrap the table in a <form> with the action set to the page you want to send it to, with a method of POST and a hidden input field. I know your question says you can only use HTML and PHP; without Javascript, the only way you can really do this is to add a button to the form/table. You could use CSS to style it so it looks like a link, or anything not-button like.
Example without Javascript:
<form action="index.php" method="post">
<tr class="table tr">
<td class ="table td">
<img src="images\editpensil.jpg" alt="UserId" width="25" height="25">
</td>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['first_name'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['active'] ?></td>
<td><?php echo $row['type'] ?></td>
<td><?php echo $row['acct_nbr'] ?></td>
<td><input type="submit" value="Link to" /></td>
</tr>
<input type="hidden" name="user_id" value="<?php echo $row['user_id'] ?>" />
</form>
When that button is clicked, the form will be posted to index.php and you can get to the id through $_POST['user_id']
If you are willing and able to use Javascript, you should be able to do the following HTML/PHP:
<form action="index.php" method="post" id="user_form_<?php echo $row['user_id'] ?>">
<tr class="table tr">
<td class ="table td user_id_link" data-userid="<?php echo $row['user_id'] ?>">
<img src="images\editpensil.jpg" alt="UserId" width="25" height="25">
</td>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['first_name'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['active'] ?></td>
<td><?php echo $row['type'] ?></td>
<td><?php echo $row['acct_nbr'] ?></td>
</tr>
<input type="hidden" name="user_id" value="<?php echo $row['user_id'] ?>" />
</form>
And the following Javascript, although this will only work in IE9+:
var links = document.getElementsByClassName('user_id_link');
for (link in links) {
link.addEventListener('click', function() {
var formId = 'user_form_' + this.userid;
document.getElementById(formId).submit();
}, false);
}
Or the much cleaner jQuery version, which has better browser compatibility and is really an invaluable toolkit for a web developer:
HTML/PHP:
<form action="index.php" method="post">
<tr class="table tr">
<td class ="table td user-id-link">
<img src="images\editpensil.jpg" alt="UserId" width="25" height="25">
</td>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['first_name'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['active'] ?></td>
<td><?php echo $row['type'] ?></td>
<td><?php echo $row['acct_nbr'] ?></td>
</tr>
<input type="hidden" name="user_id" value="<?php echo $row['user_id'] ?>" />
</form>
Javascript/jQuery:
$(function() {
$('.user-id-link').on('click', function() {
$(this).closest('form').submit();
});
});

You're mixing PHP and JS. The function called in an onclick handler must be JS, it cannot be php.
What's wrong with passing the id through the URL? That's quite obviously the only simple practical solution. Alternatives are:
using cookies, but that's a mess if a user opens multiple tabs/windows on your site, or wants to bookmark the page, or for a reopen after restart of the browser. And that really doesn't help much.
using some kind of other id that points to the user id, but that's just adding another layer for not much.
Unless you tell us exactly why you don't want to pass the id through the URL, it will be difficult to suggest anything.

Related

How to submit a value from a PHP defined table to a php page for further processing?

I am using a form to submit data to a php page and then presenting it as a table using the method which is working fine for me. Now I want to submit one of the data field ($row->id) to another php page for processing detailed user profile. I tried using the form method as shown in the code but it doesn't work and keeps submitting same value for every profile.
<tr>
<td><?php echo $n++; ?></td>
<td><?php echo $row->id; ?></td>
<td><?php echo $lang[$row->gender]; ?></td>
<td><?php echo $row->age; ?></td>
<td><?php echo $row->height; ?></td>
<td><?php echo $row->edn; ?></td>
<td><?php echo $lang[$row->income]; ?></td>
<td><form action= 'compact.php' method='post'>
<input type ='hidden' name='pid' value="<?php echo $row->id?>">
<input type='submit' name='submit' value='View Details'></td>
</tr>
Any help will be highly appreciated. Thanks
Based on suggestions in the comments to above question, I changed my script as follows and used GET instead of POST at compact.php and was able to successfully capture the posted variable for further processing:
<tr>
<td><?php echo $n++; ?></td>
<td><?php echo $row->id; ?></td>
<td><?php echo $lang[$row->gender]; ?></td>
<td><?php echo $row->age; ?></td>
<td><?php echo $row->height; ?></td>
<td><?php echo $row->edn; ?></td>
<td><?php echo $lang[$row->income]; ?></td>
<td><a href="compact.php?pid=<?php echo $row->id?>">
View details</a></td>
</tr>

HTML Checkbox cannot be checked

I have some checkbox in a table that contains the id of that row item. I want to allow the user to select multiple rows. However, I can't seem to check the checkbox on Chrome. I loaded the site up on my mobile and it works. I have tried to insert an onclick but it doesn't seem like the checkbox is registering any clicks to it as well. Please help.
The table
<?php echo form_open_multipart('Events/Two/Search');?>
<table class="table">
<thead>
<tr class="text-left">
<td></td>
<td>Name</td>
<td>Email</td>
<td>Phone Number</td>
<td>Address</td>
</tr>
</thead>
<tbody>
<?php
if(!empty($datatable)){
foreach ($datatable as $data){
?>
<tr>
<td><input type="checkbox" name="id[]" value="<?php echo $data->id; ?>"/></td>
<td><?php echo $data->first_name." ".$data->last_name; ?></td>
<td><?php echo $data->email; ?></td>
<td><?php echo $data->phone_number; ?></td>
<td><?php echo $data->address;?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<?php echo form_close(); ?>
EDIT: I have cleared my cache and cookies as well. It works on the mobile but not on chrome for some reason.
EDIT 2 : It works on Safari
EDIT 3 : If I place a checkbox on another place, I can check it. Just not in the table
<tbody>
<?php
if(!empty($datatable)){
foreach ($datatable->result() as $data){
?>
<tr>
<td><input type="checkbox" name="id[]" value="<?php echo $data->id; ?>"/></td>
<td><?php echo $data->first_name." ".$data->last_name; ?></td>
<td><?php echo $data->email; ?></td>
<td><?php echo $data->phone_number; ?></td>
<td><?php echo $data->address;?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
First Your query is not working it seems so please preview this code the use jquery append to add more row if you need help on that code will be below
Jquery on click ADD/REMOVE ROW please Visit the link below
https://jsfiddle.net/susanadhikary/omngzss8/8/

PHP MYSQL Table delete button not working

Here is the query statement that I have. For some reason it works for another table I have but doesn't work with this table. I checked phpmyadmin to see if I would create an error but there were zero errors.
$Store_ID = filter_input(INPUT_POST,'Store_ID',FILTER_VALIDATE_INT);
//DELETE the Store
$query="DELETE FROM store_location WHERE Store_ID = :Store_ID";
$statement2=$db->prepare($query);
$statement2->bindValue(':Store_ID',$Store_ID);
$statement2->execute();
$statement2->closeCursor();
Here is the table code for the Delete Button
<?php foreach ($stores as $s) : ?>
<tr>
<td><?php echo $s['Store_ID']; ?></td>
<td><?php echo $s['Loc_Street']; ?></td>
<td><?php echo $s['Loc_City']; ?></td>
<td><?php echo $s['Loc_State']; ?></td>
<td><?php echo $s['Rent']; ?></td>
<td><?php echo $s['License']; ?></td>
<td><?php echo $s['Inspect']; ?></td>
<td><form action="stores.php" method="post">
<input type ="hidden" name ="Store_ID" value ="<?php echo $s['Store_ID']; ?>"/>
<input type="button" name="delete" value="Delete"/>
</form></td>
</tr>
<?php endforeach; ?>
</table>

updating a textfield based on radio button selection

I have some 25 radio buttons, such that only one selection can be done. On selecting a particular radio-button, an ajax update is to be performed on the risk grade textfield of this form. And, this textfield should get uniquely updated on every radio-button selection.
How's this possible. Please, I am really stuck here.
I had done by normal radio button code for each one.
I used the ajax update as done in dependent dropdown textfield, but, I am not sure the same thing is appropriate here.
I think I am getting something wrong here. Any thing would be really helpful.
<table id="department_permissions_table" class="display">
<thead>
<tr style="background-color: lavender"><td style="background-color: white"></td><td><b>Effect</b></td>
<td></td><td></td>
<td></td><td></td></tr>
<tr>
<td style="background-color: lavender"><b>Likelihood of recurrence</b></td>
<td style="background-color: #f5f5f5"><b>Negligible</b></td>
<td style="background-color: #f5f5f5"><b>Minor</b></td>
<td style="background-color: #f5f5f5"><b>Moderate</b></td>
<td style="background-color: #f5f5f5"><b>Major</b></td>
<td style="background-color: #f5f5f5"><b>Catastrophic</b></td>
</tr>
</thead>
<tbody>
<tr>
<td style="background-color: #f5f5f5"><b>Almost certain</b></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>11,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>12,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>13,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>14,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>15,'uncheckValue'=>null)); ?></td>
</tr>
<tr>
<td style="background-color: #f5f5f5"><b>Likely</b></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>21,
'uncheckValue'=>null,
'ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('Cinvestigation/load'),
'update'=>'#risk_grade',
'data'=>array('region_id'=>'js:this.value'),
)
)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>22,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>23,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>24,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>25,'uncheckValue'=>null)); ?></td>
</tr>
<tr>
<td style="background-color: #f5f5f5"><b>Possible</b></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>31,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>32,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>33,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>34,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>35,'uncheckValue'=>null)); ?></td>
</tr>
<tr>
<td style="background-color: #f5f5f5"><b>Unlikely</b></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>41,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>42,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>43,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>44,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>45,'uncheckValue'=>null)); ?></td>
</tr>
<tr>
<td style="background-color: #f5f5f5"><b>Rare</b></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>51,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>52,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>53,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>54,'uncheckValue'=>null)); ?></td>
<td><?php echo $form->radioButton($model,'a11',array('value'=>55,'uncheckValue'=>null)); ?></td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
<table class="table">
<tbody>
<tr>
<td><div class="span2" style="width:auto; margin-top:5px; margin-bottom: 5px">Grade risk: </div></td>
<td><?php echo $form->textFieldRow($model,'risk_grade',array('class'=>'span2','maxlength'=>100)); ?>
</td>
</tr>
<tr>
I don't think you need ajax update for this task. You could easily do it with jQuery, you just need to have some kind of mapper for radio button values and string you want to set for the text field
First set the class for each radio button
<?php echo $form->radioButton($model,'a11',array('value'=>51,'uncheckValue'=>null, 'class'=>'radioClass')); ?>
Than set the class for textfield
<?php echo $form->textFieldRow($model,'risk_grade',array('class'=>'textClass span2','maxlength'=>100)); ?>
Then attach event listener in javascript
<script>
$(function(){
$(".radioClass").click(function(){
$(".textClass").val($(this).val());
});
});
</script>
This code will but clicked radio button value into textfield's value
Now you may need some kind of mapper for radio button values and the text you want to display, which may be generated by the server side
I hope I helped

How can I implement show() hide() function using jQuery in Codeigniter based on onclick of button in a table?

I need to have it so extra information pops up onclick and is otherwise hidden. I have tried using show() and hide() but I am unsure of what I am doing. Thanks guys, Kieran
View:
<table class="table table-striped">
<thead>
<tr><th>Last name</th><th>First name</th><th>Practice</th><th>County</th><th>Email</th><th></th></tr>
</thead>
<tbody>
<?php foreach ($health_professional as $hp): ?>
<tr>
<td><?php echo $hp['last_name'] ?></td>
<td><?php echo $hp['first_name'] ?></td>
<td><?php echo $hp['practice'] ?></td>
<td><?php echo $hp['county'] ?></td>
<td><?php echo $hp['email'] ?></td>
<td><a class="btn btn-info" href="hp/view/<?php echo $hp['hpid'];?>">Expand</a></td>
</tr>
<thead>
<tr><th>Permission to send info?</th><th>Registered</th><th>Attendance</th><th>Date of last update</th></tr>
</thead>
<tr id ="trid">
<td><?php echo $hp['send_info']; ?></td>
<td><?php echo $registration_count; ?></td>
<td><?php echo $attendance_count; ?></td>
<td><?php echo $hp['send_info_last_update']; ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
One problem that you are going to run into is that:
<tr id="trid">
is not going to be a unique id. Since you have an id associated with each $health_professional you can change it to:
<tr id="tr-<?php echo $hp['hpid'];?>" class="hidden">
Now that you have unique id's and a generic class, set up your link like so:
<td><a class="expand" data="<?php echo $hp['hpid'];?>">Expand</a></td>
Now in javascript you can call
$(document).ready(function(){
$('.expand').click(function () {
var id = $(this).attr('data');
$('#tr-' + id).show();
});
});
and in your css:
.hidden{display:none;}
Here is a hardcoded example (terrible styling aside):
http://jsfiddle.net/4qbFQ/
I can't see where you have provided the code where your button is, but you could simply put:
onclick="$('#extra_info_div_id').toggle();"
In the code for the button.
Here's how I would do it.
<script type="text/javascript">
$(document).ready(function(){
$('#trid').hide();
$('#expand').click(function(){
$('#trid').show();
});
});
</script>
...
<td><a class="btn btn-info" id="expand" href="javascript:;">Expand</a></td>

Categories