Jump to content

trouble with dropdown menu


nec9716

Recommended Posts

here my script:

<html>
<head>
<title>Manage data</title>
</head>
<body>
<?php
/*************************************************************************
control code for application
*************************************************************************/
//submit button was pressed so call the process form function
if (isset($_POST['submit']))
{
process_form();
die();
}//end if

//call the get_data function
if (isset($_GET['id']))
{
get_data();
}//endif

if ((empty($_POST))&&(empty($_GET)))
{
list_users();
die();
}//end if

//request to add a new contact so call the show_form function
if ((isset($_GET['action']))&&($_GET['action']=='add'))
{
show_form();
}//endif
/*************************************************************************
get the data for an individual contact
*************************************************************************/
function get_data()
{
//validate the id has been passed at that it is a number
if ((empty($_GET['id']))||(is_nan($_GET['id'])))
{
//there was a problem so list the users again
list_users();
//kill the script
die();
}else{
//all is ok and assign the data to a local variable
$id = $_GET['id'];
}//end if

$sql = "select * from pick where id = $id";

$result = conn($sql);
if (mysql_num_rows($result)==1){
//call the form and pass it the handle to the resultset
show_form($result);
}else{
$msg = "No data found for selected contact";
confirm($msg);
//call the list users function
list_users();
}//end if
}//end function
/*************************************************************************
show the input / edit form
*************************************************************************/
function show_form($handle='',$data='')
{
//$handle is the link to the resultset, the ='' means that the handle can be empty / null so if nothing is picked it won't blow up
//set default values
$tpname = '';
$id = '';
$value = 'Add'; //submit button value
$action = 'add'; //default form action is to add a new kid to db

//set the action based on what the user wants to do
if ($handle)
{
//set form values for button and action
$action = "edit";
$value = "Update";

//get the values from the db resultset
$row = mysql_fetch_array($handle);

//EDIT
$tpname = $row['pname'];
$id = $row['id'];
}//end if

//error handling from the processing form function
if($data != '')
{
$elements = explode("|",$data);
$tpname = $elements[0];
$id = $elements[1];
}
?>
<body>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?action=<?php echo $action?>">
<table width='75%' align='center' cellpadding='0' cellspacing='0'>
<tr>
<td colspan="2" align="center" style="font-size:18px; font-weight:bold;">Manage USER PICK </td></tr>
<input type="hidden" value="<?php echo $id?>" name="id">
<tr>
<td align="center">****** be sure to have already insert your USER,DRIVERS,CHASSIS and ENGINE ****** </td><BR><BR>
</tr>
</body>
<?php
require_once 'connect.php';
// echo "<input type="hidden" value="' .$row['id'] .'" name="id">";
if(isset($_POST['pname']))
$tpname = $_POST['pname'];
else
$tpname = 1;

//Generate and run the query to populate the pilote listbox. (DropDown1)
$query6 = "SELECT userid, uname FROM user";
$result6 = mysql_query($query6);

//If there are records in the table, create the listbox.
if(mysql_num_rows($result6) > 0) {

echo "<table width='100%' align='center' cellpadding='0' cellspacing='0'>

<tr><BR>
<td width=10%>User Name:</td>
</tr>";
//--Create 6listbox.

echo "<tr>";
echo "<td width=10%>";
echo'<select name="pname" size="1" onchange="setItemListBox()">';
while($row = mysql_fetch_array($result6, MYSQL_NUM))
{
if($row[0] == $tpname)
echo '<option value="' . $row[1] . '" selected>' . $row[1] . '</option>';
else
echo '<option value="' . $row[1] . '">' . $row[1] . '</option>';
}
echo '</select>';
echo "</td >";
//--End 6 listbox

echo "</table>";
}

?>
<body>
<BR><BR>
<tr>
<td colspan="2" align="center"><input name="submit" type="submit" value="<?php echo $value?>"> <input name="reset" type="reset" value="Clear Form"></td>
</tr>
</table>
</form>
</body>
<?
}//end function

/*************************************************************************
list all the contacts in the db
*************************************************************************/
function list_users()
{
$y = 0; //counter
echo "<table width='80%' align='center' cellpadding='0' cellspacing='0'>
<tr><td colspan='2' align='center' style='font-size:28px; font-weight:bold;'>Manage USER PICK Data</td></tr>
<td colspan='2' align='center' ><a href='".$_SERVER['PHP_SELF']."?action=add'>Add A New User Pick</a></td></tr>
<tr><td colspan='2' align='center;'>Click A User name To Edit DATA</td></tr>
<tr><td colspan='2'> </td></tr>";
$sql = "SELECT * FROM pick ORDER BY pick.pname ASC"; //sort by name when loaded first time
$result = conn($sql);

$y = 0; //counter
if (mysql_num_rows($result) != 0) {
while($rows = mysql_fetch_array($result)){

//change row background color
(($y % 2) == 0) ? $bgcolor = "#99ff66" : $bgcolor=" #33ff00";

$qpname = $rows['pname'];
$id = $rows['id'];

//echo out the row
echo "<tr style='background-color:$bgcolor;'>
<td width=13%><a href='".$_SERVER['PHP_SELF']."?id=$id'>$qpname</a></td>
</tr>";
$y++; //increment the counter
}//end while
echo "</table>";
} else {
//handle no results
echo "<tr><td colspan='2' align='center'>No data found.</td></tr>";
}//endif
}

/*************************************************************************
add / update the contact's data
*************************************************************************/
function process_form()
{
$fpname = '';
$id = '';
$action = '';

$fpname = @$_POST['pname'];
$id = @$_POST['id'];
$action = @$_GET['action'];

if (($fpname==''))
{
$msg = "Some data from the form was forgotten. Please fill in the entire form.";
confirm($msg);
$data = "$fpname|$id";
show_form('',$data);
die();
}//end if

//You could add some validation of the data

if ($action == "add")
{
$sql = "insert into pick (pname) values('$fpname')";
$msg = "Record successfully added";
}elseif($action=="edit"){
//EDIT
$sql = "update pick set pname = '$fpname' where id = $id";

$msg = "Record successfully updated";
}
$result = conn($sql);
if (mysql_errno()==0)
{
confirm($msg);
list_users();
}else{
$msg = "There was a problem adding the user to the database. Error is:".mysql_error();
confirm($mag);
}//end if
}

 

this script let me add new date or update

add new data work great

but update date is calling the form page but the date in the dropdown list is not what it should be ( look like the id is not attach with the dropdown menu

 

so I have try few thing but with no result

 

I think it's something in the show the input / edit form section ....but can"t find out exactly

 

 

Link to comment
https://forums.phpfreaks.com/topic/99271-trouble-with-dropdown-menu/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.