Viral Bhoot
Viral Bhoot

Reputation: 357

To fetch value of particular textbox

Here, I am using session to store multiple textbox value. But when I am going to fetch from session, I am getting the same value for all textbox which is in session.

My code:

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 } ?>

In 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
        });
 });

In view_orders_checked_array.php :

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

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

Here, I am not getting the value of particular textbox. I am getting the value which I have last inserted. Where I am going wrong?

Upvotes: 1

Views: 92

Answers (3)

Zeeshan
Zeeshan

Reputation: 1675

Check below working code, if you just pass the id with your txtval and create session for each id key and value . Now when you print session array you will get all key values in session array. Please ask if difficult to understand.

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 file 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');

Upvotes: 3

Abbas
Abbas

Reputation: 412

you have to maintain array in session also so that you can do with the help of ids

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

and in your view_orders_checked_array page

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

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

and in your code

 <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']];} ?>">

i suggest you to use POST method for passing values

Upvotes: 2

Standej
Standej

Reputation: 753

Problem is that you are putting the same value in all text fields

$_SESSION['txtval'] 

in your loop is always the same.

Edit

And also I think you getting same last inserted value because instead to store all text fields in array $_SESSION['txtval']['another_id_key'] you storing it in $_SESSION['txtval'] which is only one value

Upvotes: 0

Related Questions