Ajax call doesn't work

QuestionsAjax call doesn't work
Antonis Kosmopoulos asked 11 years ago

Hello again,

I'm sure where I'm stumped at is something trivial, but here goes:

I have a phpgrid in a div populated based on some initial values where the page loads, and in the same page I have 3 dropdown select controls based on the values of which I want to update the control. I do that through an Ajax call to a php page that builds the grid whenever the values of the select controls are changed:

xmlhttp.open("GET","deltap.php?month="+cm+"&year="+cy+"&name="+cn,true);

and set the .innerHTML property of the div to the xmlhttp.responseText. The ajax call executes, but the div does not display the updated grid, instead it stays blank (if I put a custom echo statement in the deltap.php file, then it displays in the div so the ajax call seems to work).

If I manually call the deltap.php page providing the parameters on the address line, the the grid displays as it should (on a page of its own which is not the functionality I'm aiming for).

Any ideas what I'm doing wrong?

Thanks,

Antonis

15 Answers
Antonis Kosmopoulos answered 11 years ago

I'd like to add a clarification; I don't want to display the grid in a new page based on $_POST variables of the previous page; that's simple and it works fine. I want to refresh the grid on the same page based on the changed values of the select controls.

Antonis

Antonis Kosmopoulos answered 11 years ago

In order to simplify the question even further, what I need to do is refresh my grid, but with a different (changed) SQL select command.

varun answered 11 years ago

use JQuery onchange event with all the $_POST variables coming from previous page and the new value and use window.location.reload()

Antonis Kosmopoulos answered 11 years ago

Hello Varun,

Thank you for your answer but my problem is that there are no $_POST variables coming from a previous page. The variables change the current page on client side through select controls, so I'm sending them to a php script on the server through an ajax call.

varun answered 11 years ago

Post code

varun answered 11 years ago

If i am not wrong u r trying to do smething like this right?

Load the page with grid and some sql statements in form and u need to update the grid with whatever result u get from ajax or server of that sql query?right

Antonis Kosmopoulos answered 11 years ago

Correct. The code is kind of extensive but here it goes. When the select controls are changed the following javascript code executes:

function dAjax()
{
var xmlhttp;
var cm = document.getElementById("cMonth").value;
var cy = document.getElementById("cYear").value;
var cn = document.getElementById("cName").value;
if (cm==0 || cy==0 || cn.length==0)
{
document.getElementById("dpd").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("dpd").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","deltap.php?month="+cm+"&year="+cy+"&name="+cn,true);
xmlhttp.send();
}

While the deltap.php file builds the grid, and echoes the $out grid rendered structure (and if called by itself, it displays the grid just fine in a page of its own (please ignore the unreadable greek characters – also I left out several column definitions because the code becomes too large for me to post):

<?php
session_start();

if (isset($_GET["name"]))
{
$name=$_GET["name"];
$month=$_GET["month"];
$year=$_GET["year"];
//echo "vars from get<br>";
}
else
{
$name=$_SESSION["name"];
$month=$_SESSION["month"];
$year=$_SESSION["year"];
//echo "vars from session";
}

// set up DB
$conn = mysql_connect("localhost", "user", "pass");
mysql_select_db("myDB");
// set your db encoding — for ascent chars (if required)
mysql_query("SET NAMES 'utf8'");

// include and create object
include("lib/inc/jqgrid_dist.php");

/////////////////////////////
// grid deltia apasxolisis //
/////////////////////////////
$grid_da = new jqgrid();

// set params
$opt_da["multiselect"] = false;
$opt_da["caption"] = ""; //"Ðßíáêáò Áðáó÷üëçóçò";
$opt_da["sortname"] = 'id';
$opt_da["sortorder"] = "asc";
$opt_da["autowidth"] = false;
$opt_da["scroll"] = true;
$opt_da["hidegrid"] = false;
$opt_da["cellEdit"] = true;
$opt_da["shrinkToFit"] = false;
$opt_da["width"] = "1206";
$opt_da["height"] = "160";
$grid_da->set_options($opt_da);
$grid_da->set_actions(array("autofilter" => false, "search" => "advance"));

$grid_da->select_command = "SELECT * FROM timesheets".$year." WHERE year=".$year." AND month=".$month." AND employee='".$name."' AND overtime=0";

$col_da = array();
$col_da["title"] = "ÐáñáäïôÝï";
$col_da["name"] = "deliverable";
$col_da["width"] = "141";
$col_da["search"] = false;
$col_da["editable"] = true;
$col_da["align"] = "left";
$col_da["edittype"] = "select";
$col_da["editoptions"] = array("value" => ":;ÔÅÕ×ÏÓ:ÔÅÕ×ÏÓ;Ó×ÅÄÉÏ:Ó×ÅÄÉÏ", "style" => "width: 141px");
$col_da["formatter"] = "select";
$cols_da[] = $col_da;

for ($dd=1; $dd<=cal_days_in_month(CAL_GREGORIAN, date('n'), date('Y')); $dd++)
{
$tt=strtotime(date('Y')."-".date('m')."-".$dd);
$ddd=date('D',$tt);
if ($ddd=='Mon') {$ddg='Äå';}
if ($ddd=='Tue') {$ddg='Ôñ';}
if ($ddd=='Wed') {$ddg='Ôå';}
if ($ddd=='Thu') {$ddg='Ðå';}
if ($ddd=='Fri') {$ddg='Ðá';}
if ($ddd=='Sat') {$ddg='Óá';}
if ($ddd=='Sun') {$ddg='Êõ';}

if ($ddd=='Sat' || $ddd=='Sun')
{
$f = array();
$f["column"] = "d".$dd;
$f["css"] = "'background-color':'#00ffff'";
$f_conditions[] = $f;
}
$col_da = array();
$col_da["title"] = "<b>".$ddg."</b><br>".$dd;
$col_da["name"] = "d".$dd;
$col_da["width"] = "19";
$col_da["search"] = false;
$col_da["align"] = "center";
$cols_da[] = $col_da;
}
$grid_da->set_conditional_css($f_conditions);

$col_da = array();
$col_da["title"] = "¿ñåò";
$col_da["name"] = "thours";
$col_da["width"] = "26";
$col_da["formatter"] = "number";
$col_da["formatoptions"] = array("thousandsSeparator"=>",","decimalSeparator"=>".","decimalPlaces"=>'0');
$col_da["search"] = false;
$col_da["align"] = "center";
$cols_da[] = $col_da;

$grid_da->set_columns($cols_da);
$out_da = $grid_da->render("list5");

?>

<html>
<head>

<link rel="stylesheet" type="text/css" media="screen" href="lib/js/themes/smoothness/jquery-ui.custom.css"></link>
<link rel="stylesheet" type="text/css" media="screen" href="lib/js/jqgrid/css/ui.jqgrid.css"></link>
<script src="lib/js/jquery.min.js" type="text/javascript"></script>
<script src="lib/js/jqgrid/js/i18n/grid.locale-el.js" type="text/javascript"></script>
<script src="lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script src="lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script&gt;
<!– these css and js files are required by php grid –>
<!–[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script&gt;
<![endif]–>
</head>
<body>
<?php
echo $out_da;
?>
</body>
</html>

varun answered 11 years ago

Are those cMonth,cYear and cName part of deltap.php?

varun answered 11 years ago

Assuming that all those 3 dropdowns are from same page which has grid,let me just tel u how to reload page with selected values in dropdown.

function dAjax()
{
var cm = document.getElementById("cMonth").value;
var cy = document.getElementById("cYear").value;
var cn = document.getElementById("cName").value;

if this 3 are not inter-related then try to load code for cName.onchange,while assuming u have selected cMonth and cYear(if not display alert to select).

$("#cName").change(){function (
url=deltap.php?month="+cm+"&year="+cy+"&name="+cn;
window.location.reload(url);
)}
}

something like this will help u load same deltap.php page once again with all the selected values in dropdowns and put it in ur address bar,further ur GET_methods will do all the work for deltap.php

Abu Ghufran answered 11 years ago

Hello Antonis,

Please check demos/search/search-form.php file.
It uses internal ajax call of grid to refresh data based on your filter.

Regards,

Antonis Kosmopoulos answered 11 years ago

Hello Abu and Varun,

First of all, thank you both for your help.

Abu, I have seen the search-onload.php (can't find search-form.php, maybe it's not in the premium version?), and I get the JSON search filter.

But my problem is that I don't want to reload the page based on the values of my select controls – that's easy. I want only to reload the grid based on the new search parameters. That means that I either need to pass them to the grid through javascript/jquery or what I'm trying to do (set the innerHTML property of the div inside which my grid is to the response of an external ajax call to the php file that creates the grid – which fails as the grid never appears).

Maybe also I don't understand how to use the $grid["url"] parameter, which could be a solution to what I'm looking for.

Thanks you again for your time.

varun answered 11 years ago

$grid["url"] is something u go from grid to new tab where u will use grid data,as i know u r trying to send some data into grid using <div>. best solution would be to reload same page with data in the new reloaded url. That solves ur problem

Abu Ghufran answered 11 years ago

Demo allow you to pass search data from html tags to grid ajax call.
I have emailed you the missing file.

Regards,

Antonis Kosmopoulos answered 11 years ago

Thank you Abu, the search function works well.

Varun, also thank you for your suggestion. I have done that, as it is the obvious way, I was just looking to see if I could avoid reloading the page.

Abu, one last observation though: the search is performed through the recordset that the grid contains since it was built by the php code.

So, if I wish to load a new recordset (or even change the DB table from which I take the recordset), the only option I have is to reload the page so the php executes again server-side, correct? Or I can do that using jquery calls?

Finally, is there any example documenting the $grid["url"]=…. option?

Thanks for your patience,

Antonis

Abu Ghufran answered 11 years ago

Hello,

The dropdown reload after record modifications is still pending, perhaps you found an alternate so i am closing ticket for now.
The "url" is the setting on which the ajax call is sent for modification purpose. It is usually same unless custom implementation is done.

Regards

Your Answer

18 + 17 =

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?