data remains at grid on postback

Questionsdata remains at grid on postback
Ronald asked 10 years ago

Hi!

I would like to set new sql statement and update data of grid after the user press button in my page. I changed my sql and update grid ($g->select_command=sql;) but old data remain in page.

Any ideas?

5 Answers
Abu Ghufran answered 10 years ago

The columns of grid are rendered first time, and then only data is fetched using ajax postback.
Most likely the new sql columns are not matching the rendered columns, and not loading data in it.

If you are changing sql that have different columns, you need to refresh page using location.reload();

Ronald answered 10 years ago

Abu

There is not new columns, just change a parameter in my own filter criteria.

When user click on button "Show date", I wanna do a submit for same page, and show new data based on filter textbox. See steps below:

1 – original sql

2- Show grid with all data in firt time.

3 – User clik on button "Show date".

4 – in my php page ShowResults.php:

$bAction = $_POST['btnAction'];
$txtFilter = $_POST['txtDateFilter'];

// first accessing to page
if($bAction=="")
{
$sql = "SELECT * FROM tb_test1";
}
// postback
else{
$sql = "SELECT * FROM tb_test1 WHERE tb_test1.date=" . $txtFilter;
}
$g = new jqgrid();
$g->select_command = $sql;
$g->table = "tb_test1";

"SELECT * FROM tb_test1" is fine. But when user click on submit button, grid don't show data with new criteria ("SELECT * FROM tb_test1 WHERE tb_test1.date=" . $txtFilter).

I used echo $sql to check if $sql variable is ok.

So … How do I update my grid datasource?

Is there a "refresh" method to update grid datasource? Is ajax code necessary to show data based on new sql? If yes, how do it?

Thanks in advanced.

Abu Ghufran answered 10 years ago

No special work required.
You need to preserve the $_POST data in session.

Refer this faq:

Q) How to load grid based on $_POST data from other page?

The grid is loaded with 2 server calls.

1) load the columns of grid.
2) do an ajax call, to load data of grid.

Now, if you want to pass data from external form, it is available for step1. But not there in 2nd ajax call, as it is not posted.
Solution is to put the POST variable in session and use it from session for step2.

e.g.

if (!empty($_POST["personid"]))
{
$_SESSION["personid"] = $_POST["personid"];
}
$pid = $_SESSION["personid"];

and use `$pid` in your SQL.

Ronald answered 10 years ago

It worked! Great 🙂

Now I have a new problem. I set $_SESSION["blablabla"] = "" in another page. But when I return to first page, the old Session value remains.

I love phpgrid and I really want to understand how it works.

I din't understand very well how phpgrid works with ajax call to update data. I read all examples, faq and documentation, but I didn't understand yet.

Please, can you show me phpgrid's life cycle?

Thanks in advanced.

Abu Ghufran answered 10 years ago

The grid does not have any persistance storage. Even when you post data from other page to grid, it can't save that data, unless you use session or some other alternate.

As grid is loaded in 2 calls, 1st is non-ajax which make structure of grid. 2nd is ajax call that loads data in grid.
The externally posted data is available at 1st server call but not at ajax call.

If you are having a data persistence issue, you can have put check something like ..

if (non ajax call and no posted data)
reset session;

I've not tested it but it should work.

$is_ajax = (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

Your Answer

14 + 1 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?