This topic is locked
[SOLVED]

 Add Combobox to Login Page

7/10/2020 2:33:59 PM
PHPRunner General questions
A
asawyer13 authorDevClub member

I want to add a combobox to the login page so the user can select which database they want to use.
Right now I have a code snippet.



echo "<html>";
echo "Select Database:";
echo "<select> id='selecteddatabase'";
//echo "<option value='Select'>Select</option>";

echo "<option value='Dev'>Dev</option>";

echo "<option value='Test'>Test</option>";

echo "<option value='Prod' selected>Prod</option>";
echo "</select>";

echo "</html>";


which seems to show what I want.
Which event and how can I look at what they've selected so I can open the correct database?
Thanks

Sergey Kornilov admin 7/11/2020

First, you need to fix your code snippet.

echo "Select Database:";

echo "<select name='database' id='selecteddatabase'>";
echo "<option value='Dev'>Dev</option>";

echo "<option value='Test'>Test</option>";

echo "<option value='Prod' selected>Prod</option>";
echo "</select>";


Then in an event like Login Page BeforeProcess you can read the value of $_POST["database"] and do something with it:

if ($_POST["database"]=="Dev")

$_SESSION["database"]="database1";

if ($_POST["database"]=="Test")

$_SESSION["database"]="database2";

if ($_POST["database"]=="Prod")

$_SESSION["database"]="database3";


Then you can create a new Server Database Connection on the Output directory screen and use $_SESSION["database"] instead of the hardcoded database name. Something like this:

$host="localhost";

$user="root";

$pwd="";

$port="3306";

$sys_dbname=$_SESSION["database"];
A
asawyer13 authorDevClub member 7/11/2020

Sergey,

Thanks so much.

A
asawyer13 authorDevClub member 7/12/2020

Almost solved..
When I run it locally everything works, it will open the app properly and point to any of my 3 databases.
When I upload to my server and run it, before the login screen even appears I get this error:
Fatal error: -1 Invalid option  was passed to sqlsrv_connect.

in /var/www/vhosts/xxxxx.com/xxxxx.com/connections/Connection.php on line 626.
Line 626 is an error routine from what I can see, so doesn't help me directly.
I am using MSSQL. I replaced my domain name with the xxxxx. So it's opening mssql prior to displaying the login page.
I did try adding the setting of the session variable in the app initialization stuff but that didn't change the error. Just tried setting it to Prod.
If I change the connection back to the default, and send to the server, everything works, so I'm not sure what its unhappy about.
Any help would be appreciated. I know this probably isn't enough info to debug this, but if you can tell me what you need, I can get it for you.
Alan

A
asawyer13 authorDevClub member 7/12/2020

I'm thinking it has to be an issue with the session variable not being set, but I did try setting it in the after app initialization event.
I must be doing something a little wrong.

A
asawyer13 authorDevClub member 7/12/2020

In case I made a typo or something this is what's in my Dynamic Connection that I added. I am changing IP and password to protect the info.
$host="nn.nn.nnn.nn";

$user="cpdbade";

$pwd="xxxxxxx";

$dbname=$_SESSION["database"];

$ODBCString = "Provider=SQLOLEDB;Server=nn.nn.nnn.nn;Uid=cpdbade;Pwd=xxxxxxx;Database=" . $_SESSION["database"];

Sergey Kornilov admin 7/13/2020

Make sure, that the code is added to correct events.
Also, if the very first connection happens before the session variable is populated you need to modify the code of Server Database Connection to check if the session variable is populated.

if (!empty($_SESSION["database"])) {
$host="nn.nn.nnn.nn";

$user="cpdbade";

$pwd="xxxxxxx";

$dbname=$_SESSION["database"];

$ODBCString = "Provider=SQLOLEDB;Server=nn.nn.nnn.nn;Uid=cpdbade;Pwd=xxxxxxx;Database=" . $_SESSION["database"];
} else {
// here goes the connection code that uses the hardcoded database name
}