Jump to content
Killersites Forums

RETRIVE DROPDOWN RESULT WHILE EDITING IN PHP


Recommended Posts

hey awsm work....it really helped me a lot...but in this i m facing one problem in edit.php....In my form i have used drop down list. I am not able to retrieve the result of that drop down list while editing the form. When i redirected to edit.php the fields having drop down list show first option in drop down list instead of showing selecting option while adding data...plsss help me in this....should i post my code?

 

 

i have attached my file where i want to insert this changes...if u r finding this code quite complicated then just help me with simple example

 

suppose i make a drop down menu having value 1, 2, 3, 4, 5, one option can be selected, and selected option is stored in database.

now i create an edit page, how do i display the selected value in the menu and other values in drop down,

for example,

Menu is like

Select One

1

2

3

4

5

 

and i select 3, this 3 is stored in database, now on the edit page i want to show 3 as already selected,

something like that..

 

 

<select name="select_thing">

<option value="1">1</option>

<option value="2">2</option>

<option value="3" selected>3</option>

<option value="4">4</option>

<option value="5">5</option>

</select>

 

I will be very thankful, if someone could help by explaining how do i make option 3 as selected.. eg. how i know option 3 is selected and stored in db.

Link to post
Share on other sites

if you can post your code for this I'll take a look at it.

<?php include "../includes/drop.php";

include_once "common.php";

_checkSession();

 

 

//include "includes/function.php";

 

function renderForm($srno,$mou,$name,$day,$date,$coord,$client,$tel,$mob,$fb,$sex,$sell,$firstadvance,$balreceived,$shootreceived,$rolls,$cheque,$shootdate,$postshoot,$error)

{

$result=mysql_query("SELECT day,date,sex,rolls,shootdate,postshoot FROM shoot_details WHERE Sr_no=$srno");

?>

 

<html>

<head>

<meta http-equiv="Content-Language" content="en-us">

<meta name="ProgId" content="FrontPage.Editor.Document">

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<link href="emp.css" rel="stylesheet" type="text/css">

<title>MOU Entry</title>

</head>

 

<body>

<br>

<table width="778" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td></td>

</tr>

 

<tr>

<td>

 

<form method=post action="" >

<input type="hidden" name="Sr_no" value="<?php echo $srno?>">

 

<table width="100%" border="0" cellpadding="1" cellspacing="0" bgcolor="#CCCCCC">

<tr>

<td><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">

<tr bgcolor="#F2FECD">

<td height="20" colspan="2" class="blue_links1">  Memorandum Of Understanding(MOU) </td>

</tr>

<tr>

<td colspan="2" class="form_text"><img src="images/spacer_gray11.jpg" width="100%" height="1"></td>

</tr>

<tr>

<td class="form_text"> </td>

<td> </td>

</tr>

 

 

 

 

 

<tr>

<td height="30" class="txt1">  MOU no</td>

<td height="30" class="txt1">

<input name="mou" type="text" class="form1" size="30" value="<?php echo $mou?>">

</td>

</tr>

 

 

<tr>

<td height="30" class="txt1">  Name</td>

<td height="30" class="txt1">

<input name="name" type="text" class="form1" size="30" value="<?php echo $name?>">

</td>

</tr>

 

<tr>

<td height="30" class="txt1">  Day</td

<td height="30" class="txt1">

 

 

 

</select><?php drop("arr_day","$day");?>

</td>

</tr>

 

<tr>

<td height="30" class="txt1">  Date </td>

<td height="30" class="txt1"><?php drop("arr_date",$datearr[2],"sign_date" );?><select name="sign_month" class="form1"><option value="">-Select-</option>

 

<option value="01"<?php if ($datearr[1]=="01") echo "selected";?>>January</option>

<option value="02"<?php if ($datearr[1]=="02") echo "selected";?>>February</option>

<option value="03"<?php if ($datearr[1]=="03") echo "selected";?>>March</option>

<option value="04"<?php if ($datearr[1]=="04") echo "selected";?>>April</option>

<option value="05"<?php if ($datearr[1]=="05") echo "selected";?>>May</option>

<option value="06"<?php if ($datearr[1]=="06") echo "selected";?>>June</option>

<option value="07"<?php if ($datearr[1]=="07") echo "selected";?>>July</option>

<option value="08"<?php if ($datearr[1]=="08") echo "selected";?>>August</option>

<option value="09"<?php if ($datearr[1]=="09") echo "selected";?>>September</option>

<option value="10"<?php if ($datearr[1]=="10") echo "selected";?>>October</option>

<option value="11"<?php if ($datearr[1]=="11") echo "selected";?>>November</option>

<option value="12"<?php if ($datearr[1]=="12") echo "selected";?>>December</option>

 

</select>

<?php drop("arr_year",$datearr[0],"sign_year");?>

<font face="Verdana, Arial, Helvetica, sans-serif" size="-2">(DD/MM/YYYY)</font>

</tr>

<tr>

<td height="30" class="txt1">  Co-Ordinator Name</td>

<td height="30" class="txt1">

<input name="coord" type="text" class="form1" size="30" value="<?php echo $coord?>">

</td>

</tr>

 

<tr>

<td height="30" class="txt1">  Client's Name</td>

<td height="30" class="txt1">

<input name="client" type="text" class="form1" size="30" value="<?php echo $client?>">

</td>

</tr>

 

 

 

 

<tr>

<td height="30" class="txt1">   Telephone No.</td>

<td height="30" class="txt1">

<input name="tel" type="text" class="form1" size="30" value="<?php echo $tel?>">

</td>

</tr>

<tr>

<td height="30" class="txt1">  Mobile No.</td>

<td height="30" class="txt1">

<input name="mobile" type="text" class="form1" size="30" value="<?php echo $mob?>">

</td>

</tr>

 

<tr>

<td height="30" class="txt1">  Feedback</td>

<td height="30" class="txt1"><textarea name="fb" cols=23 rows=5 class="form1"><?php echo $fb?></textarea></td>

</tr>

<tr>

<td height="30" valign="top" class="txt1">  Sex</td>

<td height="30" class="txt1">

<?php drop("arr_sex","$sex");?>

</td>

</tr>

 

<tr>

<td height="30" class="txt1">  Number Of Rolls</td>

<td height="30" class="txt1">

<?php drop("arr_rolls","$rolls");?>

</td>

</tr>

 

 

 

 

 

<tr>

<td height="30" class="txt1">  Selling Price</td>

<td height="30" class="txt1">

<input name="sell" type="text" class="form1" size="30" value="<?php echo $sell?>">

</td>

</tr>

 

 

 

 

 

<tr>

<td height="30" class="txt1">  First Advance Received</td>

<td height="30" class="txt1">

<input name="firstadvance" type="text" class="form1" size="30" value="<?php echo $firstadvance?>">

</td>

</tr>

<tr>

<td height="30" class="txt1">  On Shoot Received</td>

<td height="30" class="txt1">

<input name="shootreceived" type="text" class="form1" size="30" value="<?php echo $shootreceived?>">

</td>

</tr>

 

 

 

 

 

 

 

 

 

 

 

<tr>

<td height="30" class="txt1">  Cheque Details</td>

<td height="30" class="txt1"><textarea name="cheque" cols=23 rows=5 class="form1"><?php echo $cheque?></textarea></td>

</tr>

 

 

 

 

<tr>

<td height="30" class="txt1">  Shoot Date</td>

<td height="30" class="txt1">

<?php drop("arr_date",$shootarr[2],"shoot_date");?><select name="shoot_month" class="form1"><option value="00">-Select-</option>

 

<option value="01"<?php if ($shootarr[1]=="01") echo "selected";?>>January</option>

<option value="02"<?php if ($shootarr[1]=="02") echo "selected";?>>February</option>

<option value="03"<?php if ($shootarr[1]=="03") echo "selected";?>>March</option>

<option value="04"<?php if ($shootarr[1]=="04") echo "selected";?>>April</option>

<option value="05"<?php if ($shootarr[1]=="05") echo "selected";?>>May</option>

<option value="06"<?php if ($shootarr[1]=="06") echo "selected";?>>June</option>

<option value="07"<?php if ($shootarr[1]=="07") echo "selected";?>>July</option>

<option value="08"<?php if ($shootarr[1]=="08") echo "selected";?>>August</option>

<option value="09"<?php if ($shootarr[1]=="09") echo "selected";?>>September</option>

<option value="10"<?php if ($shootarr[1]=="10") echo "selected";?>>October</option>

<option value="11"<?php if ($shootarr[1]=="11") echo "selected";?>>November</option>

<option value="12"<?php if ($shootarr[1]=="12") echo "selected";?>>December</option>

</select>

<?php drop("arr_year",$shootarr[0],"shoot_year");?>

<font face="Verdana, Arial, Helvetica, sans-serif" size="-2">(DD/MM/YYYY)</font></td>

</tr>

<tr>

<td height="30" class="txt1">  Postpond Shoot</td>

<td height="30" class="txt1"><?php drop("arr_date",$postarr[2],"post_date" );?><select name="post_month" class="form1"><option value="">-Select-</option>

 

<option value="01"<?php if ($postarr[1]=="01") echo "selected";?>>January</option>

<option value="02"<?php if ($postarr[1]=="02") echo "selected";?>>February</option>

<option value="03"<?php if ($postarr[1]=="03") echo "selected";?>>March</option>

<option value="04"<?php if ($postarr[1]=="04") echo "selected";?>>April</option>

<option value="05"<?php if ($postarr[1]=="05") echo "selected";?>>May</option>

<option value="06"<?php if ($postarr[1]=="06") echo "selected";?>>June</option>

<option value="07"<?php if ($postarr[1]=="07") echo "selected";?>>July</option>

<option value="08"<?php if ($postarr[1]=="08") echo "selected";?>>August</option>

<option value="09"<?php if ($postarr[1]=="09") echo "selected";?>>September</option>

<option value="10"<?php if ($postarr[1]=="10") echo "selected";?>>October</option>

<option value="11"<?php if ($postarr[1]=="11") echo "selected";?>>November</option>

<option value="12"<?php if ($postarr[1]=="12") echo "selected";?>>December</option>

 

</select>

<?php drop("arr_year",$postarr[0],"post_year");?>

<font face="Verdana, Arial, Helvetica, sans-serif" size="-2">(DD-MM-YYYY)</font> </td>

</tr>

 

<tr>

<td colspan="2" valign="top" class="form_text" align="center"><br>

<input name="submit" type="submit" class="form1_bu" value="Register Me" ID="Submit1" onClick="return validate();">

<input name="back" type="button" class="form1_bu" onclick="history.back();" value="Back">

</td>

</tr>

<tr>

<td colspan="2" valign="top" class="form_text" align="center"> </td>

</tr>

</table></td>

</tr>

</table>

<input type="hidden" name="action" value="<?php echo $action;?>">

<input type="hidden" name="Sr_no" value="<?php echo $srno;?>">

 

</form>

</td>

</tr>

 

</table>

 

</body>

</html>

<?php

}

// check if the form has been submitted. If it has, process the form and save it to the database

if (isset($_POST['submit']))

{

// confirm that the 'id' value is a valid integer before getting the form data

if (is_numeric($_POST['Sr_no']))

{

// get form data, making sure it is valid

$srno = $_POST['Sr_no'];

$mou = mysql_real_escape_string(htmlspecialchars($_POST['mou']));

$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));

$day = mysql_real_escape_string(htmlspecialchars($_POST['day']));

$date=mysql_real_escape_string(htmlspecialchars($_POST['sign_year']))."-".mysql_real_escape_string(htmlspecialchars($_POST['sign_month']))."-" .mysql_real_escape_string(htmlspecialchars($_POST['sign_date']));

$coord = mysql_real_escape_string(htmlspecialchars($_POST['coord']));

$client = mysql_real_escape_string(htmlspecialchars($_POST['client']));

$tel = mysql_real_escape_string(htmlspecialchars($_POST['tel']));

$mob = mysql_real_escape_string(htmlspecialchars($_POST['mob']));

$fb = mysql_real_escape_string(htmlspecialchars($_POST['fb']));

$sex = mysql_real_escape_string(htmlspecialchars($_POST['sex']));

$rolls = mysql_real_escape_string(htmlspecialchars($_POST['rolls']));

$sell = mysql_real_escape_string(htmlspecialchars($_POST['sell']));

$firstadvance = mysql_real_escape_string(htmlspecialchars($_POST['firstadvance']));

$shootreceived = mysql_real_escape_string(htmlspecialchars($_POST['shootreceived']));

$balreceived = mysql_real_escape_string(htmlspecialchars($_POST['balreceived']));

$cheque = mysql_real_escape_string(htmlspecialchars($_POST['cheque']));

$shootdate = mysql_real_escape_string(htmlspecialchars($_POST['shootdate']));

$postshoot = mysql_real_escape_string(htmlspecialchars($_POST['postshoot']));

if($a=$_POST['firstadvance']||$a=$_POST['shootreceived'])

{$balreceived=$sell-$firstadvance-$shootreceived;

}

 

// check that firstname/lastname fields are both filled in

if ($mou == '')

{

// generate error message

$error = 'ERROR: Please fill in all required fields!';

 

//error, display form

renderForm($srno,$mou,$name,$day,$date,$coord,$client,$tel,$mob,$fb,$sex,$sell,$firstadvance,$balreceived,$shootreceived,$rolls,$cheque,$shootdate,$postshoot, $error);

}

else

{

// save the data to the database

mysql_query("UPDATE shoot_details SET mou='$mou', day=' $day ', date=' $date', coord=' $coord ', client=' $client ', tel=' $tel . ', mob=' $mob', fb=' . $fb ', sex=' $sex ', sell=' $sell ', firstadvance=' $firstadvance ', balreceived=' $balreceived ', shootreceived=' $shootreceived ',cheque='$cheque', rolls='$rolls', shootdate='$shootdate ', postshoot='$postshoot ' WHERE Sr_no='$srno'")

or die(mysql_error());

 

// once saved, redirect back to the view page

// header("Location: view.php");

}

}

else

{

// if the 'id' isn't valid, display an error

echo 'Error!';

}

}

else

// if the form hasn't been submitted, get the data from the db and display the form

{

 

// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)

if (isset($_GET['Sr_no']) && is_numeric($_GET['Sr_no']) && $_GET['Sr_no'] > 0)

{

// query db

$srno = $_GET['Sr_no'];

$result = mysql_query("SELECT * FROM shoot_details WHERE Sr_no=$srno")

or die(mysql_error());

$row = mysql_fetch_array($result);

 

// check that the 'id' matches up with a row in the databse

if($row)

{

 

// get data from db

$srno=$row['Sr_no'];

$mou=$row['mou'];

$name=$row['name'];

$day = $row['arr_day'];

$date=$row['date'];

$coord=$row['coord'];

$client=$row['client'];

$tel=$row['tel'];

$mob=$row['mobile'];

$fb=$row['fb'];

$sex=$row['arr_sex'];

$sell=$row['sell'];

$firstadvance=$row['firstadvance'];

$balreceived=$row['balreceived'];

$shootreceived=$row['shootreceived'];

$rolls=$row['arr_rolls'];

$cheque=$row['cheque'];

$shootdate=$row['shootdate'];

$postshoot=$row['postshoot'];

 

// show form

renderForm($srno,$mou,$name,$day,$date,$coord,$client,$tel,$mob,$fb,$sex,$sell,$firstadvance,$balreceived,$shootreceived,$rolls,$cheque,$shootdate,$postshoot,'');

}

else

// if no match, display result

{

echo "No results!";

}

}

else

// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error

{

echo 'Error!';

}

}

?>

Link to post
Share on other sites

It looks like you have the right general idea. You need to create a variable that will hold your dropdown's value, and then compare that value against the option's value to figure out which is correct.

 

<select name="shoot_month" class="form1"><option value="00">-Select-</option>

<option value="01"<?php if ($shootarr[1]=="01") echo "selected";?>>January</option>
<option value="02"<?php if ($shootarr[1]=="02") echo "selected";?>>February</option>
<option value="03"<?php if ($shootarr[1]=="03") echo "selected";?>>March</option>
<option value="04"<?php if ($shootarr[1]=="04") echo "selected";?>>April</option>
<option value="05"<?php if ($shootarr[1]=="05") echo "selected";?>>May</option>
<option value="06"<?php if ($shootarr[1]=="06") echo "selected";?>>June</option>
<option value="07"<?php if ($shootarr[1]=="07") echo "selected";?>>July</option>
<option value="08"<?php if ($shootarr[1]=="08") echo "selected";?>>August</option>
<option value="09"<?php if ($shootarr[1]=="09") echo "selected";?>>September</option>
<option value="10"<?php if ($shootarr[1]=="10") echo "selected";?>>October</option>
<option value="11"<?php if ($shootarr[1]=="11") echo "selected";?>>November</option>
<option value="12"<?php if ($shootarr[1]=="12") echo "selected";?>>December</option>
</select>

This code looks fine (I usually use "selected="selected"" and add a space between that and the value, but that's up to you), assuming your $shootarr array is set up properly. You might want to try displaying your array values such as $shootarr[1] on the page to make sure they are holding the values you expect.

 

After looking over your code again, where do you define the $postarr and $shootarr arrays? I see that you try to access them (for example, in your code above) but I don't see where you create those variables and set their values.

Link to post
Share on other sites

It looks like you have the right general idea. You need to create a variable that will hold your dropdown's value, and then compare that value against the option's value to figure out which is correct.

 

<select name="shoot_month" class="form1"><option value="00">-Select-</option>

<option value="01"<?php if ($shootarr[1]=="01") echo "selected";?>>January</option>
<option value="02"<?php if ($shootarr[1]=="02") echo "selected";?>>February</option>
<option value="03"<?php if ($shootarr[1]=="03") echo "selected";?>>March</option>
<option value="04"<?php if ($shootarr[1]=="04") echo "selected";?>>April</option>
<option value="05"<?php if ($shootarr[1]=="05") echo "selected";?>>May</option>
<option value="06"<?php if ($shootarr[1]=="06") echo "selected";?>>June</option>
<option value="07"<?php if ($shootarr[1]=="07") echo "selected";?>>July</option>
<option value="08"<?php if ($shootarr[1]=="08") echo "selected";?>>August</option>
<option value="09"<?php if ($shootarr[1]=="09") echo "selected";?>>September</option>
<option value="10"<?php if ($shootarr[1]=="10") echo "selected";?>>October</option>
<option value="11"<?php if ($shootarr[1]=="11") echo "selected";?>>November</option>
<option value="12"<?php if ($shootarr[1]=="12") echo "selected";?>>December</option>
</select>

This code looks fine (I usually use "selected="selected"" and add a space between that and the value, but that's up to you), assuming your $shootarr array is set up properly. You might want to try displaying your array values such as $shootarr[1] on the page to make sure they are holding the values you expect.

 

After looking over your code again, where do you define the $postarr and $shootarr arrays? I see that you try to access them (for example, in your code above) but I don't see where you create those variables and set their values.

i used yr suggested code.but it is displaying first value of the array. this is the portion where i defined postarr and shootarr in mou.php. this file adds new entry.

$srno=$_GET['Sr_no'];

$mou="";

$name="";

$day = "";

$date="";

$coord="";

$client="";

$tel="";

$mob="";

$fb="";

$sex="";

$sell="";

$firstadvance="";

$balreceived="";

$shootreceived="";

$rolls="";

$cheque="";

$shootdate="";

$postshoot="";

 

if(!empty($srno) && isset($srno)){

$objectIndex = "SD";

$fields = _getFields("MF");

 

$whereclause = "WHERE Sr_no=$srno" ;

$otherclause = "";

$result_array = _getRows($objectIndex, $fields, $whereclause, $otherclause);

 

 

 

if(!empty($result_array)){

for($i=0;$i<count($result_array);$i++)

{

 

$srno=$result_array[$i][0];

$mou=$result_array[$i][1];

$name=$result_array[$i][2];

$day =$result_array[$i][3];

$date=$result_array[$i][4];

$datearr=split('[-/]',$date);

$coord=$result_array[$i][5];

$client=$result_array[$i][6];

$tel=$result_array[$i][7];

$mob=$result_array[$i][8];

$fb=$result_array[$i][9];

$sex=$result_array[$i][10];

$sell=$result_array[$i][11];

$firstadvance=$result_array[$i][12];

$balreceived=$result_array[$i][13];

$shootreceived=$result_array[$i][14];

$rolls=$result_array[$i][15];

$cheque=$result_array[$i][16];

$shootdate=$result_array[$i][17];

$shootarr=split('[-/]',$shootdate);

$postshoot=$result_array[$i][18];

$postarr=split('[-/]',$postshoot);

}

}

 

}

?>

 

 

all data is saved thru save_mou.php....in this

 

sign_date=$_POST['sign_date'];

$sign_year=$_POST['sign_year'];

$sign_month=$_POST['sign_month'];

$date=$sign_year."-".$sign_month."-".$sign_date;

 

$shoot_date=$_POST['shoot_date'];

$shoot_month=$_POST['shoot_month'];

$shoot_year=$_POST['shoot_year'];

$shootdate=$shoot_year."-".$shoot_month."-".$shoot_date;

 

$post_date=$_POST['post_date'];

$post_month=$_POST['post_month'];

$post_year=$_POST['post_year'];

$postshoot=$post_year."-".$post_month."-".$post_date;

 

$srno=$_POST["srno"];

$mou=$_POST["mou"];

$name=$_POST["name"];

$day = $_POST["day"];

$date=$_POST['sign_year']."-".$_POST['sign_month']."-" .$_POST['sign_date'];

$coord=$_POST["coord"];

$client=$_POST["client"];

$tel=$_POST["tel"];

$mob=$_POST["mob"];

$fb=$_POST["fb"];

$sex=$_POST["arr_sex"];

$sell=$_POST["sell"];

$firstadvance=$_POST["firstadvance"];

$balreceived=$_POST["balreceived"];

$shootreceived=$_POST["shootreceived"];

$rolls=$_POST["arr_roll"];

$cheque=$_POST["cheque"];

$shootdate=$_POST['shoot_year']."-".$_POST['shoot_month']."-" .$_POST['shoot_date'];

$postshoot=$_POST['post_year']."-".$_POST['post_month']."-" .$_POST['post_date'];

 

 

now suggest me something..if u r nt getting the code..plss tell me yr ideas...i'l try to implement it

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...