2012-03-09 11 views
0

Iはstrategyresourceためtarget1呼ば行と動的即ちtarget2行を追加するJavaScript、target3等と同じでフォームを持っています。PHP動的データベース挿入

私はphpとmysqlを使ってこれらをデータベースに入力したいと思います。投稿された行の量は動的なので、私はforeachステートメントまたはwhileループが必要と思う。

私は運がないforeach()を試みました。

ここにコードがあります。

ダイナミック行のJavaScript/PHPファイル:ここで

<?php 
header('Content-type: application/x-javascript'); 
if($_REQUEST['type'] == "targets"){ 
?> 
function addRowToTargetTable() 
{ 
    var tbl = document.getElementById('targets'); 
    var lastRow = tbl.rows.length; 
    // if there's no header row in the table, then iteration = lastRow + 1 
    var iteration = lastRow; 
    var row = tbl.insertRow(lastRow); 

    // left cell 
    var cellLeft = row.insertCell(0); 
    var textNode = document.createTextNode(iteration + "."); 
    var targetid = document.createElement('input'); 
    targetid.type = 'hidden'; 
    targetid.name = 'target' + iteration; 
    targetid.id = 'target' + iteration; 
    targetid.value = 'target' + iteration; 
    cellLeft.appendChild(textNode); 
    cellLeft.appendChild(targetid); 

    // right cell 
    var cellRight = row.insertCell(1); 
    var el = document.createElement('textarea'); 
    el.name = 'target' + iteration; 
    el.id = 'target' + iteration; 
    el.className = 'validate[required]'; 
    el.style.cssText="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"; 
    cellRight.appendChild(el); 

    // far right cell 
    var cellFarRight = row.insertCell(2); 
    var el2 = document.createElement('textarea'); 
    el2.name = 'targetcriteria' + iteration; 
    el2.id = 'targetcriteria' + iteration; 
    el2.className = 'validate[required]'; 
    el2.style.cssText="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"; 
    cellFarRight.appendChild(el2); 

} 
function removeRowFromTargetTable() 
{ 
    var tbl = document.getElementById('targets'); 
    var lastRow = tbl.rows.length; 
    if (lastRow > 2) tbl.deleteRow(lastRow - 1); 
} 
<?php 
}elseif($_REQUEST['type'] == "strategies"){ 
?> 
function addRowToStrategyTable() 
{ 
    var tbl = document.getElementById('strategies'); 
    var lastRow = tbl.rows.length; 
    // if there's no header row in the table, then iteration = lastRow + 1 
    var iteration = lastRow; 
    var row = tbl.insertRow(lastRow); 

    // left cell 
    var cellLeft = row.insertCell(0); 
    var textNode = document.createTextNode(iteration + "."); 
    var strategyid = document.createElement('input'); 
    strategyid.type = 'hidden'; 
    strategyid.name = 'strategy' + iteration; 
    strategyid.id = 'strategy' + iteration; 
    strategyid.value = 'strategy' + iteration; 
    cellLeft.appendChild(textNode); 
    cellLeft.appendChild(strategyid); 

    // right cell 
    var cellRight = row.insertCell(1); 
    var el = document.createElement('textarea'); 
    el.name = 'strategy' + iteration; 
    el.id = 'strategy' + iteration; 
    el.className = 'validate[required]'; 
    el.style.cssText="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"; 
    cellRight.appendChild(el); 

} 
function removeRowFromStrategyTable() 
{ 
    var tbl = document.getElementById('strategies'); 
    var lastRow = tbl.rows.length; 
    if (lastRow > 2) tbl.deleteRow(lastRow - 1); 
} 

<?php }elseif($_REQUEST['type'] == "resources"){ 
?> 
function addRowToResourceTable() 
{ 
    var tbl = document.getElementById('resources'); 
    var lastRow = tbl.rows.length; 
    // if there's no header row in the table, then iteration = lastRow + 1 
    var iteration = lastRow; 
    var row = tbl.insertRow(lastRow); 

    // left cell 
    var cellLeft = row.insertCell(0); 
    var textNode = document.createTextNode(iteration + "."); 
    var resourceid = document.createElement('input'); 
    resourceid.type = 'hidden'; 
    resourceid.name = 'resource' + iteration; 
    resourceid.id = 'resource' + iteration; 
    resourceid.value = 'resource' + iteration; 
    cellLeft.appendChild(textNode); 
    cellLeft.appendChild(resourceid); 

    // right cell 
    var cellRight = row.insertCell(1); 
    var el = document.createElement('textarea'); 
    el.name = 'resource' + iteration; 
    el.id = 'resource' + iteration 
    el.style.cssText="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"; 
    cellRight.appendChild(el); 

} 
function removeRowFromResourceTable() 
{ 
    var tbl = document.getElementById('resources'); 
    var lastRow = tbl.rows.length; 
    if (lastRow > 2) tbl.deleteRow(lastRow - 1); 
} 
<?php 
} 
?> 

は形式です:

<div id="non-indent"> 
<form action="complete-iep-operations.php?operation=create&action=insert" method="post" id="validate" enctype="multipart/form-data"> 
<table border="1" class="listing full" id="register" style="border-collapse:collapse;"> 
    <tr class="no-pointer"> 
    <th scope="col" width="48"> 
     <h4>IEP No.</h4> 
    </th> 
    <th scope="col" width="153"> 
     <h4>IEP Date</h4> 
    </th> 
    <th scope="col" width="124"> 
     <h4>Renewal Date</h4> 
    </th> 
    <th scope="col" width="116"> 
     <h4>IEP Meeting Conducted</h4> 
    </th> 
    </tr> 
    <tr> 
    <td> 
    <p><input name="iep_number" type="text" id="iep_number" maxlength="2" class="validate[required]" style="margin-top:5px; width:40px;" /></p> 
    </td> 
    <td> 
    <p> 
     <input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="iep-day" id="iep-day" maxlength="2" /> 
     - 
     <input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="iep-month" id="iep-day" maxlength="2" /> 
     - 
     <input type="text" style="margin-top:5px; width:60px;" class="validate[required]" name="iep-year" id="iep-day" maxlength="4" /> 
    </p> 
    </td> 
    <td> 
    <p> 
     <input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="renewal-day" id="renewal-day" maxlength="2" /> 
- 
<input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="renewal-month" id="renewal-day" maxlength="2" /> 
- 
<input type="text" style="margin-top:5px; width:60px;" class="validate[required]" name="renewal-year" id="renewal-day" maxlength="4" /> 
    </p> 
    </td> 
    <td> 
    <p> 
     <input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="completed-day3" id="completed-day3" maxlength="2" /> 
- 
<input type="text" style="margin-top:5px; width:30px;" class="validate[required]" name="completed-day3" id="completed-day3" maxlength="2" /> 
- 
<input type="text" style="margin-top:5px; width:60px;" class="validate[required]" name="completed-day3" id="completed-day3" maxlength="4" /> 
    </p> 
    </td> 
    </tr> 
</table> 
<br /> 
<h3>Targets</h3> 
<br /> 
<table border="1" class="listing full" id="targets" style="border-collapse:collapse;"> 
    <tr class="no-pointer"> 
    <th scope="col" width="102"> 
     <h4>Target Number</h4> 
    </th> 
    <th scope="col"> 
     <h4>Target Details</h4> 
    </th> 
    <th scope="col"> 
     <h4>Target Achievement Criteria</h4> 
    </th> 
    </tr> 
    <tr> 
    <td> 
    <p>1.</p> 
    <input type="hidden" value="target1" id="target1" /> 
    </td> 
    <td> 
     <label for="target1"></label> 
     <textarea name="target1" id="target1" class="validate[required]" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea> 
    </td> 
    <td> 
     <label for="targetcriteria1"></label> 
     <textarea name="targetcriteria1" id="targetcriteria1" class="validate[required]" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea> 
    </td> 
    </tr> 
</table> 
<br /> 
<table border="1" class="listing full" id="register" style="border-collapse:collapse;"> 
<tr> 
<td> 
<div align="right"> 
<input type="button" value="Remove Target Row" id="submit" onclick="removeRowFromTargetTable();" style="margin:0px; margin-right:10px; float:right;" /> 
<input type="button" value="Add Another Target" id="submit" onclick="addRowToTargetTable();" style="margin:0px; margin-right:20px; float:right;" /> 
</div> 
</td> 
</tr> 
</table> 
<br /> 
<h3>Strategies</h3> 
<br /> 
<table border="1" class="listing full" id="strategies" style="border-collapse:collapse;"> 
    <tr class="no-pointer"> 
    <th scope="col" width="102"> 
     <h4>Strategy Number</h4> 
    </th> 
    <th scope="col"> 
     <h4>Strategy Details</h4> 
    </th> 
    </tr> 
    <tr> 
    <td> 
    <p>1.</p> 
    <input type="hidden" value="strategy1" id="strategy1" /> 
    </td> 
    <td> 
     <label for="strategy1"></label> 
     <textarea name="strategy1" id="strategy1" class="validate[required]" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea> 
    </td> 
    </tr> 
</table> 
<br /> 
<table border="1" class="listing full" id="register" style="border-collapse:collapse;"> 
<tr> 
<td> 
<div align="right"> 
<input type="button" value="Remove Strategy Row" id="submit" onclick="removeRowFromStrategyTable();" style="margin:0px; margin-right:10px; float:right;" /> 
<input type="button" value="Add Another Strategy" id="submit" onclick="addRowToStrategyTable();" style="margin:0px; margin-right:20px; float:right;" /> 
</div> 
</td> 
</tr> 
</table> 
<br /> 
<h3>Resources</h3> 
<br /> 
<table border="1" class="listing full" id="resources" style="border-collapse:collapse;"> 
    <tr class="no-pointer"> 
    <th scope="col" width="102"> 
     <h4>Resource Number</h4> 
    </th> 
    <th scope="col"> 
     <h4>Resource Details</h4> 
    </th> 
    </tr> 
    <tr> 
    <td> 
    <p>1.</p> 
    <input type="hidden" value="resource1" id="resource1" /> 
    </td> 
    <td> 
     <label for="resource1"></label> 
     <textarea name="resource1" id="resource1" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea> 
    </td> 
    </tr> 
</table> 
<br /> 
<table border="1" class="listing full" id="register" style="border-collapse:collapse;"> 
<tr> 
<td> 
<div align="right"> 
<input type="button" value="Remove Resource Row" id="submit" onclick="removeRowFromResourceTable();" style="margin:0px; margin-right:10px; float:right;" /> 
<input type="button" value="Add Another Resource" id="submit" onclick="addRowToResourceTable();" style="margin:0px; margin-right:20px; float:right;" /> 
</div> 
</td> 
</tr> 
</table> 
<br /> 
<table border="0" class="full" id="register" style="border-collapse:collapse;"> 
<tr> 
<td> 
<div align="right"> 
<input name="submit" type="submit" id="submit" style="margin:0px; margin-right:10px; float:right;" /></div> 
</td> 
</tr> 
</table> 
</form> 
</div> 

とPHPコード:

<?php 
mysql_connect('host', 'XXXXX', 'XXXXX') or die(mysql_error()); 
mysql_select_db('db401728075') or die(mysql_error()); 

if($_REQUEST['operation']=="create" && $_REQUEST['action']=="insert") { 

$targeti = 1; 
while(true) { 
    if(isset($_POST['target'.$targeti])) { 
     $target = $_POST['target'.$targeti]; 
     echo $target; 
     echo "<BR>"; 
     $targeti++; 
    } else { 
     break; 
    } 
} 

あなたは私を与えることができますすべてのヘルプは次のようになり高く評価。

+7

**公共のWebページ上のホスト、ユーザ名とパスワードのようなあなたのデータベース接続情報を投稿しないでください** – tonymarschall

+0

使用名=「ターゲット[]」のために!ターゲットの入力を受け入れ、ループすることができる配列を返す – Ruben

答えて

1

$ _POST ['target']は存在しないため、反復できません。あなたはそのようなものが必要:

$targeti = 1; 
while(true) { 
    if(isset($_POST['target'.$targeti])) { 
     $target = $_POST['target'.$targeti]; 
     // process $target value here 
     $targeti++; 
    } else { 
     break; 
    } 
} 

および他のタイプEDITED

のために同じコード:あなたがループ内で$目標の値を処理したくない場合は、配列に保存することができます($ targets [] = $ target)。変数を動的に作成する場合($ {'target'。$ targeti} = $ _POST [...])、カウンタ変数を保存する必要があります$ targeti)、作成したすべての変数を見つけるためにあとで使ってください。

+0

このコードをありがとう。しかし、これはサイトをループさせ、ページのエラー500をロードしません。なぜでしょうか? –

+0

更新されたコードを見てください。私はbreak文を追加するのを忘れていました。ご迷惑をおかけします。 –

+0

まだエラー500が発生しています –

0

あなたのPHPコードでは、あなたはどんなmysqlの挿入アクションも行っていません。また$targeti = "2";$targeti++;のようになります。そうでない場合は、常にtarget2でループします。

+0

なぜなら、それはページの下にあるエコーの$ _postを識別しないため、挿入を含まない理由です。それはそれを挿入するときです。 (投稿がある場合) –

+0

と編集された質問 –

0

私は間違っているかもしれませんが、私は確認します。

<textarea name="target []" id="target" class="validate[required]" style="margin:4px; max-width:400px; width:400px; max-height:35px; height:35px;"></textarea> 

あなたの値は配列としてアップロードされているそのように:

HTMLのように記述する必要があります。

そのため

とPHPは次のよ​​うになります。

<?php 
if(isset($_POST['target'])) { 
     $target = $_POST['target'];    
     foreach($target as $targetvalue){ 
      echo $targetvalue; // do something with the target value here. 
     } 
    } 
?> 
+0

ローカル変数に値を代入しているのはなぜですか?本当に理由はありません、そうですか? –

+0

ブラウザーがサーバーに名前を送信するため、Name属性は "target []"でなければなりません –

+0

私の悪いです。急いでそれを書いた。ありがとう。それは問題を解決しますか? –

関連する問題