How to find attribute on a row table using jquery - php

if I have html that integrated on php like this :
foreach ($data_pc as $data) {
?>
<tr>
<td class="center"><?php echo $no++ . ". "; ?> </td>
<td class="center" data-no="<?php echo $data['id_pc'] ?>"><?php echo $data['nama_user']; ?></td>
<td class="center"><?php echo $data['perusahaan']; ?></td>
<td class="left">
<a class="btn btn-info" name="edit" title="Edit User" req_id="<?php echo $data['nama_user']; ?>" data-toggle="modal" data-target="#myModal1" role="button">
<i class="halflings-icon white edit"></i>
</a>
<td>
<a class="btn btn-danger" id="btn-hapus" title="Hapus User" req_nama="<?php echo $data['nama']; ?>">
<i class="halflings-icon white trash" ></i>
</a>
</td>
</td>
</tr>
<?php } ?>
that would be representated row on table html.
I want to take the attribute data-no on a row.
So, I write jquery code like this :
$(document).on('click', '#btn-hapus', function() {
var $row = $(this).closest("tr"); // Find row
var data = $row.find("data-no").text(); // Find text
alert(data);
});
It gives me a blank value ? How to find attribute on a row table using jquery ?

You need to use Has Attribute Selector [name] to target the element that have the required attribute:
var data = $row.find("[data-no]").text(); // get text of element that have attr data-no
if you want to get the attribute value of element then use .data() selector
var data = $row.find("[data-no]").data('no'); //get data attribute value

Related

how can I edit/remove info in MySQL database via bootstrap table

I'm creating an admin panel for small ecommerce website (to practice) and I have no idea how to make php understand which id product I want to update after I click this little <i></i> element because I already use $_POST['<i> element name'] in order to detect whether or not it's ready to be updated, I know you cannot assign 2 names to one element otherwise I could use $id = $results['id']; as second name... any advices? I'm completely new in php. sorry if this post is stupid I just need some help...
$query="select * FROM productadd";
$result= mysqli_query($connection, $query);
$results = mysqli_fetch_array($result);
if(!empty($results['id'])) {
while($results = mysqli_fetch_array($result)){
$id = $results['id'];
$name = $results['name'];
$text = $results['text'];
$price = $results['price'];
$image = $results['image'];
echo '
<tr>
<td>'.$id.'</td>
<td>'.$name.'</td>
<td>'.$text.'</td>
<td>$'.$price.'</td>
<td>
<img src="'.$image.'" style="style="display:block;" width="100%" height="100%" ">
</td>
<td>
<a class="add" value="asd" title="Add" name="edit" data-toggle="tooltip">
<i class="material-icons"></i>
</a>
<a class="edit" name="edit" title="Edit" data-toggle="tooltip">
<i class="material-icons"></i>
</a>
<a class="delete" title="Delete" data-toggle="tooltip">
<i class="material-icons"></i>
</a>
</td>
</tr>';
}
}

Need help passing two php variables through a url to the next page

I'm new to Php but what I need this php code to do is to pass two variables through the URL like this
<td align="center"><a class="btn btn-danger" href="reserve.php?distroid=<?php echo $row["distroid"] echo $row['orderid']; ?>">Place Order</a></td>
When we try and do it with just distroid, it works completely fine. But I'm unsure how to pass both values.
Any help would be appreciated.
EDIT: SOLVED IT THANK YOU FOR THE HELP ALL
Try to add more key for the query string like this :
<td align="center">
<a class="btn btn-danger"
href="reserve.php?distroid=<?php echo $row["distroid"]; ?>&orderid=<?php echo $row['orderid']; ?>">
Place Order
</a>
</td>
Then you can get both distroid and orderid
Try this query string like this :
<td align="center">
<a class="btn btn-danger"
href="reserve.php?distroid=<?php echo $row["distroid"]."&orderid=".$row['orderid']; ?>">
Place Order
</a>
</td>
Then you can get both variable distroid and orderid
Hi you can try this anchor tag, it will work for you:
<a class="btn btn-danger" href="reserve.php?distroid=<?php echo $row['distroid'];
?>&orderid=<?php echo $row['orderid']; ?>">
Place Order
</a>

"Issue with updating a record using a button”

When I click on the update button, the SQL query updates the last record not the wanted one
when displaying several courses, I would like to give the user the right to confirm one of the courses, when I click on the update button, it updates the last record
<?php
$cc = mysqli_query($mysqli, "SELECT * from course WHERE c_email ='$emailu' AND course_situation='pending'");
while($ccc = mysqli_fetch_array($cc)) {
$idcourse = $ccc['idcourse'];
$city = $ccc['idville'];
$pickup = $ccc['course_depart'];
?>
<tr>
<td><?= $idcourse ?></td>
<td><?= $pickup ?></td>
<td><?php echo $ccc['course_date'] ?>, <?php echo
$ccc['course_heure'] ?></td>
<td>
<?php
if($ccc['course_situation'] == "pending") { ?>
<span class="badge badge-danger">Pending</span>
<?php } else { ?>
<span class="badge badge-success">Confirmed</span>
<?php } ?>
</td>
<td><?php echo $ccc['c_phone'] ?></td>
<td><?php echo $ccc['c_phone'] ?></td>
<td><?php echo $ccc['idclient'] ?></td>
<td>
<button type="submit" name="cancelcourse" class="btn btn-danger btn-xs"><i class="icon md-check" aria-hidden="true"></i> Update</button>
</tr>
<?php }
if(isset($_POST["cancelcourse"])) {
$query = "UPDATE course SET id='$idu' AND course_situation='confirmed' WHERE idcourse='$idcourse' ";
if(mysqli_query($mysqli, $query)) {
echo "<div class=' alert alert-success' style='padding-left:150px'>
<strong>Success!</strong> Event page updated.</a>.
</div>";
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=index.php">';
} else {
echo "<div class='alert alert-danger'>
<strong>ERROR!</strong> We invite you to try Again Later.
</div> " . mysqli_error($mysqli);
}
}
You generate button for each course, but all your buttons are equal, they don't contains any information about which course you want to update. You use variable $idcourse, but it is assigned inside while-loop that generates table - for each row that is shown this variable is changed, so after showing whole table it will contains id for last row.
Please remember, that each time your page is reloaded (like after clicking button) whole script is executed from beginning and no variables are stored, unless you pass it yourself.
Simplest solution:
1.Add id to button
<button type="submit" name="cancelcourse" class="btn btn-danger btn-xs" value="<?=htmlspecialchars($idcourse)?>">
2.Change query
$idcourseToRemove = mysqli_real_escape_string($mysqli, $_POST["cancelcourse"]);
$query = "UPDATE course SET id='$idu' AND course_situation='confirmed' WHERE idcourse='$idcourseToRemove' ";
And last advice: learn about SQL Injection and how to avoid it (like for example by mysqli_real_escape_string function)
UPDATE course SET id='$idu',course_situation='confirmed' WHERE
idcourse='$idcourse'

Show row collapsed in Bootstrap

$i = 0;
while($row = $statement->fetch()) {
$current_id = 'id-' . $i;
$i++;
echo "
<tr>
<td>
<p>
<button class='btn btn-primary'
type='button'
data-toggle='collapse'
data-target='#<?=$current_id?>'
aria-expanded='false'
aria-controls='Collapse'>
Button with data-target
</button>
</p>
<div class='collapse' id='<?=$current_id?>'>
<div class='card card-block'>
Here is the content for block which will be shown when the button. This uses button with data-target attribute for collapsing.
</div>
</div>
</td>
";
}
Where is my error? It doesn't collapse but I need the unique ids for my rows.
now it work. What i have to do now:
$ccid = "#$current_id";
$ccid2 = "$current_id";
And then i have put the variables inside of ID and Href Like this:
href='".$ccid."'
id='".$ccid2."'
That fixxed this problem. Thanks to all!

how to delete a row in database using codeigniter

hye, i'm trying to do 'delete function' for my modul. i've tried so many different ways for this. but it come out no result, some come with error.
this is my controller (named expert.php) :
public function remove() {
$siri = $this->uri->segment(3);
$this->Kepakaran_m->delete($siri);
$this->view();
}
siri is the a field name (column name) in my table. it it primary key.
this is my model (named Kepakaran_m.php):
function delete($siri) {
$this->dbsmk->where('siri', $siri);
$this->dbsmk->delete('kexpt003pakar');
if ($this->dbsmk->affected_rows() == 1) {
return TRUE;
}
return FALSE;
}
kexpt003pakar is the table name.
so my view is this (named kepakaran.php):
<tbody>
<?php if(empty($kepakaran)) { ?>
<tr>
<td colspan="8">Pengguna tidak mempunyai rekod kepakaran!</td>
</tr>
<?php } else {
$num = 0;
foreach ($kepakaran as $list_kepakaran) {
$num++;
?>
<tr>
<td><?php echo $num; ?></td>
<td><?php echo $list_kepakaran->kategori; ?></td>
<td><?php echo $list_kepakaran->bidang; ?></td>
<td><?php echo $list_kepakaran->spesifik; ?></td>
<!-- untuk keluarkan tahap -->
<td><?php
if($list_kepakaran->tahap=='1'){
echo "Sederhana";
}elseif ($list_kepakaran->tahap=='2') {
echo "Tinggi";
}elseif ($list_kepakaran->tahap=='3') {
echo "Sangat Tinggi";
}
?>
</td>
<!-- done untuk keluarkan tahap -->
<td><?php echo $list_kepakaran->biltahun; ?></td>
<td>
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#tambahkepakaran"><i class="glyphicon glyphicon-pencil"></i></button>
<i class="glyphicon glyphicon-trash"></i>
</td>
</tr>
<?php }} ?>
</tbody>
so the delete button is in this area >>siri; ?>" type="button".....
i hope your answer will help me to discover this up. thank you :)
What's the error? I think you just need to modify your codes becomes:
<i class="glyphicon glyphicon-trash"></i>
Then it should be ok.
In order to use uri segment..do not forget to call url helper in autoload.php
english is not my native language.
For solve your problem, you can check a lot of things.
Did you forget to load model in your controller ?
Try to dump value of $siri in your remove function and show response on your network tab in chrome devTools for example.
If you don't call your controller, look at side of your href or open Application/config/routes.php for use routing and add this $route['expert/remove/(:num)']['GET'] = "expert/remove/$1";
If $siri is empty look at the side of $this->uri->segment(3);
Verify if you load correctly the helper.
For that, go to Application/config/autoload.php and see if url is in helper $autoload['helper'] = array('url','security','language', 'form', 'text');

Categories