|
|
|
เพิ่มข้อมูลแบบ Multi row Record ต้องเขียนโค้ดทำยังไงครับ |
|
|
|
|
|
|
|
คือผมต้องการบันทึกข้อมูลแบบ multi rowต้องทำไงอ่ะครับ
ถ้าเป็น php
Form (PHP)
<script type="text/javascript">
var rowNum = 0;
function addRow(frm) {
// exp_id
if(jQuery('#exp_id').val()==''){
alert('เลือกค่าใช่จ่าย');
jQuery('#exp_id').focus();
return false;
};
rowNum ++;
/*var row = '<p id="rowNum'+rowNum+'">Item quantity: <input type="text" name="qty[]" size="4" value="'+frm.add_qty.value+'"> Item name: <input type="text" name="name[]" value="'+frm.add_name.value+'"> <input type="button" value="Remove" onclick="removeRow('+rowNum+');"></p>';*/
var row = '<p id="rowNum'+rowNum+'"><input type="checkbox" checked="true" name="exp_index[]" size="4" value="'+rowNum+'"><input type="hidden" name="exp_id['+rowNum+']" value="'+jQuery('#exp_id').val()+'" /> ' + jQuery('#exp_id option:selected').text()+' จำนวน <input type="text" class="input-mini isnumber" name="exp_amount['+rowNum+']" value="'+jQuery('#exp_amount').val()+'"> <input type="button" class="btn " value="ลบ" onclick="removeRow('+rowNum+');"></p>';
//$('#yourdropdownid option:selected').text();
jQuery('#itemRows').append(row);
jQuery('#exp_id').val('0');
jQuery('#exp_amount').val('');
}
function removeRow(rnum) {
jQuery('#rowNum'+rnum).remove();
}
</script>
<div class="row-fluid">
<div class="control-group span10" >
<label class="control-label" for="task_main">ค่าใช้จ่าย</label>
<div class="controls">
<?
//CAST(Field1 AS Text) As Field1
$sql = "SELECT exp_id AS id,CAST(exp_name AS varchar(50)) as name FROM tbexpenese";
//$sql = "SELECT CAST(exp_id AS varchar(12)) as id, CAST(exp_name AS varchar(12)) as name FROM tbexpenese";
$result = mssql_query($sql);
//$arr = mssql_fetch_array($result);
//print_r($arr);
?>
<select class="input-xlarge" name="exp_id" id="exp_id">
<option value=""></option>
<? while($rec_exp = mssql_fetch_assoc($result)){ ?>
<option value="<?=$rec_exp["id"]?>">
<?=$name=($rec_exp["name"]); ?>
</option>
<? } ?>
</select>
จำนวนเงิน <input name="text" type="text" class="input-mini isnumber" id="exp_amount" />
<input onClick="addRow(this.form);" class="btn" type="button" value="เพิ่มค่าใช้จ่าย" />
</div>
</div>
Submit(PHP)
$budget_index = $_POST["budget_index"];
$budget_id = $_POST["budget_id"];
$budget_amount = $_POST["budget_amount"];
//INSERT INTO budget_expenese(exp_id, exp_amount, withdraw_id)
//VALUES (,,)
$c = count($budget_index);
//echo "<br>count exp index".$c;
if ($c>0)
{
foreach($budget_index as $c){
//echo "<br>c :".$c;
//echo "<br>budget_id :".$budget_id[$c];
//echo "<br>budget_amonut :".$budget_amount[$c];
$sql_from = "INSERT INTO budget_aval_from(id,withdraw_id, budget_year, budget_id, budget_amount)
VALUES ('$budget_index[$c]','$withdraw_id','$budget_year','$budget_id[$c]','$budget_amount[$c]')";
//echo "<br>$sql_aval";
$objQuery3 = mssql_query($sql_from);
if(!$objQuery3)
{
mssql_query("ROLLBACK");
echo "Error Save [".$sql_from."] ".mssql_get_last_message();
exit();
}
}//end foreach budget_id
}
แล้วถ้าเป็น ASP จะต้องเขียนยังไงอ่ะครับ
ผมลองเขียนให้คล้ายกันแล้วมันขึ้น http 500
Form (ASP)
<script type="text/javascript">
var rowNum = 0;
function addRow(frm) {
// exp_id
if(jQuery('#status_answer').val()==''){
alert('เลือกคำตอบด้วย');
jQuery('#status_answer').focus();
return false;
};
rowNum ++;
/*var row = '<p id="rowNum'+rowNum+'">Item quantity: <input type="text" name="qty[]" size="4" value="'+frm.add_qty.value+'"> Item name: <input type="text" name="name[]" value="'+frm.add_name.value+'"> <input type="button" value="Remove" onclick="removeRow('+rowNum+');"></p>';*/
var row = '<tr id="rowNum'+rowNum+'"><td><input type="checkbox" checked="true" name="answerindex" size="4" value="'+rowNum+'"><input type="hidden" name="hdnno('+rowNum+')" value="'+rowNum+'"></td><td><input type="hidden" name="choise_answer('+rowNum+')" value="'+jQuery('#choise_answer').val()+'">'+jQuery('#choise_answer').val()+'</td><td><input type="hidden" name="status_answer('+rowNum+')" value="'+jQuery('#status_answer').val()+'">'+ jQuery('#status_answer option:selected').text()+'</td><td> <input type="button" class="btn " value="ลบ" onclick="removeRow('+rowNum+');"></td></tr>';
//$('#yourdropdownid option:selected').text();
jQuery('#itemRows').append(row);
jQuery('#choise_answer').val('');
jQuery('#status_answer').val('');
}
function removeRow(rnum) {
jQuery('#rowNum'+rnum).remove();
}
</script>
<div class="form-group">
<label class="col-sm-2 control-label"></label>
<div class="col-sm-10">
<table width="100%">
<thead>
<th width="10%"></th>
<th width="55%">ตัวเลือกคำตอบ</th>
<th width="25%">คำตอบ</th>
<th width="10%"></th>
</thead>
<tbody id="itemRows">
</tbody>
</table>
</div>
</div><!-- from-group -->
<div class="form-group">
<label class="col-sm-2 control-label">ตัวเลือกคำตอบ</label>
<div class="col-sm-5">
<input type="text" id="choise_answer" value="" placeholder="" class="form-control " />
</div>
<label class="col-sm-1 control-label">คำตอบ</label>
<div class="col-sm-2">
<select id="status_answer" class="form-control ">
<option value="" selected>สถานะ</option>
<option value="1">ถูก</option>
<option value="0">ผิด</option>
</select>
</div>
<div class="col-sm-2">
<input onClick="addRow(this.form);" class="form-control btn-success glyphicon glyphicon-search mini" type="button" value="เพิ่ม" />
</div>
</div><!-- from-group -->
Submit (ASP)
Dim answer,exe_a,i,j
For i = 0 To UBound (Request.Form("hdnno"))
j = i+1
answer = "INSERT INTO medquiz_answer(med_id,med_choice_id,med_choice_name,med_choice_answer,datein) VALUES('" & no & "','" & no&j & "','" & Request.Form("choise_answer")(j) & "','" & Request.Form("status_answer")(j) & "','" & dd & "')"
Set exe_a=con.Execute(answer)
Next
Tag : ASP, Ms SQL Server 2008, JavaScript, jQuery
|
|
|
|
|
|
Date :
2016-10-17 14:52:24 |
By :
Clamore |
View :
1580 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใชเวิธีแบบนี้ไม่น่าจะได้ครับ เพราะ Index น่าจะเปลี่ยนไปตามจำนวน Textbox mี่มรข้อมูล ให้ใช้วิธีกำหนด id หรือ name เป็นชื่อ rows แล้ว loop insert ตาม rows ครับ
|
|
|
|
|
Date :
2016-10-18 09:32:19 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|