まず、const.php がページのあるディレクトリにあることを確認しました。
管理者が Web サイトを介して MySQL テーブルに複数のエントリを追加できるページを作成しています。JavaScript を使用してテキストボックス入力フィールドの配列を拡張し、管理者が必要以上のエントリを入力する必要がないようにします。ただし、管理者が追加しようとしているエントリが既に存在する場合は、追加されません。コードが実行されると、テーブルに追加されたエントリと、追加されなかったエントリが既に存在するため、ユーザーに通知されます。
以下は、入力配列を PHP コードに渡すフォームです。
form id=userform action="addplayers.php" method="post" >
<legend>Player Info</legend>
<ol>
<div id="dynamicInput">
<li>
<label for=player>Player</label>
<input id=player type="text" name="player[]">
</li>
<li>
<label for=team>Team</label>
<input id=team type="text" name="team[]">
</li>
<li>
<label for=path>Player page path</label>
<input id=path type="text" name="path[]">
</li>
<li>
<label for=image>Player image path</label>
<input id=image type="text" name="image[]">
</li>
<br/>
</div>
</ol>
<input type="button" value="ADD ANOTHER PLAYER" onClick="addInput();">
<button type=submit name=submit> Submit </button>
</form>
ここで、javascript コードは、入力配列を展開するテキストボックス入力フィールドを動的に作成します。
<script language="Javascript" type="text/javascript">
function addInput(){
var newdiv = document.createElement('div');
newdiv.innerHTML = "<li><label for=player>Player</label><input id=player type='text' name='player[]'></li>";
document.getElementById('dynamicInput').appendChild(newdiv);
var newdiv = document.createElement('div');
newdiv.innerHTML = "<li><label for=team>Team</label><input id=team type='text' name='team[]'></li>";
document.getElementById('dynamicInput').appendChild(newdiv);
var newdiv = document.createElement('div');
newdiv.innerHTML = "<li><label for=path>Player page path</label><input id=path type='text' name='path[]'></li>";
document.getElementById('dynamicInput').appendChild(newdiv);
var newdiv = document.createElement('div');
newdiv.innerHTML = "<li><label for=image>Player image path</label><input id=image type='text' name='image[]'></li><br/>";
document.getElementById('dynamicInput').appendChild(newdiv);
}
</script>
これは、フォームの投稿先の php コードです。
include "const.php";
$entry_results = "";
if( isset($_POST['submit']) )
{
$conn = mysql_connect(MYSQL_HOST, MYSQL_LOGIN, MYSQL_PASSWORD) or die("Could not connect: " . mysql_error());
mysql_select_db(MYSQL_DB);
$player = $_POST['player'];
$team = $_POST['team'];
$path = $_POST['path'];
$image = $_POST['image'];
$invalid = array();
$valid = array();
$j = 0;
$k = 0;
for($i=0; $i<count($player);$i++)
{
//Check to see if player is in the database
$query = "Select name FROM tblPlayers where name = '" . $player[i] ."'";
$result = mysql_query($query);
if(!empty($result))//if query gives a result add player to list of invalid entries
{
$invalid[$j++] = $player[$i];
}
else//otherwise add to database
{
$valid[$k++] = $player[$i];
if(empty($image[$i]))
{$image[$i] = '#';}
if(empty($path[$i]))
{$path[$i] = '#';}
$query = "SELECT entity_id FROM tblTeams WHERE team = '" . $team[$i] . "'";
$result = mysql_query($query);
$query = "INSERT INTO tblPlayers ( team_id, name, image, path) VALUES (
'" . $result . "',
'" . $player[$i] . "',
'" . $image[$i] . "',
'" . $path[$i] . "'
)";
$result = mysql_query($query);
}
}
if(!empty($invalid[0]))
{
for($i=0;$i<count($invalid);$i++){
$entry_results .= $invalid[$i];
if(($i+1)!=count($invalid))
$entry_results .= ', ';
}
$entry_results .= "were found in the database and were not enterered to prevent duplicant record. ";
}
if(!empty($valid[0]))
{
for($i=0;$i<count($valid);$i++){
$entry_results .= $invalid[$i];
if(($i+1)!=count($valid))
$entry_results .= ', ';
}
$entry_results .= "were entered into the players table.";
}
mysql_close($conn);
}
?>
PHP コードのこの別の行は、管理者にエントリの結果を伝えます。
<?php
if( !empty($entry_results) )
{
echo "<h3>$register_message</h3><br />\n";
}
?>