PHP Grid Component – parameters another program is not working

QuestionsPHP Grid Component – parameters another program is not working
celso asked 11 years ago

EXAMPLE: program1.php ===================================
$query = 'SHOW TABLES'; // all the tables
$tabelas_consulta = mysqli_query($com, $query);
?>
<table border="0" width="100%" height="10" cellspacing="0">

<form method="POST" action="seleciona.php">
<p class="pergu"> Selecione TABELA:
<select name="tabela">

<?php
while ($tabelas_linha = mysqli_fetch_row($tabelas_consulta)) {
$tabelas[] = $tabelas_linha[0];
}
mysqli_close($com);
//
$i = 1;
while ($i < count($tabelas)) {
echo "<br><option value="$tabelas[$i]">$tabelas[$i]</option>" ;
//echo "<br><option value="$i">$tabelas[$i]</option>" ;
$i++;
}
?>
</select>

<table border="0" width="80%" cellspacing="0" cellpadding="1">
<tr>
<td width="80%"><input type="submit" value="CONSULTAR" class="Button"></td>
</tr>
</table>

</form>

</table>

EXAMPLE: program2.php ===================================
<?php
/**
* PHP Grid Component
*
* @author Abu Ghufran <[email protected]> – http://www.phpgrid.org
* @version 1.4.6
* @license: see license.txt included in package
*/
$tab_select = $_POST["tabela"];
….
grid["caption"] = "TABLE =".$tab_select;
$grid["multiselect"] = true;
$g->set_options($grid);

// set database table for CRUD operations
$g->table = $_POST["table"];

PROBLEM: First time it is working but second time when I change of option to another "table" is not working.
Can you help me about this problem?
Thanks,
//

9 Answers
celso answered 11 years ago

REMEMBER
// set database table for CRUD operations
$g->table = $_POST["tabela"];

celso answered 11 years ago

Sorry, the "program2.php" is the "seleciona.php".

Abu Ghufran answered 11 years ago

Pasting from 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 code.

celso answered 11 years ago

I want to pass data from external form.

Abu Ghufran answered 11 years ago

At top of program 2,

if (!empty($_POST["tabela"]))
{
$_SESSION["tabela"] = $_POST["tabela"];
}
$tab = $_SESSION["tabela"];

and in code use this variable.

// set database table for CRUD operations
$g->table = $tab;

CELSO answered 11 years ago

Please can you analyze these programs? Today it is not working. Thanks.

<?php
// Source: program1.php
///////////////////////
$db = Array();
$db = Array(
"host"=>"www.example.com",
"user"=>"user",
"senha"=>"password",
"nome"=>"database"
);

$com=mysqli_connect($db[host], $db[user], $db[senha],$db[nome]);
if (mysqli_connect_errno($com)) {
printf("FALHA NA CONECAO: %sn",mysqli_connect_error());
exit();
}

$query = 'SHOW TABLES'; // all the tables
$tabelas_consulta = mysqli_query($com, $query);
?>

<table border="0" width="100%" height="10" cellspacing="0">

<form method="POST" action="seleciona.php">
<p class="pergu"> Selecione TABELA:
<select name="tabela">

<?php
while ($tabelas_linha = mysqli_fetch_row($tabelas_consulta)) {
$tabelas[] = $tabelas_linha[0];
}
mysqli_close($com);
//
$i = 1;
while ($i < count($tabelas)) {
echo "<br><option value="$tabelas[$i]">$tabelas[$i]</option>" ;
//echo "<br><option value="$i">$tabelas[$i]</option>" ;
$i++;
}
?>
</select>

<table border="0" width="80%" cellspacing="0" cellpadding="1">
<tr>
<td width="80%"><input type="submit" value="CONSULTAR" class="Button"></td>
</tr>
</table>

</form>

</table>

==================================================================
<?php
// Source: program2.php
/////////////////////////////////
if (!empty($_POST["tabela"]))
{
$_SESSION["tabela"] = $_POST["tabela"];
}
$tab = $_SESSION["tabela"];

$db = Array();
$db = Array(
"host"=>"www.example.com",
"user"=>"user",
"senha"=>"password",
"nome"=>"database"
);

// set up DB
$conn = mysql_connect($db[host], $db[user], $db[senha]);
mysql_select_db($db[nome]);

// set your db encoding — for ascent chars (if required)
mysql_query("SET NAMES 'utf8'");

// include and create object
include("inc/jqgrid_dist.php");
$g = new jqgrid();

// set few params
$grid["caption"] = "TABELA SELECIONADA = <B>".$tab."<B>";
$grid["multiselect"] = true;
$g->set_options($grid);

// set database table for CRUD operations
$g->table = $tab;

// render grid
$out = $g->render("list1");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
<html>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="js/themes/redmond/jquery-ui.custom.css"></link>
<link rel="stylesheet" type="text/css" media="screen" href="js/jqgrid/css/ui.jqgrid.css"></link>

<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script src="js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
</head>
<body>
<div style="margin:10px">
<?php echo $out?>
</div>
</body>
</html>

Abu Ghufran answered 11 years ago

Everything seems fine is code. I'll email you sample code.
http://pastebin.com/UMk2x5zP

Yann answered 11 years ago

I have the same problem

$tab variable is not passing

———————————————-
<?php

/**
* PHP Grid Component
*
* @author Abu Ghufran <[email protected]> – http://www.phpgrid.org
* @version 1.4.6
* @license: see license.txt included in package
*/

// set up DB
$conn = mysql_connect("localhost", "root", "root");
mysql_select_db("air004");

// set your db encoding — for ascent chars (if required)
mysql_query("SET NAMES 'utf8'");

// preserve selection for ajax call
if (!empty($_POST["tables"]))
{
$_SESSION["tab"] = $_POST["tables"];
$tab = $_SESSION["tab"];
}

echo $tab;

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

$g = new jqgrid();

// set few params
$grid["caption"] = "Grid";
$grid["multiselect"] = true;
$g->set_options($grid);

// set database table for CRUD operations
$g->table = $tab;

// subqueries are also supported now (v1.2)
// $g->select_command = "select * from (select * from invheader) as o";

// render grid
$out = $g->render("list1");
?>
———————————————-

Abu Ghufran answered 11 years ago

Looks like both are not starting session before $_SESSION usage.
Put this at line 0

session_start();

Also refer my pasted working code from this link: http://pastebin.com/UMk2x5zP

Your Answer

17 + 9 =

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?