获取特定文本框的值

To fetch value of particular textbox

在这里,我使用会话来存储多个文本框值。 但是,当我要从会话中获取数据时,我会为会话中的所有文本框获取相同的值。

我的代码:

if ($order_list) {
     $i = $start +1; 
     foreach ($order_list as $row) 
     {
?>
<input type="text" name="<?php echo $row['id']; ?>" class="txt" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" value="<?php if(isset($_SESSION['txtval'])) { echo $_SESSION['txtval'];} ?>">
<?php } ?>

在javascript中:

$(document).on('blur','.txt',function(){
    var myVar = $(this).val();

    //alert(myVar);
    $.ajax({
            type: "GET",
            url: "view_orders_checked_array.php",
            data: {account: myVar, task: 'alltxt'},
            async: false
        });
 });

在view_orders_checked_array.php中:

$task = $_GET['task'];
    if($task == "alltxt")
    {

        $_SESSION['txtval'] = $account;
    }

在这里,我没有得到特定文本框的值。我正在获取上次插入的值。 我哪里错了?

问题是您在所有文本字段中输入了相同的值

$_SESSION['txtval'] 

在你的循环中总是一样的。

编辑

而且我认为您得到的最后插入值相同,因为不是将所有文本字段存储在数组 $_SESSION['txtval']['another_id_key'] 中,而是将其存储在 $_SESSION['txtval'] 只有一个值

你还必须在会话中维护数组,这样你就可以在 ids 的帮助下完成

 var id=your loop id;
 data: {account: myVar, task: 'alltxt',id:id },

并在您的 view_orders_checked_array 页面中

$task = $_GET['task'];
$id=$_GET['id'];
if($task == "alltxt")
{

    $_SESSION['txtval'][$id] = $account;
}

并在您的代码中

 <input type="text" name="<?php echo $row['id']; ?>" class="txt" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" value="<?php if(isset($_SESSION['txtval'])) { echo $_SESSION['txtval'][$row['id']];} ?>">

我建议你使用POST方法来传递值

检查下面的工作代码,如果你只是用你的 txtval 传递 id 并为每个 id 创建会话键 。现在,当您打印会话数组时,您将获得会话数组中的所有键值。难懂的请追问

Javascript

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<?php
session_start();
$_SESSION['txtval'] = '';
$order_list[0] = array('id'=>'1');
$order_list[1] = array('id'=>'2');
$order_list[2] = array('id'=>'3');
$order_list[3] = array('id'=>'4');

$start = '';
if ($order_list) {
    $i = $start + 1;
    foreach ($order_list as $row) {
        ?>
        <input type="text" name="<?php echo $row['id']; ?>" class="txt" autocomplete="off"
               id="txtid_<?php echo $row['id']; ?>" value="<?php if (isset($_SESSION['txtval'])) {
            echo $_SESSION['txtval'];
        } ?>">
    <?php }
}?>

<script type="text/javascript">
    $(document).on('blur','.txt',function(){
        var myVar = $(this).val();
        var myVarid = this.id;
        $.ajax({
                type: "GET",
                url: "view_orders_checked_array.php",
                data: {account: myVar, task: 'alltxt', id: myVarid },
                async: false,
                success:function(data){
                    console.log(data);
                }
            });
     });
</script>

PHP 文件 view_orders_checked_array.php

<?php
session_start();
$task = $_GET['task'];
if ($task == "alltxt") {

    $_SESSION['txtval'][$_REQUEST['id']] = $_REQUEST['account'];
}
echo '<pre>';print_r($_SESSION['txtval'] );echo '</pre>';
die('Call');