別のドロップダウンからオプションを選択して、ドロップダウンに入力しようとしているコードを作成しました。私はグーグルで調べて、これがjavascriptで可能であることを理解しました。onchange()
しかし、最初のコンボ ボックスのメソッドでPHP 配列を JavaScript に渡すときにエラーが発生します。配列は完全に問題ありません。目的の結果が得られますが、データを入力しようとするとエラーが発生します...初期化されていない変数エラーが表示されます...しかし、値を設定する方法が見つかりません。
<?php
session_start(); // NEVER forget this!
if(!isset($_SESSION['loggedin']))
{
die("To access this page, you need to <a href='main_login.php'>LOGIN</a>");
}
?>
<html>
<head>
<script type="text/javascript">
function validateForm()
{
var name=document.forms["form1"]["name"].value;
var passno=document.forms["form1"]["passno"].value;
var applicationno=document.forms["form1"]["applicationno"].value;
var price=document.forms["form1"]["price"].value;
var date=document.forms["form1"]["date"].value;
var reg = /(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d/;
if ((name=="" || name==null) || (passno=="" || passno==null) || (applicationno=="" || applicationno==null || isNaN(applicationno) ) || (price=="" || price==null || isNaN(price)) || (date=="" || date==null || date=="00-00-0000" || !date.match(reg)))
{
alert("Applicant Name, Passport Number, Application Number,Price And Date Must Be Filled Out.\n\n Valid Date Format Is : (dd-mm-yyyy) \n\n Only numbers are allowed for Application Number and Price.");
return false;
}
else {
return true;
}
}
function fillDate(array1,array2)
{
document.form1.subagentname.options.add( new Option(array1,array2) );
}
</script>
<style type="text/css">
input.uppercase {
text-transform: uppercase;
}
</style>
</head>
<body>
<h1 align="center">ADD VISA DETAILS</h1>
<form id="form1" name="form1" method="post" action="insertvisa.php" onSubmit="return validateForm();">
<p>
<label for="name">APPLICANT NAME : * </label>
<input type="text" name="name" id="name" style="text-transform:uppercase;" onKeyUp="this.value=this.value.toUpperCase()"/>
</p>
<p>
<label for="company">COMPANY : </label>
<select name="company" id="company">
<option value="TULIP TOURISM LLC">TULIP TOURISM LLC</option>
<option value="TULIP TRAVELS LLC">TULIP TRAVELS LLC</option>
<option value="EXCELLENT MARINA">EXCELLENT MARINA</option>
</select>
</p>
<p>
<label for="nationality">NATIONALITY : </label>
<input type="text" name="nationality" id="nationality" style="text-transform:uppercase;" onKeyUp="this.value=this.value.toUpperCase()"/>
</p>
<p>
<label for="passno">PASSPORT NUMBER : * </label>
<input type="text" name="passno" id="passno" style="text-transform:uppercase;" onKeyUp="this.value=this.value.toUpperCase()"/>
</p>
<p>
<label for="applicationno">APPLICATION NUMBER : * </label>
<input type="text" name="applicationno" id="applicationno" style="text-transform:uppercase;" onKeyUp="this.value=this.value.toUpperCase()"/>
</p>
<p>
<label for="proctype">PROCEDURE TYPE : </label>
<select name="proctype" id="proctype">
<option value="NORMAL">NORMAL</option>
<option value="URGENT">URGENT</option>
</select>
</p>
<p>
<label for="visatype">VISA TYPE : </label>
<select name="visatype" id="visatype">
<option value="TOURIST">TOURIST</option>
<option value="SERVICE">SERVICE</option>
<option value="TT">TT</option>
<option value="EXTENSION">EXTENSION</option>
<option value="CANCELLATION">CANCELLATION</option>
</select>
</p>
<p>
<label for="agent">AGENT : </label>
<?php
$server="localhost";
$username="root";
$password="hp";
$link=mysql_connect($server, $username, $password) or die ("Cannot connect to mysql server: ".mysql_error());
$dbname = 'apcom_visa_system';
mysql_select_db($dbname, $link) or die ("Cannot connect to database: ".mysql_error());
$query="SELECT agent_id,agent_name FROM agent";
$result = mysql_query ($query) or die ('error submitting');
echo "<select name='agentname' id='agentname' onChange='return fillData(htmlspecialchars(json_encode($subagentsID), ENT_NOQUOTES),htmlspecialchars(json_encode($subagentsName), ENT_NOQUOTES));'>";
while($drop=mysql_fetch_array($result)){
//data stored in $drop
echo "<option value='$drop[agent_id]'>$drop[agent_name]</option>";
$agentsID[]=$drop[agent_id];
}
echo "</select>";
foreach($agentsID as $values)
{
//echo "agents-->".$values;
$query1="SELECT subagent_id,subagent_name FROM subagent WHERE agent_id=$values";
$result1 = mysql_query ($query1) or die ('error submitting');
while($drop1=mysql_fetch_array($result1)){
$subagentsID[]=$drop1['subagent_id'];
$subagentsName[]=$drop1['subagent_name'];
//echo "subagents-->".$drop1['subagent_id'];
}
}
mysql_close($link);
?>
</p>
<p>
<label for="subagentname">SUBAGENT : </label>
<select name="subagentname" id="subagentname"></select>
</p>
<p>
<label for="cost">COST : </label>
<select name="cost" id="cost">
<option value="210">210</option>
<option value="310">310</option>
<option value="610">610</option>
<option value="90">90</option>
</select>
</p>
<p>
<label for="price">PRICE : * </label>
<input type="text" name="price" id="price" style="text-transform:uppercase;" onKeyUp="this.value=this.value.toUpperCase()"/>
<label for="currency"></label>
<select name="currency" id="currency">
<option value="AED">AED</option>
<option value="PKR">PKR</option>
</select>
</p>
<p>
<label for="status">STATUS : </label>
<select name="status" id="status">
<option value="APPLICATION NOT POSTED">APPLICATION NOT POSTED</option>
<option value="APPLICATION POSTED">APPLICATION POSTED</option>
<option value="APPLICATION INPROCESS">APPLICATION INPROCESS</option>
<option value="APPLICATION REJECTED">APPLICATION REJECTED</option>
<option value="APPLICATION APPROVED">APPLICATION APPROVED</option>
<option value="DOCUMENTS REQUIRED">DOCUMENTS REQUIRED</option>
</select>
</p>
<p>
<label for="date">APPLICATION DATE : * </label>
<input type="text" name="date" id="date">
(DATE FORMAT : dd-MM-yyyy , Example : 11-05-2012) </p>
<p>
<p>
* Required
</p>
<input type="submit" name="submit" id="submit" value="Submit" />
<input type="submit" name="cancel" id="cancel" value="Cancel" onClick="document.location.href='main.php';return false;"/>
</p>
</form>
</body>
</html>