Php 当尝试添加更多字段并选择其冲突的第一行时

Php 当尝试添加更多字段并选择其冲突的第一行时,php,mysql,Php,Mysql,当我试图从第一行中选择选项值时,并没有问题,但若我添加更多选项并在第二行中选择可选值,那个么它首先会发生冲突。每次选择可选值时,只有第一行冲突我希望在更改第一个选择选项时更改第一行。第二行选择仅更改第二行值 index.php 当您更改下拉列表中的选择时,它会将请求发送到服务器: getsaleinfo.php,项目索引为“hello” 执行 从项目中选择*,其中项目名称为“hello”一行 发送 <div class="float-left"> <!--<lab

当我试图从第一行中选择选项值时,并没有问题,但若我添加更多选项并在第二行中选择可选值,那个么它首先会发生冲突。每次选择可选值时,只有第一行冲突我希望在更改第一个选择选项时更改第一行。第二行选择仅更改第二行值

index.php


当您更改下拉列表中的选择时,它会将请求发送到服务器:

getsaleinfo.php,项目索引为“hello”

执行

从项目中选择*,其中项目名称为“hello”一行

发送

<div class="float-left">
    <!--<label id="unit" ></label>-->
    <input  type="text"  name="unit_price" id="unit_price" class="input-mini" 
            value="<?php echo $row['item_price'];?>" >
</div>
在salesdetail函数中,这只是指第一个ID,因为根据HTML,每个ID只能有一个实例,而其他ID则被忽略。

试试这个

代替一次呼叫,这样就可以了

/*------------------------index.php--------------------------*/
<?php 
if (!empty($_POST["save"])) {
    $conn = mysql_connect("localhost", "root", "");
    mysql_select_db("ajaxphp", $conn);
    $itemCount = count($_POST["item_index"]);
    $itemValues = 0;
    $query = "INSERT INTO item (item_name,item_price) VALUES ";
    $queryValue = "";

    for ($i = 0; $i < $itemCount; $i++) {
        if (!empty($_POST["item_index"][$i]) || !empty($_POST["unit_price"][$i])) {
            $itemValues++;
            if ($queryValue != "") {
                $queryValue .= ",";
            }
            $queryValue .= "('" . $_POST["item_index"][$i] . "', '" . $_POST["unit_price"][$i] . "')";
        }
    }
    $sql = $query . $queryValue;
    if ($itemValues != 0) {
        $result = mysql_query($sql);
        if (!empty($result))
            $message = "Added Successfully.";
    }
}
?>
<HTML>
    <HEAD>
        <TITLE>PHP jQuery Dynamic Textbox</TITLE>
        <LINK href="style.css" rel="stylesheet" type="text/css" />
        <SCRIPT src="http://code.jquery.com/jquery-2.1.1.js"></SCRIPT>
        <SCRIPT>
            var cnt = 1;
            function addMore() {
                $("<DIV>").load("input.php?cnt=" + cnt, function() {
                    $("#product").append($(this).html());
                    cnt++;
                });
            }
            function deleteRow() {
                $('DIV.product-item').each(function(index, item) {
                    jQuery(':checkbox', this).each(function() {
                        if ($(this).is(':checked')) {
                            $(item).remove();
                        }
                    });
                });
            }
        </SCRIPT>
    </HEAD>
    <BODY>
        <FORM name="frmProduct" method="post" action="">
            <DIV id="outer">
                <DIV id="header">
                    <DIV class="float-left">&nbsp;</DIV>
                    <DIV class="float-left col-heading">Item Name</DIV>
                    <DIV class="float-left col-heading">Item Price</DIV>
                </DIV>
                <DIV id="product">
                    <?php require_once("input.php") ?>
                </DIV>
                <DIV class="btn-action float-clear">
                    <input type="button" name="add_item" value="Add More" onClick="addMore();" />
                    <input type="button" name="del_item" value="Delete" onClick="deleteRow();" />
                    <span class="success"><?php
                        if (isset($message)) {
                            echo $message;
                        }
                        ?></span>
                </DIV>
                <DIV class="footer">
                    <input type="submit" name="save" value="Save" />
                </DIV>
            </DIV>
        </form>
    </BODY>
</HTML>
input.php

/*------------------------input.php--------------------------*/

<script type="text/javascript" src="js/jquery.min.js"></script>
<script>
    function salesdetail(item_index, item_id)
    {
        alert(item_index);

        $.ajax({
            url: 'getsaleinfo.php',
            type: 'POST',
            data: {item_index: item_index, item_id: item_id},
            success: function(result) {
                alert(result);
                $('#div_' + item_id).html(result);
            }
        });
    }
</script>
<?php $_REQUEST['cnt'] = (isset($_REQUEST['cnt'])) ? $_REQUEST['cnt'] : 0; ?>
<DIV class="product-item float-clear" style="clear:both;">
    <DIV class="float-left"><input type="checkbox" name="item_ind[]" id="item_ind_<?php echo $_REQUEST['cnt']; ?>" /></DIV>
    <DIV class="float-left"><select name="item_index[]" id="item_index_<?php echo $_REQUEST['cnt']; ?>"  class="required input-small" onchange="salesdetail(this.value, '<?php echo $_REQUEST['cnt']; ?>');" >
            <option>Select</option>
            <?php
            $conn = mysql_connect("localhost", "root", "");
            mysql_select_db("ajaxphp", $conn);
            $result = mysql_query("select * from item");
            while ($row = mysql_fetch_assoc($result)) {
                echo "<option>" . $row['item_name'] . "</option>";
            }
            ?>
        </select></DIV>
    <DIV class="float-left" id="div_<?php echo $_REQUEST['cnt']; ?>"><input type="text" id="unit_price_<?php echo $_REQUEST['cnt']; ?>" name="unit_price[]" /></DIV>
</DIV>
getsaleinfo.php

/*------------------------getsaleinfo.php--------------------------*/
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("ajaxphp", $conn);
$supplier = $_POST['item_index'];
$sql = "select * from item where item_name='$supplier'";
$rs = mysql_query($sql);
?>
<?php
$_REQUEST['item_id'] = (isset($_REQUEST['item_id'])) ? $_REQUEST['item_id'] : '';

if ($row = mysql_fetch_array($rs)) {
    ?>
    <div class="float-left">
        <input  type="text"  name="unit_price[]" id="unit_price_<?php echo $_REQUEST['item_id']; ?>" class="input-mini" value="<?php echo $row['item_price']; ?>" >

    </div>
<?php }
?>

我已经调整了你们的代码,但我仍然不能得到你们的问题是什么…事实上,问题是当我在Droop down中选择可选值时,首先你们选择hi,然后自动将值输入项目价格。当您添加更多内容并选择下拉菜单“您好”时,其值仅显示在第一个文本字段中,而不显示在第二个文本字段中。@Bhavesh添加一些屏幕截图将有助于理解问题。仅从代码来看,它不是很清楚。此外,考虑重新措辞标题。谢谢,但我没有让你正确地编码我必须使用MeNCK。你能推荐我吗?用这个函数代替salesdetailitem_索引并回复结果它不起作用,但我从其他人那里得到了解决方案。谢谢你尝试。。。
<div class="float-left">
    <!--<label id="unit" ></label>-->
    <input  type="text"  name="unit_price" id="unit_price" class="input-mini" 
            value="<?php echo $row['item_price'];?>" >
</div>
$('#div1').html(result)
        $('body').on('change','#item_index',function() {    //works for ajax loaded contents
        var id      =   $("#item_index").val();

        var formid  =   new FormData();

        formid.append('item_index',id);

        $.ajax({            

                        url         :   'getsaleinfo.php',
                        dataType    :   'text',
                        cache       :   false,
                        contentType :   false,
                        processData :   false,
                        data        :   formid,        
                        type        :   'post',
                        success     :   function(data){     
                                    alert(result);
                                    $('#div1').html(result);                        
                                    //document.getElementById("div1").innerHTML=data;

                        }
                });
     }
/*------------------------index.php--------------------------*/
<?php 
if (!empty($_POST["save"])) {
    $conn = mysql_connect("localhost", "root", "");
    mysql_select_db("ajaxphp", $conn);
    $itemCount = count($_POST["item_index"]);
    $itemValues = 0;
    $query = "INSERT INTO item (item_name,item_price) VALUES ";
    $queryValue = "";

    for ($i = 0; $i < $itemCount; $i++) {
        if (!empty($_POST["item_index"][$i]) || !empty($_POST["unit_price"][$i])) {
            $itemValues++;
            if ($queryValue != "") {
                $queryValue .= ",";
            }
            $queryValue .= "('" . $_POST["item_index"][$i] . "', '" . $_POST["unit_price"][$i] . "')";
        }
    }
    $sql = $query . $queryValue;
    if ($itemValues != 0) {
        $result = mysql_query($sql);
        if (!empty($result))
            $message = "Added Successfully.";
    }
}
?>
<HTML>
    <HEAD>
        <TITLE>PHP jQuery Dynamic Textbox</TITLE>
        <LINK href="style.css" rel="stylesheet" type="text/css" />
        <SCRIPT src="http://code.jquery.com/jquery-2.1.1.js"></SCRIPT>
        <SCRIPT>
            var cnt = 1;
            function addMore() {
                $("<DIV>").load("input.php?cnt=" + cnt, function() {
                    $("#product").append($(this).html());
                    cnt++;
                });
            }
            function deleteRow() {
                $('DIV.product-item').each(function(index, item) {
                    jQuery(':checkbox', this).each(function() {
                        if ($(this).is(':checked')) {
                            $(item).remove();
                        }
                    });
                });
            }
        </SCRIPT>
    </HEAD>
    <BODY>
        <FORM name="frmProduct" method="post" action="">
            <DIV id="outer">
                <DIV id="header">
                    <DIV class="float-left">&nbsp;</DIV>
                    <DIV class="float-left col-heading">Item Name</DIV>
                    <DIV class="float-left col-heading">Item Price</DIV>
                </DIV>
                <DIV id="product">
                    <?php require_once("input.php") ?>
                </DIV>
                <DIV class="btn-action float-clear">
                    <input type="button" name="add_item" value="Add More" onClick="addMore();" />
                    <input type="button" name="del_item" value="Delete" onClick="deleteRow();" />
                    <span class="success"><?php
                        if (isset($message)) {
                            echo $message;
                        }
                        ?></span>
                </DIV>
                <DIV class="footer">
                    <input type="submit" name="save" value="Save" />
                </DIV>
            </DIV>
        </form>
    </BODY>
</HTML>
/*------------------------input.php--------------------------*/

<script type="text/javascript" src="js/jquery.min.js"></script>
<script>
    function salesdetail(item_index, item_id)
    {
        alert(item_index);

        $.ajax({
            url: 'getsaleinfo.php',
            type: 'POST',
            data: {item_index: item_index, item_id: item_id},
            success: function(result) {
                alert(result);
                $('#div_' + item_id).html(result);
            }
        });
    }
</script>
<?php $_REQUEST['cnt'] = (isset($_REQUEST['cnt'])) ? $_REQUEST['cnt'] : 0; ?>
<DIV class="product-item float-clear" style="clear:both;">
    <DIV class="float-left"><input type="checkbox" name="item_ind[]" id="item_ind_<?php echo $_REQUEST['cnt']; ?>" /></DIV>
    <DIV class="float-left"><select name="item_index[]" id="item_index_<?php echo $_REQUEST['cnt']; ?>"  class="required input-small" onchange="salesdetail(this.value, '<?php echo $_REQUEST['cnt']; ?>');" >
            <option>Select</option>
            <?php
            $conn = mysql_connect("localhost", "root", "");
            mysql_select_db("ajaxphp", $conn);
            $result = mysql_query("select * from item");
            while ($row = mysql_fetch_assoc($result)) {
                echo "<option>" . $row['item_name'] . "</option>";
            }
            ?>
        </select></DIV>
    <DIV class="float-left" id="div_<?php echo $_REQUEST['cnt']; ?>"><input type="text" id="unit_price_<?php echo $_REQUEST['cnt']; ?>" name="unit_price[]" /></DIV>
</DIV>
/*------------------------getsaleinfo.php--------------------------*/
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("ajaxphp", $conn);
$supplier = $_POST['item_index'];
$sql = "select * from item where item_name='$supplier'";
$rs = mysql_query($sql);
?>
<?php
$_REQUEST['item_id'] = (isset($_REQUEST['item_id'])) ? $_REQUEST['item_id'] : '';

if ($row = mysql_fetch_array($rs)) {
    ?>
    <div class="float-left">
        <input  type="text"  name="unit_price[]" id="unit_price_<?php echo $_REQUEST['item_id']; ?>" class="input-mini" value="<?php echo $row['item_price']; ?>" >

    </div>
<?php }
?>