Page 1 of 1

converted from asp to php and problem

Posted: Thu Mar 27, 2003 4:59 am
by ridwan
Hi all I converted the following code from asp to php the function of the script is to create a drop down box that call sit's values from a db and then when the user selects a value from the drop down it shows specific values in certain fields by the way I am using odbc to connect to my db b'cos I am using a ms sql 2k server.

The thing is it works fine in asp but when I converted it I get an error telling me that the id variable is undefined.

Code: Select all

<body>
<select name="Ref_ID" onchange="doSel(this)">
<option value="">Please select</option>
<? while (odbc_fetch_row($result))
	&#123;?>
<option value='location.href='dropdown.php?id=<? odbc_result($result, 'ContentId'); ?>'"
	<?if (intval($id) == odbc_result($result, 'ContentId'))
	&#123;
	echo " selected";
    &#125;
	?>>
<? odbc_result($result, 'publication') ?> </option>
	<? &#125; ?>

</select>

<?if (empty($id))
&#123;
//Select the resulset that is going to be used to query data
$strSQLa = "SELECT * FROM publications WHERE ContentId = '" . $id  . "'";
$result= odbc_exec( $dbConnection, $strSQLa);

?>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
   <td>publication</td>
   <td><INPUT NAME="publication" TYPE="TEXT" VALUE="<? echo odbc_result($result, 'publication'); ?>">
</td>
 </tr>
 <tr>
   <td>contact</td>
   <td><INPUT NAME="contact" TYPE="TEXT" VALUE="<? echo odbc_result($result, 'contactee'); ?>"></td>
 </tr>
</table>
<? &#125; ?>
thanks all

Posted: Thu Mar 27, 2003 5:03 am
by volka
please read Sticky: Before Post Read: Concerning Passing Variables in PHP 4.2+
It's not a sticky thread for nothing ;)

its on by thw

Posted: Thu Mar 27, 2003 5:08 am
by ridwan
I have turned my register globals on by the way to test this script

Posted: Thu Mar 27, 2003 5:56 am
by volka
probably it's only a notice/warning because $id is used wether it has been transmitted or not
e.g.
if (intval($id) == odbc_result($result, 'ContentId'))

Code: Select all

if (isset($id) && intval($id) == odbc_result($result, 'ContentId'))

i see

Posted: Thu Mar 27, 2003 6:22 am
by ridwan
I see what that does ,(define the variable and then tel it what ype it is) but I still get an error on the line where I have my second sql query which goes as follows:

Code: Select all

$strSQLa = "SELECT * FROM publications WHERE ContentId = '" . isset($id)  . "'";
Another thing is that it shows nothing in my drop down list but there are values it's as if they're invisible :?

i see

Posted: Thu Mar 27, 2003 6:25 am
by ridwan
I see what that does ,(define the variable and then tel it what ype it is) but I still get an error on the line where I have my second sql query which goes as follows:

Code: Select all

$strSQLa = "SELECT * FROM publications WHERE ContentId = '" . isset($id)  . "'";
Another thing is that it shows nothing in my drop down list but there are values it's as if they're invisible :?

I think if I can sort the second problem of there not being anything in the drop down list I can get the second issue to go away 'cos then there will be a value which will be passed to the second sql statement by the way I'm just re-including the code as I changed a couple of things:

Code: Select all

<select name="Ref_ID" onchange="doSel(this)">
<option value="">Please select</option>
<? while (odbc_fetch_row($result))
	&#123;?>
<option value='location.href='dropdown.php?id=<? odbc_result($result, 'ContentId'); ?>'"
	<?if (isset($id) && intval($id) == odbc_result($result, 'ContentId'))
	&#123;
	echo " selected";
    &#125;
	?>>
<? odbc_result($result, 'publication') ?> </option>
	<? &#125; ?>

</select>

<?if (empty($id))
&#123;
//Select the resulset that is going to be used to query data
$strSQLa = "SELECT * FROM publications WHERE ContentId = '" . isset($id)  . "'";
$resulta= odbc_exec( $dbConnection, $strSQLa);

?>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
   <td>publication</td>
   <td><INPUT NAME="publication" TYPE="TEXT" VALUE="<? echo odbc_result($resulta, 'publication'); ?>">
</td>
 </tr>
 <tr>
   <td>contact</td>
   <td><INPUT NAME="contact" TYPE="TEXT" VALUE="<? echo odbc_result($resulta, 'contactee'); ?>"></td>
 </tr>
</table>

Posted: Thu Mar 27, 2003 7:08 am
by volka
bool isset ( mixed var [, mixed var [, ...]]) returns TRUE if the variable passed as argument exists, FALSE otherwise.
if (empty($id))
the body of the if-statement is only executed if $id is not defined, is == '' or is == '0'.

Code: Select all

/*
 if there is a variable $id 
 and the numeric value of $id is unequal to 0
 casting $id to (int) removes the need for mysql_escape_string or similar
*/
if (isset($id) && ($id = (int)($id)) != 0) 
{
// assuming ContentId is numeric
	//Select the resulset that is going to be used to query data
	$strSQLa = "SELECT * FROM publications WHERE ContentId='" . $id  . "'";
what is
<option value='location.href='dropdown.php?id=...
supposed to do?

the meaning

Posted: Thu Mar 27, 2003 7:27 am
by ridwan
you see the first page that you go into is called dropdown.php and when you select a value from the dropdown box it jumps to dropdown.php?id='whatever value the user selected' this is so that the user doesn't have to press on submit each time he changes a value

Here is the javascript for that select box:

Code: Select all

<script language="Javascript">
function doSel(obj)
&#123;
  for (i = 0; i < obj.length; i++)
     if (obj&#1111;i].selected == true)
       eval(obj&#1111;i].value);
&#125;
</script>

Posted: Thu Mar 27, 2003 7:43 am
by volka
then change
<option value='
to

Code: Select all

<option value="
otherwise the property is always parsed as value='location.href='

btw:

Code: Select all

function doSel(obj)
&#123;
    eval(obj.value);
&#125;
will do the same

thanks so much

Posted: Thu Mar 27, 2003 8:52 am
by ridwan
tahnks for all your help it works gr8 now :wink: