当点击按钮添加行和更新记录 [英] when click button added row and update record

查看:66
本文介绍了当点击按钮添加行和更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



这个问题我几天都在挣扎。



我的应用程序,我已经用一个按钮添加行,所以当我点击 addrow 按钮行添加成功。

如果多次点击,还会添加多行。

在添加的行中,我有一个下拉框和一个文本框。

In下拉,我有一些来自数据库的项目。

每当我选择一个项目时,根据所选项目填充文本框中的值。因此,每当我尝试更新多行时,更新行都不起作用。

只有第一行正在更新,其余行不会更新。



Mycode就在这里。

Hi friends,

I am struggling for few days for this issue.

In my appication, I have taken one button for adding row, so when I click addrow button row added successfully.
If click multiple times, multiple rows also added.
In added row, I have taken one dropdown box and one text box.
In drop down, I have some items coming from database.
Whenever I select one item, based on that selected item populating value in textbox. So, whenever I try to update multiple rows, that updating rows are not working.
Only first row is updating and the remaining rows are not updated.

Mycode is here.

<?php

?>
<SCRIPT language="javascript">

        function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var colCount = table.rows[0].cells.length;

            for(var i=0; i<colCount; i++) {



                var newcell = row.insertCell(i);



                newcell.innerHTML = table.rows[0].cells[i].innerHTML;

                //alert(newcell.childNodes);

                switch(newcell.childNodes[0].type) {

                    case "text":

                            newcell.childNodes[0].value = "";

                            break;

                    case "checkbox":

                            newcell.childNodes[0].checked = false;

                            break;

                    case "select-one":

                            newcell.childNodes[0].selectedIndex = 0;

                            break;

                }

            }

        }

        </script>

<script>
        if (window.XMLHttpRequest)
        {
              xmlhttp=new XMLHttpRequest();

        }
        else
        {
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        function validateform2(x)
        {
              //alert(x)
              xmlhttp.onreadystatechange=function()
              {
                  if (xmlhttp.readyState==4 && xmlhttp.status==200)
                  {
                document.getElementById("tax").value=xmlhttp.responseText;
                      //alert(tax);
                  }
              }

              xmlhttp.open("POST","state-tax.php",true);
              xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
              xmlhttp.send("key="+x);
        }
</script>

<form name="retire" action="state-apportionment-busd.php" method="POST">
<table border="0" align="left" width="100%" bgcolor="#d6eafb" cellpadding="8" cellspacing="1">
<tbody><tr>
<td width="14%" align="center" valign="top" class="tbl-area-tabs"><strong> Business Name</strong></td>
<td width="14%" align="center" valign="top" class="tbl-area-tabs"><strong> Tax ID Employer ID</strong></td>
</tr></table>

<table border="0" align="left" width="100%" bgcolor="#d6eafb" cellpadding="8" cellspacing="1">
<?php



$lu=$_COOKIE['lu'];//login user id

$sql="select * from state_apportionment where user_id=$lu";





//echo $sql;

$result=mysql_query($sql) or die("Query failed $sql".mysql_error());







while($row=mysql_fetch_array($result))

{

?>
<tr bgcolor="#FFFFFF">

<td width="14%" align="left" valign="top"  style="margin:0px auto;">
<select name="textfield1[]" class="select-box-new" id="state-one" onchange="return validateform2(this.value)">
<option value="">select</option>
<?php

$sql=mysql_query("select basic_name from business_profile where user_id=$lu");

while($result=mysql_fetch_array($sql))

{

?>
<option value="<?php echo $result['basic_name'];?>" <?php if($row['textfield1']==$result['basic_name']){?> selected="selected" <?php } ?>><?php echo $result['basic_name']; ?></option>
<?php

}

?>





</select></td>





<td width="14%" align="left" valign="top" style="margin:0px auto;">
<input name="textfield2[]" type="text" id="tax" size="8" value="<?php echo $row['textfield2']; ?>"/>
</td>
<?php } ?>




<table border="0" align="left" width="100%" bgcolor="#d6eafb" cellpadding="8" cellspacing="1" id="dataTable">
<tr>



<td width="14%" align="left" valign="top" style="margin:0px auto;">
<select name="textfield1[]" class="select-box-new" id="textfield1" id="state-one" onchange="return validateform2(this.value)">
<option value="">select</option>
<?php

$sql=mysql_query("select basic_name from business_profile where user_id=$lu");

while($result=mysql_fetch_array($sql))

{

?>
<option value="<?php echo $result['basic_name'];?>" <?php if($row['textfield1']==$result['basic_name']){?> selected="selected" <?php } ?>><?php echo $result['basic_name']; ?></option>
<?php

}

?>
</select></td>


<td width="14%" align="left" valign="top" style="margin:0px auto;">
<input name="textfield2[]" type="text" id="tax" size="8" /></td>



<br><INPUT type="button" value="Add Row" onClick="addRow('dataTable')" />

</table>

</table>
<table align="center">
<tr>
<td align="right" valign="middle"><input type="submit" name="submit" value="Save">
</td>
</tr>
</table>
</form>



//state-apportionment-busd.php

<?php

include "db.php";





$lu=$_COOKIE['lu'];

//echo "cookie".$_COOKIE['lu'];

extract($_POST);



$textfield1 = $_POST['textfield1'];

$textfield2 = $_POST['textfield2'];





//print_r($_POST);

//echo "<pre>";
//die();

$search_r1=mysql_query("select user_id from state_apportionment_total where user_id=".$lu);
$sql1="select * from state_apportionment  where user_id=$lu";

//echo $sql;
$result1=mysql_query($sql1) or die("Query failed $sql1".mysql_error());

if(mysql_num_rows($result1)!=0)
{
$sql1="DELETE FROM state_apportionment WHERE user_id=$lu";
$result1=mysql_query($sql1) or die("Query failed $sql1".mysql_error());
}
foreach($textfield1 as $a => $b)
    {

$sql="INSERT INTO state_apportionment  VALUES('',$lu,'$textfield1[$a]','$textfield2[$a]')";

//echo $sql;
$result=mysql_query($sql) or die("Query failed $sql".mysql_error());


}




/*else
{
        foreach($textfield1 as $a => $b)
    {
    $sql2="UPDATE retirement_expenses SET textfield1='$textfield1[$a]',textfield2='$textfield2[$a]' where user_id=$lu ";

//echo $sql2;
$result2=mysql_query($sql2) or die("Query failed $sql2".mysql_error());

    }}
 */
?>

<script>parent.location='somepage.php?data=update'; </script>





Please give solution.





Thanks & Regards



Mallesh

Email Removed



[Edit]

1. Email removed.

2. Grammatical errors corrected.

[/Edit]

推荐答案

lu=
lu=


_COOKIE['lu'];//login user id

_COOKIE['lu'];//login user id


sql=\"select * from state_apportionment where user_id=
sql="select * from state_apportionment where user_id=


这篇关于当点击按钮添加行和更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆