Jump to content

change dropdown value


imran

Recommended Posts

hello,

 

i'm new with php and need your help.

 

i've two dropdown fields

1) products - (mobile phone, camera, webcam, tv, dvd player and etc

2) accessories

 

how accessories field can be changed if visitor selects product.

 

for example: if visitor selects TV, accessories field should automatically changed with cable wire, electric board and etc (without reloading)

 

another example is www bestessays com/order.php

when you'll select "Thesis" from type of document, urgency and cost per page values will be changed.

 

as i told earlier that i'm new with php, may be i've not cleared my question for which pl forgive me.

 

guys, thanks in advance for your guidance.

 

Rgds/Imran

 

Link to comment
https://forums.phpfreaks.com/topic/44836-change-dropdown-value/
Share on other sites

I am doing the same thing, but only the first drop down is populated by the php array value (the year). The next fields are populated by the MySQL database.

 

<?php
import_request_variables("gpc","");

function show_year($year) {
$year_arr = array("","1994","1995","1996","1997","1998");
$arr_size = count($year_arr);


for ($i = 0; $i < $arr_size; $i++) {
    if ($year_arr[$i] == $year) 
       print "<option selected value=\"" .$year_arr[$i] ."\">".$year_arr[$i] ."</option>\n";
    else 
       print "<option value=\"" .$year_arr[$i] ."\">".$year_arr[$i] ."</option>\n";
  }
}


function show_manufactur($year,$man)
{

//Result SQL statement        
        $sql="SELECT * FROM " .$year. "data GROUP by MFR";

//Connect to server
You Should know how to do this!

//Count the number of results
        $result = mysql_query("SELECT * FROM " .$year. "data GROUP by MFR");
        
        //$rows = mysql_result($result, 0, 'total');
        $rows = mysql_num_rows($result);

//Populate the drop down list        
        $addblankspace = "";
        print "<option value=\"" .$addblankspace ."\">".$addblankspace ."</option>\n";
        for ($i = 0; $i < $rows; $i++) 
        {
        $tempmfr = mysql_result($sql_result,$i,'MFR');
        
        if ($tempmfr == $man) 
          print "<option selected value=\"" .$tempmfr."\">".$tempmfr."</option>\n";
        else 
          print "<option value=\"" .$tempmfr ."\">".$tempmfr ."</option>\n";

        }  
        $sql = "";
//Close the database connection
         mysql_close();
}


function show_model($year,$man)
{


//Result SQL statement        
        $sql="SELECT * FROM " .$year. "data WHERE MFR = '" .$man. "' GROUP by MODEL";

//Connect to server
You Should know how to do this!

//Count the number of results
        $result = mysql_query("SELECT * FROM " .$year. "data WHERE MFR = '" .$man. "' GROUP by MODEL");
        
        $rows = mysql_num_rows($result);

//Populate the drop down list        
        $results = array();
for ($i = 0; $i < $rows; $i++) 
{
$results[] = mysql_result($sql_result, $i, 'MODEL');
}

natsort($results);

foreach($results as $result)
{
print "<option value=\"" . $result . "\">" . $result . "</option>\n";
}
//Close the database connection
         mysql_close();
}

?>


<script language=javascript>
function showyear(year) 
{
  refresh = "./verdevdatabase.php?year=" + year; 
  location.href=refresh;
}

function showmodel(man,year) 
{
  refresh = "./verdevdatabase.php?man=" + man + "&year=" + year ; 
  location.href=refresh;
}
</script>


<form action="verdevquery.php" method="POST" enctype="application/x-www-form-urlencoded">
  <center>
  <table width="375" border="0" align="left" cellpadding="0" cellspacing="0">
<TR>
  <TD><div align="left"><strong>Search by Engine Family Name:</strong></div></TD>
  </TR>
<TR>
  <TD><div align="center">
        <p align="left">
          <input name="efn" type="text" id="efn" size="25">
        </p>
    <p align="left">
      <input name="searchtype" type="submit" id="searchtype" value="Search by EFN" />
        </p>
    </div>
    <div align="left"></div></TD>
  </TR>
<TR>
  <TD> </TD>
  </TR>
<TR>
  <TD> </TD>
  </TR>
<TR>
  <TD> </TD>
  </TR>
<TR>
  <TD><div align="left"><strong>Browse by Engine Year, Manufacture and Model:</strong></div></TD>
  </TR>
<TR>
	<TD WIDTH="375"><CENTER>
	  

		<TABLE WIDTH="362" HEIGHT="139" BORDER="0" align="left" CELLPADDING="0" CELLSPACING="0">
			<TR>
				<TD WIDTH="182" bgcolor="#FFFFFF" class="style1"><div align="left"><FONT FACE="Arial, Helvetica">Year:</FONT></div></TD>
				<TD WIDTH="180" bgcolor="#FFFFFF">					  <P align="left">
					  
				  <SELECT NAME=year onchange=showyear(this.options[this.selectedIndex].value)>
				    <?php 
               show_year($year);
            ?>
			      </SELECT>
					  
                  </TD>
			</TR>
			<TR>
			  <TD bgcolor="#ECECE7" class="style1"> </TD>
			  <TD bgcolor="#ECECE7"> </TD>
			  </TR>
			<TR>
				<TD WIDTH="182" bgcolor="#FFFFFF" class="style1"><div align="left"><FONT FACE="Arial, Helvetica">Manufacturer:</FONT></div></TD>
				<TD WIDTH="180" bgcolor="#FFFFFF">					  <P align="left">
				  <SELECT NAME=manufacture onchange=showmodel(this.options[this.selectedIndex].value,year.value)>
          
            <?php 
                show_manufactur($year,$man);
            ?>
				  </SELECT>

                  </TD>
			</TR>
			<TR>
			  <TD bgcolor="#ECECE7" class="style1"> </TD>
			  <TD bgcolor="#ECECE7"> </TD>
			  </TR>
			<TR>
				<TD WIDTH="182" bgcolor="#FFFFFF" class="style1"><div align="left"><FONT FACE="Arial, Helvetica">Model:</FONT></div></TD>
				<TD WIDTH="180" bgcolor="#FFFFFF">					  <P align="left">
				  <SELECT NAME=model >
					<?php 
               show_model($year,$man);
            ?>
				  </SELECT>

                  </TD>
			</TR>
			<TR>
			  <td colspan="2" bgcolor="#ECECE7"> </td>
			  </tr>
			<TR>
				<td colspan="2">
					<center>
					<p align="left"><input type="SUBMIT" name="searchtype" value="Search by YMM">
</center>					</td>
			</tr>
		</table>
</center>		</td>
  </tr>
</table>

</center>
<p>
</form>
 </p>

Link to comment
https://forums.phpfreaks.com/topic/44836-change-dropdown-value/#findComment-217752
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.