Page 1 of 1

php forms

Posted: Tue Jul 10, 2007 2:46 pm
by Nikos
i have 2 forms that communicate each other. the 1st has a drop down list with the categories and the 2nd form has another drop down list that i want to take values depends on the selection at the first drop down list. i have created this with get method. the data that the drop down lists have are in 2 different tables in mysql server. i want retrieve data and update them with new but when i change the selection at the first drop down list all data at the second form are gone. what i can do to keep data in position? i attach you the code if you dont understand somethng.

ps: please help me [s]cuz[/s] because i must give this site for my lab exams at uni

Code: Select all

<?	require ("scripts/admin_auth.php"); 
	require ("scripts/db_connection.php");
	   $query_bathmida='SELECT * FROM 32495_bathmida ORDER BY bathmida ASC';
	   $query_sxoli='SELECT * FROM 32495_sxoli';
	  
	  if(isset($_GET['sxoli'])){
	   $query_tmima='SELECT * FROM 32495_tmima where sid="'.$_GET['sxoli'].'" ORDER BY tmima ASC';
//	  	$query='SELECT * FROM 32495_contacts WHERE cid="'.$_GET["cid"].'"';
	   }else{
	    $query_tmima='SELECT * FROM 32495_tmima ORDER BY tmima ASC';
//		$query='SELECT * FROM 32495_contacts WHERE cid="'.$_GET["cid"].'"';
	   }
	   
	   $result_bathmida=mysql_query($query_bathmida);
	   $result_sxoli=mysql_query($query_sxoli);
	   $result_tmima=mysql_query($query_tmima);

	$query='SELECT * FROM 32495_contacts WHERE cid="'.$_GET["cid"].'"';
	   $result=mysql_query($query);
	   $row=mysql_fetch_assoc($result);
	   
/*	   $query_bathmida='SELECT * FROM 32495_bathmida';
	   $query_sxoli='SELECT * FROM 32495_sxoli';
	   $query_tmima='SELECT * FROM 32495_tmima'; 
	   $result_bathmida=mysql_query($query_bathmida);
	   $result_sxoli=mysql_query($query_sxoli);
	   $result_tmima=mysql_query($query_tmima);*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1253" />
<title>&#933;&#928;&#919;&#929;&#917;&#931;&#921;&#913; &#922;&#913;&#932;&#913;&#923;&#927;&#915;&#927;&#933;</title>
<link rel="stylesheet" type="text/css" href="css/base.css" />
<link rel="stylesheet" type="text/css" href="css/menu.css" />
<link rel="stylesheet" type="text/css" href="css/forms.css" />
<script type="text/javascript">

	function ValidateForm()
	{
		if ((document.addcontact.onoma.value.length < 1)||
			(document.addcontact.eponumo.value.length < 1)||
			(document.addcontact.bathmida.value.length < 1)||
			(document.addcontact.sxoli.value.length < 1)||
			(document.addcontact.tmima.value.length < 1)||
			(document.addcontact.tilefono.value.length < 1))
		{
			alert("&#928;&#929;&#927;&#931;&#927;&#935;&#919;!\n\n&#931;&#965;&#956;&#960;&#955;&#951;&#961;&#974;&#963;&#964;&#949; &#972;&#955;&#945; &#964;&#945; &#945;&#960;&#945;&#961;&#945;&#943;&#964;&#951;&#964;&#945; &#960;&#949;&#948;&#943;&#945;.");
			return false;
		}
		return true;	
	}
	
		
	function TilCk()
	{
		if (document.addcontact.tilefono.value.length <10)
		{
			alert("&#916;&#974;&#963;&#964;&#949; &#963;&#969;&#963;&#964;&#972; &#964;&#951;&#955;&#941;&#966;&#969;&#957;&#959;");
			return false;
		}
		return true;
	}
</script>


</head>

<body>

<table class="tb" cellspacing="0">
  <tr>
    <td colspan="2"><? include "header.php" ?></td>
  </tr>
  <tr>
    <td width="30%"><table cellspacing="0" class="menu">
  <tr>
    <td class="td"><img src="photos/til.JPG" class="img" /></td>
  </tr>
  <tr>
    <td class="td"><a href="scripts/test.php"/>&#913;&#961;&#967;&#953;&#954;&#942; &#931;&#949;&#955;&#943;&#948;&#945;</td>
  </tr>
  <tr>
    <td class="td"><a href=""/>&#913;&#957;&#945;&#950;&#942;&#964;&#951;&#963;&#951;</td>
  </tr>
  <tr>
    <td class="td"><a href="addmenu.php"/>&#928;&#961;&#959;&#963;&#952;&#942;&#954;&#951;</td>
  </tr>
  <tr>
    <td class="sub_menu">&#917;&#960;&#949;&#958;&#949;&#961;&#947;&#945;&#963;&#943;&#945;</td>
  </tr>
  <tr>
    <td class="sub_menu1"><a href="editcontact.php"/><span class="style3"/>&#917;&#960;&#949;&#958;&#949;&#961;&#947;&#945;&#963;&#943;&#945; &#949;&#960;&#945;&#966;&#942;&#962;</td>
  </tr>
  <tr>
    <td class="sub_menu1"><a href="editadmin.php"/><span class="style3"/>&#917;&#960;&#949;&#958;&#949;&#961;&#947;&#945;&#963;&#943;&#945; &#948;&#953;&#945;&#967;&#949;&#953;&#961;&#953;&#963;&#964;&#942;</td>
  </tr>
  <tr>
    <td class="td"><a href="scripts/logout.php"/>&#913;&#960;&#959;&#963;&#973;&#957;&#948;&#949;&#963;&#951;</td>
  </tr>
	</table></td>
    <td width="68%">
	<form name="sxoli" method="get" action="<?=$_SERVER['PHP_SELF']?>">
<table  border="0" cellpadding="1" cellspacing="1" class="center_tb1">
<tr>
    <td width="107">&#931;&#967;&#959;&#955;&#942;</td>
    <td width="192">
		<select name="sxoli" onchange="document.sxoli.submit();">
<!--			  <option value="">&#917;&#960;&#953;&#955;&#941;&#958;&#964;&#949; &#941;&#957;&#945;</option>-->
			  <? while($row_sxoli=mysql_fetch_assoc($result_sxoli)) {?>
			  <option <? if ($row_sxoli['sid']==$_GET['sxoli']) echo 'selected="selected"';?> value="<?=$row_sxoli['sid']?>"><?=$row_sxoli["sxoli"]?></option>
			  <? } ?>
		</select>
		</td>
    <td width="16"><img src="photos/required_field.gif" width="8" height="9" /></td>
  </tr>
  </table>
</form>

	<form id="editcontact" name="editcontact" method="post" action="upcon.php" onsubmit="return(ValidateForm() && TilCk());">
 <table border="0" cellpadding="1" cellspacing="1" class="center_tb1" >
  <tr>
    <td width="112">&#908;&#957;&#959;&#956;&#945;</td>
    <td width="192"><input type="text" name="onoma" value="<?=$row['onoma'] ?>" /></td>
    <td width="16"><img src="photos/required_field.gif" width="8" height="9" /></td>
  </tr>
  <tr>
    <td>&#917;&#960;&#974;&#957;&#965;&#956;&#959;</td>
    <td><input type="text" name="eponumo" value="<?=$row['eponumo'] ?>" /></td>
    <td><img src="photos/required_field.gif" width="8" height="9" /></td>
  </tr>
  <tr>
    <td>&#914;&#945;&#952;&#956;&#943;&#948;&#945;</td>
	<td><select name="bathmida">	   			  
			<? while($row_bathmida=mysql_fetch_assoc($result_bathmida)) {?>
			<option <? if ($row_bathmida['bid']==$row['bathmida']) echo 'selected="selected"';?> value="<?=$row_bathmida['bid']?>"><?=$row_bathmida["bathmida"]?></option>
			<? } ?>
		</select></td>
    <td><img src="photos/required_field.gif" width="8" height="9" /></td>
  </tr>
  <tr>
    <td>&#932;&#956;&#942;&#956;&#945;</td>
    <td><select name="tmima">
		  <? while($row_tmima=mysql_fetch_assoc($result_tmima)) {?>
		  <option <? if ($row_tmima['tid']==$row['tmima']) echo 'selected="selected"';?> value="<?=$row_tmima['tid']?>"><?=$row_tmima["tmima"]?></option>
		  <? } ?>
		</select></td>
    <td><img src="photos/required_field.gif" width="8" height="9" /></td>
  </tr>
  <tr>
    <td>e-mail</td>
    <td><input type="text" name="email" value="<?=$row['email'] ?>"  /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&#932;&#951;&#955;&#941;&#966;&#969;&#957;&#959;</td>
    <td><input type="text" name="tilefono" value="<?=$row['tilefono'] ?>"/></td>
    <td><img src="photos/required_field.gif" width="8" height="9" /></td>
  </tr>
  <tr>
    <td>Fax</td>
    <td><input type="text" name="fax" value="<?=$row['fax'] ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&#913;&#943;&#952;&#959;&#965;&#963;&#945;</td>
    <td><input type="text" name="aithousa" value="<?=$row['aithousa'] ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&#921;&#963;&#964;&#959;&#963;&#949;&#955;&#943;&#948;&#945;</td>
    <td><input type="text" name="web" value="<?=$row['web'] ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
      <td colspan="2" align="center"><input type="submit" name="Submit" value="&#917;&#957;&#951;&#956;&#941;&#961;&#969;&#963;&#951;" /></td>
    </tr>
	<input type="hidden" name="cid" value="<?=$_GET['cid']?>" />
	<input type="hidden" name="sxoli" value="<?=$_GET['sxoli'] ?>" />

</table>
</form>
	
	</td>
  </tr>
  <tr>
    <td colspan="2"><? include "footer.php" ?></td>
  </tr>
</table>

</body>
</html>

Posted: Tue Jul 10, 2007 2:49 pm
by RobertGonzalez
What triggers the filling of the second drop down?

And when posting PHP code, please use the PHP button or the Syntax... selector.

Posted: Tue Jul 10, 2007 3:05 pm
by Nikos
the onchange event of the first drop down list

Posted: Tue Jul 10, 2007 3:13 pm
by RobertGonzalez
So basically when the form submits, the second drop down shows up empty?

Posted: Tue Jul 10, 2007 3:21 pm
by Nikos
no. the problem is when i retrieve data from the db. cause of the onchange="document.sxoli.submit();" on the first form the form reloads and all data are gone.

ps:when i use this form to insert a record to the db works good

Posted: Tue Jul 10, 2007 3:27 pm
by RobertGonzalez
I see. You need to capture all of the POST data and feed it back into the form to keep the state of the data.

Posted: Tue Jul 10, 2007 3:35 pm
by Nikos
yes! i want data to stay at their position and only change the data at the first form and data in the second only at the drop down list named tmima

Posted: Tue Jul 10, 2007 6:14 pm
by mabufo

Posted: Wed Jul 11, 2007 3:22 am
by Nikos
mabufo wrote:Maybe this could help: http://www.hosting.vt.edu/tutorials/php ... validation
no this isn't what i am doing

i solved it with a hidden field at the first form

Code: Select all

<input type="hidden" name="cid" value="<?=$_GET['cid']?>" />