Jump to content

Need Help In Php


Recommended Posts

Hi,

I need some help. i have a page with a list box containing id. when i choose a id i have to show the info belongs to the id in the same page. the info are in the database with the id. I m using php and javascript

I think u understand the prob, if not, inform me.

Thanks

Link to comment
Share on other sites


Hi

You can try something like this. Now remember this is only a quick sketch to get you on the right track.

<html-code-design>

<?
// Connect to the database
mysql_connect('server', 'username', 'password');
mysql_select_db('database');

// Only when the form is submitted and id is number only
if ($_SERVER['REQUEST_METHOD'] == 'POST' && preg_match('^[0-9]+$', $_POST['infoID'])
{   $result = mysql_query('SELECT infoTitle, infoText FROM infos WHERE infoID=$_POST['infoID'] LIMIT 1');
    if (mysql_num_rows($result) > 0)
    {   $row = mysql_fetch_assoc($result);
         echo '<b>'.$row['infoTitle'].'</b><br />';
         echo $row['infoText'];
     }
     else
     {    echo 'Unknown ID';
     }
}
?>
<form name="infoForm" action="" method="post">
<select name="infoID" size="1">
  <?
  $result = mysql_query('SELECT infoID, infoTitle FROM infos ORDER BY infoTitle');
  while ($row = mysql_fetch_assoc($result))
  {   echo '<option value="'.$row['infoID'].'">'.$row['infoTitle'].'</option>';
  }
  ?>
</select>
</form>

<?
// Closes the database
mysql_close();
?>

I hope i've understood it correctly

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...