PHP: JSON/AJAX live update to display data - php

Hi everyone,
When I delete first record, my table will live update but the order number remained '2',
is it possible to make it change to '1' without refresh page,
currently I am using ajax to do it, and found json_encode & json_decode maybe able to solve my problem?
$.ajax({
type:"POST",
url:"delete_item.php",
data:{id:del_id},
success:
function(){
}
});
$(this).parents(".record").animate('fast').animate({opacity:'hide'},'slow');
This is for display my data
<tbody id='item_list'>
<?php eitem_listItem($page, $record,$search); ?>
</tbody>
Sorry for my poor english.

The easiest way of addressing this is to hide the actual id of the data row (presumably you are using id as the unique identifier for your query) - so have it as a data attribute or an id for the delete button (to give you the del_id) or other method of keeping it in the data but not displayed.
Then when you are iterating through your data to display it - have a javascript count set up so that on the display of the data - each <tr> row gets its displayed index from this count - rather than the id of the actual data.
This means that you will display a 1 for the first displayed row - but the actual id that you use for the delete button may be 2 for example.
So the number on the side of the table becomes a displayed index that will always match the actual sequence of the data rows, but not necessarily will match the id of the row.

Related

mysql - Query to display record just added

I have two pages. One is a form that I use to simply input data that will be sent to my database and the second page that actually takes the data inputted into the form and sends it to the database and is supposed to display the information that I've just added.
Everything works fine, however I'm struggling with the query slightly. What I need it to do is display all the information for the last data inputted to the database.
The query I currently have just displays the data with the highest ID:
$sql = "SELECT * FROM Results ORDER BY ID DESC LIMIT 1";
So as an example I would be left with the following information after completing my form:
Success! Data being saved:
ID = 900 Amount = 206 Date = 2016-12-26
This is obviously just showing the data with the highest ID, but since the ID and all the data fluctuates, I need it to just show the data that has just been inputted.
I came accross this: Query to select newly added records only. But I don't believe this soultion to be viable as the database is external and I don't want to be creating new tables.
I was thinking that it might be possible to assign a hidden value to each newly added record via the query. e.g. New 1, New 2, New 3 etc. Then printing the latest record for New. However, I couldn't find anything on how to do this.
Any help would be greatly appreciated!
You must use this method to have very correct value:
Input form must send to another file that do inserting (we call it here insert.php)
insert.php must insert the data after validation and after that you can fetch the last ID number from database. Depending on the method you are working with it can be different. for example if you are using PDO you can get it by PDO::lastInsertId
after getting the ID you need to forward it to the viewing or editing page. for example view.php?id=LastInsertId. This forward have some reasons:
Codes can be cleaner.
We prevent refresh and resend inserting. for example if you do inserting inside view.php and user hit F5 to refresh the page, The insertion happening again.
This is the whole idea. you can use this method for only one page:
page.php?do=new
page.php?do=insert
forward to the page.php?do=view&id=lastInsertID
why you trying to get just inputted data from database? you can do it using HTTP POST/GET method easily.just send data as parameters and show them in second page.
If you already have the data you are inserting, you don't need to run a query to get it back from the database again, you could just ensure that the query was successful and display the data directly. Anyways:
You can get the insert ID from the last insert using the MySQLi object. For example:
$sql = "<your insert statement>"
$conn->query($sql);
$last_id = $conn->insert_id; //Id of the row you just inserted
$sql = "SELECT * FROM Results WHERE id=$last_id";
This is assuming you do the insert in the same page that you display the result.

How to display the different php tables given an item from php combobox?

I have different queries that I recover and I want to display each query in php table when an item is selected in the combobox
For example if I do click in the first item below should display the corresponding table given the query that this contains, this if is possible using ajax or als in other page
I was trying to do with jquery but I couldn't.
I think I see what you are getting at, your jquery would be written so on the select change send off a get request to the page that gets the data e.g.
$.get("data.php?query=2", function(data){
console.log(data);
});
I would advise not sending the actual query from the select because it can leave you open for SQL injection. If you have a key for each item then that key would correspond in the data.php file.

PHP POST WITH DATATABLE PAGINATION

Sorry if I am repeating the question, I have a HTML FORM page which displays the employee_id & attendance check box (IN, OUT) using datatable.
I made pagination with initial load value in the screen as 10, the data are loaded from mysql DB and all the data loads perfectly with pagination. I will update attendance status of each employee which checkbox and submit the form.
Once i submit the form, i called the PHP _POST (if(isset($_POST['save']))) , inside this i am trying to get the value of all employee_id & checkbox value but, i can able to get only first 10 rows, remaining in page 2,3,4,5 are not available, is there any option to get them, i tried with jquery for each too but it also alerts on first 10 rows.
Please help me out.
I need to post the data to database from data table regardless of pagination either using PHP post method (or) Jquery.
If you using pagination by LIMIT in database query there is, of course, no way you would get more than 10 rows, so, your <form> submit will not give you more than those 10 data arrays you can save to database.
Can you explain why you need to update all rows in database? Because limiting by pagination guarantees that other rows will not be affected (if query is right).

Editing a row of data in a table using an id

I am stuck with finding a means of being able to edit a specific row in my table. I have so far managed to come up with add,delete and search for a specific item that is stored in my database, i base the delete on a id field. Currently i am using PHP and HTML, could someone please show me a simple example that retrieves data from a database which stores in a table and when a user selects edit. The row that has been selected for edit becomes editable and thereafter updated without duplicating the data.
CLARIFICATION OF My QUESTION
For Editing to work the UI must look like, when the edit button corresponding to particular row is clicked then all the columns with the data should get replaced with text boxes, i.e. row-wise edit form...
Thanks
This is a very vague question, involves a lot of things. Not, just php, mysql or html but mainly javascript. However I will give a simple example, since you must already be able to bring the data onto a table.
Suppose your <tr> structure is similar to this
<tr>
<td class="nameField"><span class="text">My name</span></td>
<td class="controls"><a class="edit">Edit</a></td>
</tr>
Now using jQuery for this
$('a.edit').click(function() {
$(this).closest("tr").find("td").not(".controls").each(function(k,v) {
// ^ find all td but not the one with the controls
var span = $(this).children('span');
//get the elem holding the value
$(this).append('<input type="text" value="'+span.text()+'" />');
// Add a text box with the input inside the td
span.hide();
});
});
Demo
SQL Syntax you are looking for is UPDATE
For example you have an articles table tied to a user with user_id, then you have a list of articles whereby users can select to edit (with edit link or something similar)
Then you'd do something like
mysql_query('UPDATE articles set content = $new_content where id = $article_id');
Don't forget to sanitize $new_content and $article_id, or better yet, use mysqli altogether.

I need to get the entire row data from jqGrid when i click the link in same row using jQgrid

I am new to JQgrid and am unsure how to handle this use case.
When a user clicks on a link in a specific row in jQgrid I need to get the row data with a separator.
Retrieving original row data from jqGrid
OR
Retrieving selected rows from jqGrid
Or else..
What I had done earlier was simpler.. appended the required data in link in a column as queryString.. e.g index.php?id=123 if it can work for u..
var newKey=jQuery("input[name='key']").val();
where 'key' is name of column
The answer for the above question is ,when your are clicking on the rows you will have a unique value for each rows from that we need to get the id and by using the jQgrid function.
With getRowData we can get that row column values.
The sample example is pasted below:
var reqlaborId = jQuery("#list1").jqGrid('getRowData', id);
reqlaborId = reqlaborId.ahcc_laborcan_header_id;
This works fine for me...

Categories