...

View Full Version : PHP Funtion and Form



tress
08-27-2007, 10:24 PM
Hi all,

Hope someone can help with this, having to learn some more of what I think are the complicated bits of php before getting my head around the basics so please excuse anything that I ask that may sound stupid.

I am trying to create a form that posts to a database, one of the fields that posts to the database is a drop down which is pulled from a table, my code is as follows and for the life of me I can get it to work, hope someone can help.

My function is called "drop" and is saved as drop.php in the route directory of my web server.


<?php
function drop($selectName,$curValue,$intID,$strName,$tableName,$strOrderField,$strMethod)

{

// create connection
$connection = mssql_connect("localhost","sa","password");

// test connection
if (!$connection)
die "Couldn't make a connection!";


// select database
$db = mssql_select_db("nai", $connection);


// test selection
if (!$db)
die "Couldn't select database!";

$txt = "<select name='$selectName'>\n";
$txt .= "<option value='-1'>Please select</option>\n";

$strQuery = "select $intID, $strName from $tableName order by $strOrderField";
$rsrcResult = mssql_query($strQuery, $connection);

while($arrayRow = mssql_fetch_assoc($rsrcResult)) {
$intIdField = $arrayRow["$intID"];
$strNameField = $arrayRow["$strName"];

$sel = ( $intIdField == $curValue ) ? " selected='selected' " : "";
$txt .= "<option $sel value=\"$intIdField\">$strNameField</option>\n";
}

$txt .= "</select>\n\n";

return $txt;
}

?>

I am just trying to run a basic test to see if it works by running the following php file called test.php which is also stored in the route directory

<p>Location:<br>
<?php echo drop("intLocation", "", IssueTypeID, IssueTypeDesc, IssueType, IssueTypeID); ?></p>

I get the following error

function drop($selectName, $curValue, $intID, $strName, $tableName, $strOrderField, $strMethod="asc") { // create connection $connection = mssql_connect("localhost","sa","password"); // test connection if (!$connection) die "Couldn't make a connection!"; // select database $db = mssql_select_db("nai", $connection); // test selection if (!$db) die "Couldn't select database!"; $txt = "\n"; $txt .= "Please select\n"; $strQuery = "select $intID, $strName from $tableName order by $strOrderField, $strMethod"; $rsrcResult = mssql_query($strQuery, $connection); while($arrayRow = mssql_fetch_assoc($rsrcResult)) { $intIdField = $arrayRow["$intID"]; $strNameField = $arrayRow["$strName"]; $sel = ( $intIdField == $curValue ) ? " selected='selected' " : ""; $txt .= "$strNameField\n"; } $txt .= "\n\n"; return $txt; }

Just seems to be showing all my code as a text string, am I just calling it all wrong or not naming my files correctly, read lots of tutorials and I cant seem to see what I am doing wrong, also if someone knows how I then use the then select value from the drop down to $_POST in my form it would be much appreciated.

Thanks tress

Fumigator
08-27-2007, 10:44 PM
Your code is not running on a server with PHP installed correctly. Is your PHP file on your local hard drive? Did you install PHP on your machine? Did you upload your PHP file to somewhere on the innernet? Does that somewhere have PHP installed?

tress
08-27-2007, 10:54 PM
Yes I do have PHP running, the latest version from the php web site, I can run basic scripts and the info file to make sure everything is installed correctly . Its running on Windows Server 2003 and is connecting to a SQL Server 2003 database, both SQL Server and IIS and PHP are all installed on the same box, and all testing is done on that box and run locally.

Hope that is of help

Thanks tress

Fumigator
08-28-2007, 12:12 AM
For whatever reason your server doesn't know this is a PHP script. The file extension must be PHP and you must use proper PHP tags. Something is not in place.

However you have the other basic script working on your server is what must be done with this script as well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum