Using javascript/jQuery to hide certain elements on screen retrieved from $.post - php

most likely an easy solution, but please forgive my early learning curve in the world of programming and using php with jquery together. I'm able to retrieve information from the database easily and display that on the screen. However, I'd like to be able to hide certain elements that are retrieved from the database and written out in php on the page and show them on a click event. I'm able to do this easily on other sections of the website, that are not being retrieved using $.post. Are there certain rules that apply with jQuery once I've used $.post and ajax to retrieve info from the database?
php below:
<?php echo "
<div class='phone_tracking customCall separation'>
<h3>Phone Tracking</h3>
<a class='showCustomDatePhone' href='#customDate_individualPhone'>View & Listen to Individual Phone Leads >></a>
<table>
<tr class='tableHead'>
<td>Date</td>
<td>Traffic+ Calls</td>
<td>New Vehicle Calls</td>
<td>Used Vehicle Calls</td>
<td>Service Calls</td>
<td class='totalTally'>Total Calls</td>
</tr>
<tr>
<td>`$valueRetrieved` - `$valueRetrieved2`</td>
<td>$customDateTrafficPlusTally</td>
<td>$customDateNewVehicleTally</td>
<td>$customDateUsedVehicleTally</td>
<td>$customDateServiceTally</td>
<td>$customDateTally</td>
</tr>
</table>
<div id='customDate_individualPhone'>
<h4>All Individual Phone Calls For `$valueRetrieved` - `$valueRetrieved2`</h4>
<table>
<tr>
<td>Date</td>
<td>Ad Source</td>
<td>Caller Phone Number</td>
<td>Tracking Phone Number</td>
<td>Target Phone Number</td>
<td>Call Duration (in seconds)</td>
<td>Result of Phone Call</td>
<td>Call Listen Link</td>
</tr> " . $showTheCustomDatePhoneTable ."
</table>
</div>
</div>
"; ?>
jQuery below:
$('#customDate_individualPhone').hide();
$('.showCustomDatePhone').click(function(){
var txt = $("#customDate_individualPhone").is(':visible') ? 'View & Listen to Individual Phone Leads >>' : '<< Hide Individual Phone Leads';
$(".showCustomDatePhone").text(txt);
$('#customDate_individualPhone').toggle();
});
The ajax to retrieve the data (if that helps)
$('#getDateRange').on('click', function(){
var startDate = $('#dateStart').val();
var endDate = $('#dateEnd').val();
if ($.trim(startDate) != '') {
$.post('../../includes/dateRangeWhitePlainsNissan.php', {dateStart: startDate, dateEnd: endDate}, function(data){
$('#fromDatabase').html(data);
});
}
});
I'm still very new to programming so you're help is greatly appreciated! Thanks

You'll need to re-initialize your click functions with a callback, or convert them to use on().
$('.showCustomDatePhone').on('click', function(){});

Related

avoiding a page reloading and scrolling for a page with many submit buttons

I am displaying a status of a device on a webpage. I want to make sure that the operator of the device sees the status. Device sends status every minute. So my page updates the data every minute. there is a form button corresponding to each status so that the operator can click (ACCEPT STATUS) it and update a table in my database (SEENORNOT). The problem is, if there is no operator for a while, the page will be filled up with data. In that case, the operator has to scroll back each time after clicking a button. I mean, for example, there is 100 status to accept at the moment. if I can see a maximum of 20 numbers on the screen without scrolling if I scroll to any value more than 20 and accept it, the screen will go back to the initial stage. I am trying to avoid this scrolling and Update the page without a refresh. Any suggestions. At the moment I am refreshing the entire page to update data every minute. I hope i can avoid it using ajax.
#db connection and getting data here- not shown here
<div class ="deviceStatus">
<table class="myTable">
<tr>
<th >Status</th>
<th >Seen or Not</th>
<th >Button to accept</th>
</tr>
<form class="" action="status.php" method="get">
<tr>
<?php
echo "<td>" . $row["STATUS"]. "</td>";
echo "<td>" . $row["SEENORNOT"]. "</td>";
echo "<td>"." <input type=hidden name=hidden value =" . $row["ID"]." </td>";
echo "<td>"." <input type=submit name=accept value=Accept id=button1 class = MyButton"." </td>";
?>
</tr>
</form>
</table>
</div>
# on button press I am updating the DB table (SEENORNOT) value using the ID.
First of all, please use alternate syntax when using PHP and HTML.
#db connection and getting data here- not shown here
<div class ="deviceStatus">
<table class="myTable">
<thead>
<tr>
<th>Status</th>
<th>Seen or Not</th>
<th>Button to accept</th>
</tr>
</thead>
<tbody>
<?php foreach($query as $row): ?> <!--Please update $query with your resultset-->
<tr>
<td><?=$row["STATUS"]?></td>
<td><?=$row["SEENORNOT"]?></td>
<td>Accept</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
# on button press I am updating the DB table (SEENORNOT) value using the ID.
Second, please don't be scared to use Ajax, because it is what you will need in this case.
I gave you a headstart here, please use it.
<script>
$('.accept_btn').click(function(e) {
e.preventDefault();
let button = $(this).attr('tag');
alert('Clicked button with row #' + button);
});
</script>

Put text between autofilled PHP-Table

I'm currently working on a table that contains 'Name', 'Info', 'Price' and 'Duration'. However the PHP-Script is connected to a database.
I want to autofill the tablecells: 'Name', 'Price', 'Duration' via the Database by using PHP and SQL and that works perfectly fine for me. Though, I want to customize the content that's in the individual Info cells (e.g. Readmore jQuery and redirect to other pages).
I tried a little bit with using tags inside the Database and other weird stuff which, obviously, didn't work.
Is there a more elegant way of solving my problem than setting up a complete normal table without PHP/SQL, where I'd have to put in every bit of data about Name,Price and Duration manually?
<table class="table table-bordered table-hover tablesorter row sortable">
<thead>
<tr>
<th class="header">Name</th>
<th class="hidden-xs">Info</th>
<th>Price (in Euro)</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
<?php
//Connect to Database
$db=mysql_connect ("xxxx", "xxxx", "xxxx") or die ('Oops! Da hat wohl' . mysqli_error(). 'Mist gebaut');
//Choose Database
$mydb=mysql_select_db("massageke_de");
//Query Database
$sql="SELECT * FROM Angebote";
//-run the query against the mysql query function
$result=mysql_query($sql);
//Show Results
while($row=mysql_fetch_array($result)){
//Start table ?>
<tr>
<td class="col-sm-2"><?php echo $Name =$row['Name'] ; ?></td>
<!--In this <td>-tag I want to put long textes with links-->
<td class="hidden-xs col-sm-5">echo $Name =$row['Info'];?<</td>
<td class="col-sm-2"><?php echo $Preis =$row['Preis']; ?></td>
<td ckass="col-sm-1"><?php echo $Dauer =$row['Dauer']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Thanks in advance for helping.
Don't bother asking me additional questions.
P.S.: I hope you can help, without needing the CSS of Bootstrap, that I used.
P.P.S.:I know that my PHP-Script is not protected against PHP-Injection
(but I want to and will learn how to secure it)
Edit: As Jester asked for it. I made it quickly with photoshop because I think an image can express much better, what I want to achieve, than my poorly coding-skills.
Get to the image
Seems to me like the easiest way would be to just edit the info column in the database for each? If you want to do it in php i'd suggest making an array using the names (ids would be better but it seems you don't have access to those?) as keys:
$info['Aroma Teilkoerper'] = "Text about aroma teilkoerper";
$info['Aroma Ganzkoerper'] = "Text about aroma ganzkoerper";
and so on until you have all. Then in the loop:
//Show Results
while($row=mysql_fetch_array($result)){
//Start table ?>
<tr>
<td class="col-sm-2"><?php echo $Name =$row['Name'] ; ?></td>
<!--In this <td>-tag I want to put long textes with links-->
<td class="hidden-xs col-sm-5">echo $Name =$info[$row['Name']];?></td>
<td class="col-sm-2"><?php echo $Preis =$row['Preis']; ?></td>
<td ckass="col-sm-1"><?php echo $Dauer =$row['Dauer']; ?></td>
</tr>
<?php } ?>
Hoping that is a workable solution for you? (also you had a syntax error in your closing php tag.
echo $Name =$row['Info'];?< // <----- should be ?> of course

How to find data and get text from html table td element next to it, without attribute using PHP Simple DOM?

I am using simple_html_dom.php for this task.
I wounder how can I get the value (plaintext) "Data c" or "Data F" from this kind of a table?
TD elements doesn't have any attributes
The "Data c" or "Data F" values can have different position - different indexes.
Is there a way frist to find td with value "Data A" and then using next_sibling() or previous_sibling() get and output the value "Data C"?
How to do this "find and get the data" next/before to the data that web have inside HTML table?
<table class="xyz">
<tbody>
<tr>
<td>Data A<p>data b</p>
</td>
<td>Data C</td>
</tr>
<tr>
<td>Data D<p>data e</p>
</td>
<td>Data F</td>
</tr>
</tbody>
</table>
Or should I use some other tehnique?
Please could You help me with that?
Thank You!
You can do it quite easily retrieve the content from the various table cells with javascript - not sure if this is what you mean?
<script>
var col=document.querySelectorAll('table.xyz td');
for( var n in col ) if( n && col[n] && col[n].nodeType==1 ) console.log( 'Cell:%d Type:%s Value:%s',n,col[n].tagName, col[n].innerHTML );
</script>
will output
-----------
Cell:0 Type:TD Value:Data A<p>data b</p>
Cell:1 Type:TD Value:Data C
Cell:2 Type:TD Value:Data D<p>data e</p>
Cell:3 Type:TD Value:Data F

PHP: Delete checked rows

I will have to mention first that I have searched for a Google and stackoverflow and anywhere else, as well as tried to use scripts given in forums and write my own ones, but nothing worked for me. I am completely stuck.
So, all I try to do is to write a script that will delete checked rows from MySQL table. Here is my HTML written inside of a PHP file:
<tr class="noP">
<td class="check"><input class="checkbox" name="checkbox[]" type="checkbox" value="'.$row["PID"].'"/></td>
<td class="id">'.$row['PID'].'</th>
<td>'.$row["name"].'</th>
<td>'.$row["surname"].'</th>
<td>'.$row["pcode"].'</th>
<td class="address">'.$row["address"].'</th>
<td class="email">'.$row["email"].'</th>
<td>'.$row["phone"].'</th>
<td class="education">'.$row["education"].'</th>
<td class="remarks">'.$row["remarks"].'</th>
</tr>
for here $row = mysql_fetch_assoc($qParts);, so this array is just a collector of field values from MySQL DB.
Basically, all I try to do is just a table with all the participants listed with ability to delete selected ones.
I would highly appreciate any help provided. Thank you!
This should help you:
foreach($_REQUEST['checkbox'] as $val)
$delIds = intval($val);
$delSql = implode($delIds, ",");
mysql_query("DELETE FROM table WHERE PID IN ($delSql)");
So, that takes your input array from $_GET/$_POST, sanitises it (a little), then implodes it to get a list of IDs (e.g. 5, 7, 9, 13). It then feeds that into an SQL statement, matching on those IDs using the IN operator.
Note that you should do this using prepared statements or similar. It's been a while though, so I can't write them off-hand, but this should give you the gist of it.
To do this using PDO, have a look here. It's a bit more complex, since you need to dynamically create the placeholders, but it should then work the same.
Reference - frequently asked questions about PDO
I think I can help you out. I had the same issue during my semester project. The problem can be solved using HTML and PHP alone.
I am assuming that PID is the primary key in your table. The trick here is to put the entire table in a form so that it looks like this:
<form action="/*NAME OF THIS PAGE HERE*/.php" method="POST">
<?php
if(isset($_POST['delete'])) //THE NAME OF THE BUTTON IS delete.
{
foreach ($_POST["checkbox"] as $id){
$de1 = "DELETE FROM //table-name WHERE PID='$id'";
if(mysqli_query($conn, $de1))
echo "<b>Deletion Successful. </b>";
else
echo "ERROR: Could not execute";
}
}
if(isset($_POST['delete'])){
echo"<b>Please make a selection to complete this operation.</b>";
}
?>
</br>
<!-- below you will see that I had placed an image as the delete button and stated its styles -->
<button type="submit" name="delete" value="delete" style="float:right;border:none; background:none;">
<img style="width:55px; height:55px;margin-left:10px; margin-bottom:6px; float:left;" src="images/del.png">
</button>
<table>
<thead>
<tr>
<th>#</th>
<th>PID</th>
<th>NAME</th>
<th>SURNAME</th>
<!--REST OF THE TABLE HEADINGS HERE -->
</tr>
<?php $fqn="SELECT * FROM //table-name here;
$fqn_run=mysqli_query($conn,$fqn);
while($row=mysqli_fetch_array($fqn_run)):?>
</thead>
<tbody>
<tr class="noP">
<td class="check"><input class="checkbox" name="checkbox[]" type="checkbox" value="<?php echo $row["PID"];?>"></td>
<td><?php echo $row['PID'];?></td>
<td><?php echo $row["name"];?></td>
<td><?php echo $row["surname"];?></td>
<!-- REST OF THE ROWS HERE -->
</tr><?php endwhile;?>
</tbody>
</table>
</div>
</div>
</form>
Hope this helps you.

Hiding a description in a dynamic table

I have a table that is created using PHP and when you click on the name of the item I want the description to show.
<table border="1" bordercolor="#000000">
<tr class="trintial">
<th width="50px"><input type="checkbox" readonly="readonly" checked="checked" /></th>
<th width="300px">To-Do Item</th>
<th width="100px">Priority</th>
<th width="100px">Due Date</th>
</tr>
<?php
$i=0;
while ($i < $num)
{
$entrynum=mysql_result($result,$i,'todo.entry');
$f2=mysql_result($result,$i,'todo.item');
$f3=mysql_result($result,$i,'todo.priority');
$f4=mysql_result($result,$i,'todo.duedate');
$f5=mysql_result($result,$i,'todo.description');
?>
<tr id="<?php echo "row$entrynum"; ?>" class="trintial">
<td><input type="checkbox" onchange="change(this,<?php echo "row$entrynum"; ?>)" name="<?php echo "box$entrynum"; ?>" value="checked" /></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>
<td><?php echo $f4; ?></td>
</tr>
<?php
$i++;
}
mysql_close($con);
?>
</table>
i need it to ouput when the text of $f2 is clicked
I like this view of the world. You really can't do that. Because what you don't echo with PHP, the user never receives in his browser. (Remember: PHP is executed server-side only, HTML, CSS, JavaScript are "executed" in the users browser.)
To make your description appear when $f2 is clicked, you'll need to pack it into your html and then hide it with CSS. After that you'll need to write some smart JavaScript which overwrites the CSS of your description, when $f2 is clicked. That's not trivial, but it's doable. If you need some help with it, post exactly what you understand under "show"ing the description, because there are lots of ways to do this.
Supertech, I get the impression that you are confusing server-side code (PHP in this case) with client-side code. PHP doesn't run in the user's browser. Once the page data has been sent, PHP is no longer running. What you want to do is create some Javascript to modify the style (CSS) of the elements on the page.
Start by outputting your table as it would look with everything expanded. Once this is done, add a CSS class to all of the elements, such as "hidden". Then in your CSS for the page, define this class with something like this
.hidden {
display:none;
}
Now, you need to add the appropriate Javascript to links to display. Something like this should do
Click Me
If all is well, yourelement will now be displayed.
You can read up on the CSS display property here:
http://www.w3schools.com/css/pr_class_display.asp
And, the Javascript necessary to change the style here:
http://www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/
I hope this helps you understand how all of these essentially unrelated pieces fit together in your web application.

Categories