I have add_person.ctp file, and there I have Inputs and function, but this function is not working, My question is why its not working, I dont see any mistake in this code. If I click button, then this doing nothing.
Here is my code
<h2>People</h2>
<table id="mytable">
<tr><th></th><th>Last Name</th><th>First Name</th><th>Email</th><th>Gender</th></tr>
<tr id="person0" style="display:none;">
<td><?php echo $this->Form->button(' - ',array('type'=>'button','title'=>'Click Here to remove this person')); ?></td>
<td><?php echo $this->Form->input('unused.lastName',array('label'=>'','type'=>'text')); ?></td>
<td><?php echo $this->Form->input('unused.firstName',array('label'=>'','type'=>'text')); ?></td>
<td><?php echo $this->Form->input('unused.email',array('label'=>'','type'=>'text')); ?></td>
<td><?php echo $this->Form->input('unused.gender',array('label'=>'','type'=>'select','options'=>array('M'=>'M','F'=>'F','T'=>'T'))); ?></td>
</tr>
<tr id="trAdd"><td> <?php echo $this->Form->button('+',array('type'=>'button','title'=>'Click Here to add another person','onclick'=>'addPerson()')); ?> </td><td></td><td></td><td></td><td></td></tr>
</table>
<?php echo $this->Html->script(array('jquery-2.1.1.min.js'));?>
<script type='text/javascript'>
var lastRow=0;
function addPerson() {
lastRow++;
$("#mytable tbody>tr:#person0").clone(true).attr('id','person'+lastRow).removeAttr('style').insertBefore("#mytable tbody>tr:#trAdd");
$("#person"+lastRow+" button").attr('onclick','removePerson('+lastRow+')');
$("#person"+lastRow+" input:first").attr('name','data[Person]['+lastRow+'][lastName]').attr('id','personLastName'+lastRow);
$("#person"+lastRow+" input:eq(1)").attr('name','data[Person]['+lastRow+'][firstName]').attr('id','personFirstName'+lastRow);
$("#person"+lastRow+" input:eq(2)").attr('name','data[Person]['+lastRow+'][email]').attr('id','personEmail'+lastRow);
$("#person"+lastRow+" select").attr('name','data[Person]['+lastRow+'][gender]').attr('id','personGender'+lastRow);
}
function removePerson(x) {
$("#person"+x).remove();
}
</script>
Thank you for helping !
You have wrong selector here #mytable tbody>tr:#person0 and here #mytable tbody>tr:#trAdd. Replace with #mytable tbody>tr#person0 and #mytable tbody>tr#trAdd respectively. Keep in mind there is no need to use colon symbol in selectors.
Related
I have a foreach loop on a tale row. Each row has an "edit" button to edit that row only. Using JS, I want to get a hidden open and edit the row.
Bellow is my code that I have tried but it works on one element only.
<?php
foreach($sofas as $sofa)
{
?>
<tr>
<td><?php echo $sofa["name"];?></td>
<td><?php echo $sofa["zirkar"];?></td>
<td><?php echo $sofa["parche"];?></td>
<td><?php echo $sofa["fiparche"];?></td>
<td><?php echo $sofa["mizvasat"];?></td>
<td><?php echo $sofa["asaly"];?></td>
<td>
<input onclick="myFunction()" type="button" value="ویرایش">
</td>
</tr>
<tr style="display:none;" id="<?php echo $sofa["id"];?>">
<td>
This is my DIV element.
</td>
</tr>
<?php
}
?>
<script>
function myFunction() {
<?php
foreach($sofas as $sofa)
{
?>
var x = document.getElementById("<?php echo $sofa["id"];?>");
<?php
}
?>
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
I'm still learning, so please help me resolve my issue. I'm expecting the code to work on each individual table row.
Why don't you use something like this:
<?php
foreach($sofas as $sofa)
{
?>
<tr>
<td><?php echo $sofa["name"];?></td>
<td><?php echo $sofa["zirkar"];?></td>
<td><?php echo $sofa["parche"];?></td>
<td><?php echo $sofa["fiparche"];?></td>
<td><?php echo $sofa["mizvasat"];?></td>
<td><?php echo $sofa["asaly"];?></td>
<td>
<input onclick="myFunction('<?php echo $sofa["id"];?>')" type="button" value="ویرایش">
</td>
</tr>
<tr style="display:none;" id="<?php echo $sofa["id"];?>">
<td>
This is my DIV element.
</td>
</tr>
<?php
}
?>
<script>
function myFunction(id) {
var x = document.getElementById(id);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
Code is very simple, you only need to send the element id to the js function.
I have an activity table that contain the list of activity student had joined before. So if the student is new student, there will have been no activity for that student.
<table align="center" width="1000" border="1" >
<h3>Activity List</h3>
</br>
<tr align="center" style="font-weight:bold" >
<td>ID</td>
<td>Activity</td>
<td>Sem</td>
<td>Session</td>
<td>Achievement</td>
<td>Level</td>
</tr>
<?php do { ?>
<tr align="center">
<td><?php echo $row_Recordset1['student_id']; ?></td>
<td><?php echo $row_Recordset1['activity']; ?></td>
<td><?php echo $row_Recordset1['sem']; ?></td>
<td><?php echo $row_Recordset1['session']; ?></td>
<td><?php echo $row_Recordset1['achievement']; ?></td>
<td><?php echo $row_Recordset1['level']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
How can I make this table only show on screen if it is not empty. Btw, im using session to display the exist record.
The best way to do this would be to get your array via mysql(i)_fetch_array() of the query you have build and then to chec to see if the query has rows like:
$qry = "SELECT `this` FROM `table`"
while ($result = mysql_fetch_array($qry)) {
if (mysql_num_rows($result) > 0) {
echo "We have rows!";
}
else {
echo "Looks like we haven't got anything here!";
}
}
I hope this helps.
Might also help to look here: PHP mysql_num_rows method.
<?php if(!empty($activity))
{
your msg ..
}
else
{
}
?>
where empty() will check a given variable is empty or not
Well that's what the if statement is used for:
if(!count($activities)) {
echo "This student has no activities yet.";
} else {
//display activities
....
}
I'm trying to select a particular row using jquery from a looped foreach. For now I'm just using a simple alert to see if I get the right row. Problem is, it only selects the last row, and not the row I clicked.
Example code:
<?php foreach ($customers as $c) : ?>
<tr>
<td><?php echo $count++; ?></td>
<td><?php echo $c['firstname'] . " " . $c['lastname']; ?></td>
<td><?php echo $c['phone']; ?></td>
<td><?php echo $c['email']; ?></td>
<td style="display:none" id="'<?php echo $c['id'] ?>'"> </td>
</tr>
<?php endforeach ?>
The Jquery call:
$('tr td ').click(function(){
var fn = '<?php echo $c['firstname']; ?>';
alert(fn);}
);
This will output all td values. Credits
<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
$count = 0;
$customers = array (
array('firstname'=>'fname1', 'lastname'=>'lname1','phone'=>'111','email'=>'a#gmail.com','id'=>1),
array('firstname'=>'fname2', 'lastname'=>'lname2','phone'=>'222','email'=>'b#gmail.com','id'=>2),
array('firstname'=>'fname3', 'lastname'=>'lname3','phone'=>'333','email'=>'c#gmail.com','id'=>3),
array('firstname'=>'fname4', 'lastname'=>'lname4','phone'=>'444','email'=>'d#gmail.com','id'=>4),
);
?>
<table class="table table-bordered">
<?php foreach ($customers as $c) : ?>
<tr class="getdetails">
<td><?php echo $count++; ?></td>
<td><?php echo $c['firstname'] . " " . $c['lastname']; ?></td>
<td><?php echo $c['phone']; ?></td>
<td><?php echo $c['email']; ?></td>
<td style="display:none" id="'<?php echo $c['id'] ?>'"> </td>
</tr>
<?php endforeach ?>
</table>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$(".getdetails").click(function(){
$(this).find("td").each(function(){
console.log($(this).html());
});
});
</script>
</body>
</html>
You can access particular row's column like bellow
$('tr.item').each(function() { // selected tr
var col1 = $(this).find("td:eq(0) > input").val(); // find the input field value inside the first column of the row
var col2 = $(this).find("td:eq(1) > input").val();}
I am trying to automate the hallticket process for an institution. This includes a certain verification process.
Iframe is working only for the first row, what should I do to open it for multiple rows based on the row selection
php code
<?php
while($row = mysqli_fetch_assoc($rows)) //Retriving the rows from the database
{
?>
<td><?php echo $app_id ?></td>
<td><?php echo $first_name ?></td>
<td><?php echo $last_name ?></td>
<td><?php echo $email_id ?></td>
<td><?php echo $mobile_number ?></td>
<td><?php echo $dd_number ?></td>
<td><?php echo $course_name ?></td>
<?php
if($dd_submit==1 && $ht_sent==0)
{?>
<td class="verified"><span>Verified</span></td>
<?php }elseif($dd_submit==0 && $ht_sent==0){ ?>
<td class="non_verified"><span>None Verified</span></td>
<?php }else{ ?> <td class="Pending"><span>Hall Ticket Sent</span></td>
<?php } ?>
<!-- I think the problem is here the java script is not repeating -->
<td><button id="dialog" name="verify" value="<?php echo $row['app_id'] ?>" > <img src="images/success_tick.png"></button><img src="images/meassage_table.png"><img src="images/comment.png"></td>
</tr>
<?php
}
?>
<div id="dialogContent" title="DD Verification for Vinay Draksharam">
<iframe src='ddverification_page.php?app_id=<?php echo $app_id ?> '></iframe>
Java Script
<script>
$(function () {
$("#dialogContent").dialog({
autoOpen: false,
modal: true
});
$('#dialog').click(function () {
$("#dialogContent").dialog( "open" );
});
});
</script>
First problem- you have many rows but you only create one dialog. You probably have to put dialog creation inside loop to have separate dialog for each row. You also need some way to create connection between dialog and row. Best idea would probably to use $app_id as id of dialog.
What i would do:
PHP:
<?php
while($row = mysqli_fetch_assoc($rows)) //Retriving the rows from the database
{
?>
<td><?php echo $app_id ?></td>
<td><?php echo $first_name ?></td>
<td><?php echo $last_name ?></td>
<td><?php echo $email_id ?></td>
<td><?php echo $mobile_number ?></td>
<td><?php echo $dd_number ?></td>
<td><?php echo $course_name ?></td>
<?php
if($dd_submit==1 && $ht_sent==0)
{?>
<td class="verified"><span>Verified</span></td>
<?php }elseif($dd_submit==0 && $ht_sent==0){ ?>
<td class="non_verified"><span>None Verified</span></td>
<?php }else{ ?> <td class="Pending"><span>Hall Ticket Sent</span></td>
<?php } ?>
<!-- I think the problem is here the java script is not repeating -->
<td><button class="dialog" name="verify" value="<?php echo $row['app_id'] ?>" >
<div class="dialogContent" id="dialogContent_<?php echo $app_id ?>" title="DD Verification for Vinay Draksharam">
<iframe src='ddverification_page.php?app_id=<?php echo $app_id ?> '></iframe> </div>
<img src="images/success_tick.png"></button><img src="images/meassage_table.png"><img src="images/comment.png"></td>
</tr>
<?php
}
?>
And Javascript:
$(function () {
$(".dialogContent").dialog({
autoOpen: false,
modal: true
});
$('.dialog').click(function () {
$("#dialogContent_"+$(this).val()).dialog( "open" );
});
});
I have the following table:
foreach ($forwards as $data):
?>
<tr style="cursor: pointer" class="main_row">
<td><?php echo $data['Offer']['id']; ?> </td>
<td><?php echo $data['Offer']['name'];?> </td>
<td><?php echo round($data['Stat']['ltr'],2)."%"; ?></td>
<td><?php echo round($data['Stat']['cpc'],2)."%"; ?></td>
<td><?php echo $data['Category']['name']?></td>
<td><?php echo $data['Country']['name'] ?></td>
</tr>
<?php
endforeach; ?>
now the idea is that when ever you click one of the main_row it should redirect to a new url. the problem is that the url contains an id for instance the url could look like www.example.com/details/2 where 2 is the id.
This id is like all of the other data stored in a php variable: $data['Offer']['id'];
Now my question how can i redirect using both php and javascript? is there a work around? .
Please do note that i am fully aware that php i server side and javascript is not. it is because of this i am asking this question.
<table>
<?php foreach ($forwards as $data): ?>
<tr data-link="http://www.example.com/details/<?php echo $data['Offer']['id']; ?>">
<td><?php echo $data['Offer']['id']; ?> </td>
<td><?php echo $data['Offer']['name'];?> </td>
<td><?php echo round($data['Stat']['ltr'],2)."%"; ?></td>
<td><?php echo round($data['Stat']['cpc'],2)."%"; ?></td>
<td><?php echo $data['Category']['name']?></td>
<td><?php echo $data['Country']['name'] ?></td>
</tr>
<?php endforeach; ?>
</table>
<script>
$('table tr').click(function(){
document.location.href = $(this).data('link');
});
</script>
if you use jQuery though.
You can get the ID from the first column in the row:
$(".main_row").click(function() {
var id = $(this).find("td:first-child").text();
window.location.href = 'www.example.com/details/' + id;
});
If you use jQuery do this:
$('.main_row').click(function(){
window.location.href = "YOUR_URL"+$(this).attr('id');
});
And modify the html for the row to look like:
<tr style="cursor: pointer" class="main_row" id="<?php echo $id ?>">
This way - any time you click on the row it appends the row ID to the url and redirect you there.
If I am understanding you question correctly, your not looking for a redirect. To redirect in php, it must be done before you echo anything to the browser.
Because you want to "redirect" after your display is rendered, you should be using anchors.
<td><a href="www.example.com/details/<?php echo $data['Offer']['id']; ?>">
<?php echo $data['Offer']['id']; ?>
</a></td>
The anchor will direct the browser to the href url.