select p text jquery - javascript

I'm retrieving data from mysql like this structure =>
echo "<table id='postI" . $chat_row['id'] . "'>";
echo "<tr>";
echo "<td><p class='post_author'>" . $chat_row['user_name'] . "</p><a href='javascript: void(0)' class='edit_post_a'><div class='edit_post'></div></a></td>";
echo "</tr>";
echo "<tr>";
echo "<td><p class='posted_on'>" . $chat_row['posted_on'] . "</p></td>";
echo "</tr>";
echo "<tr>";
echo "<td><br></td>";
echo "</tr>";
echo "<tr>";
echo "<td><p class='posted_msg'>" . $chat_row['message'] . "</p></td>";
echo "</tr>";
echo "</table>";
and I can get the table id with this expression =>
$(".edit_post_a").bind("click",function(){
var tb_id = $(this).closest("table").attr("id"); // works , gets the id of the table
alert($(tb_id + ".posted_msg").text()); // don't work, gets nothing , just alert box
});
My problem is that want to know text of the third p, what I've tried =>
alert($("#" +tb_id + ".posted_msg").text());
alert($("#" +tb_id).find("p").last().text());
alert($("#" +tb_id + " p:nth-child(3)").text());
nothing works , get empty alert box , I've no idea whats wrong ? how can I fix it ? thanks
HTML code =>
<table id='postI245'>
<tr>
<td><p class='post_author'>USER</p><a href='javascript: void(0)' class='edit_post_a'><div class='edit_post'></div></a>
</td>
</tr>
<tr>
<td><p class='posted_on'>DATE</p></td>
</tr>
<tr>
<td><br></td>
</tr>
<tr>
<td><p class='posted_msg'>MSG</p></td>
</tr>
</table>
and so on ...

Your selector selects nothing, you should add # for selecting an element by id.
var txt = $('#'+tb_id + " .posted_msg").text();
or:
var txt = $(this).closest("table").find("p:eq(2)").text()

Related

Inline JavaScript function within PHP Tag

I am running this code in a PHP file
<?php
if(empty($item)){
echo "Your cart is empty";
}
else{
echo '<table id="cart_table"><tr><th>Item</th><th>Price</th><th></th></tr>' ;
for($i = 0;$i< count($item);$i++){
if(!empty($item[$i])){
$numRow++;
echo '<tr><td>' . "<img src = 'images/$photo[$i].jpg'>" .'</td>';
echo '<td>' .'$' . $item[$i] . '</td>';
echo '<td>' . '<button id= "btn" onclick = "function(){
<script type="text/javascript">
document.getElementById("cart_table").deleteRow('. $numRow .');
}"> Remove</button>'.'</tr>' ;
}
}
echo '</table>';
}
?>
Please consider these lines of code only from the above snippet:
echo '<td>' . '<button id= "btn" onclick = "function(){
<script type="text/javascript">
document.getElementById("cart_table").deleteRow('. $numRow .');
}"> Remove</button>'.'</tr>' ;
}
The result of this code is:
The result I am trying to achieve is:
Is it possible to achieve this result? The result is going to be a button with the label "Remove" and with the onClick property that calls a INLINE(I know it's bad practice, but I have a good reason for it) function which executes some lines of code.
Try not to mix up HTML and PHP together, if there is a need to mix HTML and PHP, try in proper formatting.
<?php
if(empty($item)):
?>
<span>Your cart is empty</span>
<?php
else:
?>
<table id="cart_table">
<tr>
<th>Item</th>
<th>Price</th>
<th></th>
</tr>
<?php
for($i = 0;$i< count($item);$i++):
if(!empty($item[$i])):
$numRow++;
?>
<tr>
<td><img src ='images/<?php echo $photo[$i].jpg; ?>'/></td>
<td>$<?php echo $item[$i]; ?></td>
<td><button id= "btn" onclick = "deleteFromCart('<?php echo $numRow; ?>')"> Remove</button></td>
</tr>
<?php
endif;
endfor;
?>
</table>
<?php
endif;
?>
<script>
function deleteFromCart(rowToDelete){
document.getElementById("cart_table").deleteRow(rowToDelete);
}
</script>

Database row is not deleting, AJAX, PHP error

I want to remove DB row data from HTML table. I have an HTML table where I have called all the database table values. I want to give a delete order option to the user. I have used a delete button for that. To do this, I am trying to use ajax and jquery with php delete query. But the issue is When I click on delete it just deletes the HTML row data. It should delete that particular id row from DB as well. I need your help. Please let me know what I have done wrong? I am very much new to ajax and jquery.
remove.php
<?php
include "config.php";
$id = $_REQUEST['id'];
// Delete record
$query = "DELETE FROM mi_home_tv WHERE id='$id'";
mysqli_query($link,$query);
echo 1;
jquery+ajax code
<script src='jquery-3.0.0.js' type='text/javascript'></script>
<script type="text/javascript">
$(document).ready(function(){
// Delete
$('.delete').click(function(){
var el = this;
var id = this.id;
var splitid = id.split("_");
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax({
url: 'remove.php',
type: 'POST',
data: { id:deleteid },
success: function(response){
// Removing row from HTML Table
$(el).closest('tr').css('background','tomato');
$(el).closest('tr').fadeOut(800, function(){
$(this).remove();
});
}
});
});
});
</script>
Button Code:
echo "<td> <span id='del_<?php echo $id; ?>' type='submit' class=' delete btn c-theme-btn c-btn-uppercase btn-xs c-btn-square c-font-sm'>Delete</span> </td>";
php code:
<form method="post" action="remove.php">
<?php
echo " <thead>
<tr>
<th>Order ID</th>
<th>Date</th>
<th>Name</th>
<th>Store Name</th>
<th>Zip</th>
<th>City</th>
<th>Address</th>
<th>Contact</th>
<th>Tv Varient</th>
<th>Total</th>
<th>Delivery</th>
<th>Action</th>
</tr>
</thead>
<tbody>";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
echo"<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['rdate'] . "</td>";
echo "<td>" . $row['rname'] . "</td>";
echo "<td>" . $row['rstore'] . "</td>";
echo " <td>" . $row['rzip'] . "</td>";
echo "<td>" . $row['rcity'] . "</td>";
echo "<td>" . $row['raddress'] . "</td>";
echo "<td>" . $row['rphone'] . "</td>";
echo "<td>" . $row['rtv_varient'] . "</td>";
echo"<td>" . $row['ramount'] . "</td>";
echo"<td>" . $row['rdelivery'] . "</td>";
echo "<td> <input type='submit' value='delete' id='del_<?php echo $id; ?>' type='submit' class=' delete btn c-theme-btn c-btn-uppercase btn-xs c-btn-square c-font-sm'>Delete</button> </td>";
echo"</tr>";
}
?>
</form>
You should have a form which will have an input as follows:
<form method="post" action="remove.php">
<input type="submit" value="Delete" name="id" />
</form>

Put information message that user cant continue ,if number of rows is not true

I have code
if($rowcount1 > 0){
?>
<h3 class="text-muted"><u>DEPARTURE</u></h3>
<h4><?php echo "$origin to $destination "?></h4>
<table class="table table-hover">
<thead>
<tr>
<th>FLIGHT NO.</th>
<th>DEPART</th>
<th>ARRIVE</th>
<th>AIRPORT</th>
<th>DURATION</th>
<th>FLY ONLY</th>
<th>FLY + BAGGAGE</th>
</tr>
</thead>
<tbody>
<?php
foreach ($query as $flightr) {
echo "<tr>";
echo "<td>".$flightr['id']."</td>";
echo "<td>".$flightr['depart']."</td>";
echo "<td>".$flightr['arrive']."</td>";
echo "<td>".$flightr['airport']."</td>";
echo "<td>".$flightr['duration']."</td>";
echo "<td> <label for=lbl7> <input type='radio' checked id='lbl7' name='flight[]' value='".$flightr['flyonly']."'> PHP ".number_format($flightr['flyonly'])."</label></td>";
echo "<td> <label for=lbl8> <input type='radio' checked id='lbl8' name='flight[]' value='".$flightr['flybaggage']."'> PHP ".number_format($flightr['flybaggage'])."</label></td>";
echo "</tr>";
}
?>
</tbody>
</table>
<hr>
<?php
}else{
echo " <div class='alert alert-info' role='alert'>
No results found on <BIG class='text-muted'>Departure</BIG> pick another flight thank you.
</div>
";
}
}
}
?>
<button type="button" name="forform" onclick="" class="hit">hit</button>
how to put info message that user cant continue (and they cant click the button to continue), if number of rows is not true. Via button onclick. begginer in js sorry. Thanks in advance
Add disabled attribute of button based on rowCount
<button type="button" name="forform" onclick="" class="hit"
<?php echo ($rowcount1 > 0) ? '' : 'disabled'; ?>>hit</button>
You can do that by put button in if else condition with disable attributes
if ($rowcount1 > 0) {
?>
<h3 class="text-muted"><u>DEPARTURE</u></h3>
<h4><?php echo "$origin to $destination " ?></h4>
<table class="table table-hover">
<thead>
<tr>
<th>FLIGHT NO.</th>
<th>DEPART</th>
<th>ARRIVE</th>
<th>AIRPORT</th>
<th>DURATION</th>
<th>FLY ONLY</th>
<th>FLY + BAGGAGE</th>
</tr>
</thead>
<tbody>
<?php
foreach ($query as $flightr) {
echo "<tr>";
echo "<td>" . $flightr['id'] . "</td>";
echo "<td>" . $flightr['depart'] . "</td>";
echo "<td>" . $flightr['arrive'] . "</td>";
echo "<td>" . $flightr['airport'] . "</td>";
echo "<td>" . $flightr['duration'] . "</td>";
echo "<td> <label for=lbl7> <input type='radio' checked id='lbl7' name='flight[]' value='" . $flightr['flyonly'] . "'> PHP " . number_format($flightr['flyonly']) . "</label></td>";
echo "<td> <label for=lbl8> <input type='radio' checked id='lbl8' name='flight[]' value='" . $flightr['flybaggage'] . "'> PHP " . number_format($flightr['flybaggage']) . "</label></td>";
echo "</tr>";
}
?>
</tbody>
</table>
<hr>
<button type="button" name="forform" onclick="" class="hit">hit</button>
<?php
} else {
echo " <div class='alert alert-info' role='alert'>
No results found on <BIG class='text-muted'>Departure</BIG> pick another flight thank you.
</div>
";
echo '<button type="button" name="forform" onclick="" class="hit" disable="disable">hit</button>';
}
?>

onclick event inside table and foreach loop

I want to allow clicking on a whole host table in foreach loop in codeigniter. For some reason, the code does not work for me. Do you have an idea how to fix it?
Thanks in advance
<table class="table table-hover">
<thead>
<tr>
<th>השולח</th>
<th>נשלחה בתאריך</th>
<th>נושא ההודעה</th>
<th>תוכן ההודעה</th>
</tr>
</thead>
<tbody>
<?php foreach ($messages->result() as $row)
{
?>
<script type="text/javascript">
function newDoc() {
window.location.assign("<?php echo base_url();?>/read_messages/<?php echo $row->id;?>")
}
</script>
<?php
echo "<tr>";
echo "<td>".$row->author. "</td>";
echo "<td>".$row->posted_in. "</td>";
echo "<td>".$row->title. "</td>";
echo "<td>".$row->body. "</td>";
?>
onclick="newDoc()";
<?php
echo "</tr>";
}
?>
</tbody>
</table>
</div>
As I understand your question you want fetch click on every row. then your code should be as:
Your Script should be out side
<script type="text/javascript">
function newDoc(id) {
window.location.assign("<?php echo base_url();?>/read_messages/"+id);
}
</script>
<?php foreach ($messages->result() as $row)
{
echo "<tr onclick='newDoc(".$row->id.")'>";
echo "<td>".$row->author. "</td>";
echo "<td>".$row->posted_in. "</td>";
echo "<td>".$row->title. "</td>";
echo "<td>".$row->body. "</td>";
echo "</tr>";
}
?>
You have to assign onClick="newDoc" to html element like
<table onClick="newDoc()">
You have to issues, one your newDoc() function is not assigned to element and two you want create the newDoc function once and only once passing in the $row->id so that its affect changes for each <tr>
<script type="text/javascript">
function newDoc(rowId) {
window.location.assign("<?php echo base_url();?>/read_messages/" + rowId);
}
</script>
<?php
foreach ($messages->result() as $row) {
echo "<tr onlick='newDoc(<?php echo $row->id ?>)'>";
echo "<td>".$row->author. "</td>";
echo "<td>".$row->posted_in. "</td>";
echo "<td>".$row->title. "</td>";
echo "<td>".$row->body. "</td>";
echo "</tr>";
}
?>

The value of the text box prints as Undefined

Hi I'm writing a code to get the value of a textbox. Though all the other values prints perfectly well alert(FormID) prints as Undefined. Here I have clearly commented on what I'm getting. Please someone help me to show the mistake I have done.
<html>
<title></title>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script></head>
<script src="UserTypeSubmit.js"></script></head>
<script>
$(document).ready(function(){
$(".FormEdit").click(function(){
var B=$(this).attr('id');// Ex: Getting ID as FormEdit1 if click on first Edit link
var ReplacesVal=B.replace("FormEdit","");//Prints Value as 1 a I I need.
alert("FormIDInput"+ReplacesVal);//Alert : FormIDInput1
var FormID=$("#FormIDInput"+ReplacesVal).val();
alert(FormID);// Alert: Undefine. Im expecting the value 1 to be in the alert as $("#FormIDInput"+ReplacesVal).val() is equal to 1. and it prints well in the text box.
});
return false;
});
</script>
</head>
<body>
<div class="User" style="color:#0B173B">Forms_Available_to_Collect_Pubic_Health_Information </div>
<br><br>
<?php
include 'connectionPHP.php';
$result=mysqli_query($con,"SELECT * FROM Form");
echo "<table border='1' id='DisplayFormDetailss'>
<tr style='background-color:#D0A9F5;' height='40px;'>
<th width='100px;'>Form ID</th>
<th width='420px;'>Form Name</th>
<th width='100px;'>Inactivate</th>
<th width='70px;'>Edit</th>
<th width='70px;'>Delete</th>
<th width='70px;'>Save</th>
</tr>";
$i=1;
while($row = mysqli_fetch_array($result))
{
$w=$row['Form_ID'];
$v=$row['Form_Name'];
echo "<tr height='25px;'>";
echo "<input id='FormIDInput ".$i."' value='$w' ></input>";
//Prints 1,2,3,4,5,6 as the values of input boxes perfectly.
echo "<td name='FormID[]' class='FormID' align='center' value='$w'>$w</td>";
echo "<td name='FormName[]' id='FormName".$i."' align='left'>$row[Form_Name]</td>";
echo "<td name='FormInactive[]' id='FormInactive".$i."' align='center'><input type='checkbox'></input></td>";
echo "<td class='FormEdit' id='FormEdit".$i."' align='center'><a href='' align=left>Edit</a></td>";
echo "<td name='FormDelete[]' id='FormDelete".$i."' align='center'><a href='' align=left>Delete</td>";
echo "<td name='FormSave[]' id='FormSave".$i."' align='center'><a href='' align=left>Save</td>";
echo "</tr>";
$i++;
}
echo "</table>";
?>
</body>
</html>
In your PHP code, there is a space in the input box:
echo "<input id='FormIDInput ".$i."' value='$w' ></input>";
try to replace it with
echo "<input id='FormIDInput".$i."' value='$w' ></input>";
in input type filed missing type ='text' & space issue:
echo "<input type='text' id='FormIDInput".$i."' value='$w' ></input>";
You need to use id='FormIDInput".$i."' instead of id='FormIDInput ".$i."'.
And You can use the FormID[] input textfiled for FormIDInput
try using,
echo "<td name='FormID[]' id='FormIDInput".$i."' class='FormID' align='center' value='$w'>$w</td>";
instead of
echo "<input id='FormIDInput ".$i."' value='$w' ></input>";
//Prints 1,2,3,4,5,6 as the values of input boxes perfectly.
echo "<td name='FormID[]' class='FormID' align='center' value='$w'>$w</td>";

Categories

Resources