|
Posted by David Haynes on 07/14/06 01:21
IchBin wrote:
> I am following this thread because I am trying to do something simular.
> That is pass value back to php on a select ONCHANGE=. Problem is I do
> not know php\javascript all that well. I had posted my code to this
> newsgroup but received no answer. So I do not know if I can do what I am
> trying to do or I am just so far off that it does not warent an answer.
> This is what I have so far. I can not get to work and have comment out
> what I am trying:
>
> //
> // Handle the data coming back
> echo '<FORM NAME=author method=post action='.$_SERVER['PHP_SELF'].'>';
> //echo '<SELECT NAME=author_pk SIZE=20 COLS=20 ONCHANGE='.$_POST[id].'>';
> //echo '<SELECT NAME=author_pk SIZE=20 COLS=20 ONCHANGE=getPK()>';
> echo '<SELECT NAME=author_pk SIZE=20 COLS=20>';
>
> while ($rows = mysql_fetch_object($result)) {
> echo '<OPTION VALUE='.$rows->id.'>'.
> $rows->TITLE.' '.
> $rows->lastname.', '.
> $rows->firstname.
> $rows->middlename.
> $rows->SUFFIX.
> '</OPTION>';
> }
> echo '</SELECT>';
> echo '</FORM>';
> //echo '<script language=Javascript>';
> //echo 'function getPK() {';
> //echo
> "document.author.author_pk.options[document.author.author_pk.selectedIndex].value
> ";
> //echo '}';
> //echo "</script>";
IchBin:
You don't need to use javascript at all to do what you want to do.
You can simply let PHP do all the work.
Take a look at this example based upon your posting:
<?php
// an example of a monolithic form processor in php
// connect to mysql database
// @note: add a lot more error checking.
mysql_connect($hostname, $username, $password);
mysql_select_db($database);
// This form uses the POST method, so process any post selections here
if( isset($_POST['author_pk']) ) {
// do whatever you need to do with the author_pk
$sql = "select * from book_detail where author_pk = {$_POST['author_pk']}";
$detail = mysql_query($sql);
}
// main form query
$sql = "SELECT * from users order by lastname, firstname, middlename ";
$result = mysql_query($sql);
mysql_close();
?>
<FORM NAME="author" method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>"
<SELECT NAME="author_pk" SIZE="20" COLS="20">
<?php
// a loop to populate the select options
while( $row = mysql_fetch_object($result) ) {
printf("<option value=\"%d\">%s, %s, %s, %s, %s</option>\n",
$row->id, $row->TITLE, $row->lastname,
$row->firstname, $row->middlename, $row->SUFFIX);
}
mysql_free_result($result);
// display any book details based on the user's selection
if( isset($detail) ) {
while( $row = mysql_fetch_assoc($detail) ) {
printf("Title: %s<br>\n", $row['title']);
}
}
?>
</SELECT>
</FORM>
Navigation:
[Reply to this message]
|