简体   繁体   中英

how to set the value of array into a input field with php

I have an array

$texts = [ "maybank2u.com",
           "Open BillPayment", 
           "Status: Successful", 
           "Reference number 2950211545", 
           "Transaction date: 01 Feb 2016 13:09:17", 
           "Amount: RM100.00", 
           "From Account 564155051577 WCAa" ]

HTML

<div class="row">
    <div class="col-12">
        <ul>
            <?php foreach ($texts as $row =>$value): ?> 
                <li><h3> <?php echo ucfirst($value) ?></h3></li>
            <?php endforeach ?>
        </ul>
         <label>Status:</label><input type="text" name="status" value="" id="status"><br>

    </div>
</div>

I tried this solution but it's not working in my case. I want to set the value of $value into the input field

<div class="row">
    <div class="col-12">
        <ul>
            <?php foreach ($texts as $row =>$value): ?> 
                <li><h3> <?php echo ucfirst($value) ?></h3></li>
            <?php endforeach ?>
        </ul>
         <label>Status:</label><input type="text" name="status" value="<?php echo $texts[2] ?>" id="status"><br>

    </div>
</div>

Try This --

<div class="row">
<div class="col-12">
    <ul>
        <?php foreach ($texts as $row =>$value): ?> 
            <li><h3> <?php echo ucfirst($value) ?></h3></li>
        <?php endforeach ?>
    </ul>
     <label>Status:</label>

     <input type="text" name="status" value="<?php echo ucfirst($texts[2]) ?>" id="status"><br>

</div>

You should use index for your array:

<?php
$texts = [ 
//...
           "status": "Successful", 
           "reference_number": "2950211545", 
           "transaction_date": "01 Feb 2016 13:09:17", 
           "amount": "RM100.00", 
           "from_account": "564155051577 WCAa" 
]
?>

Then:

<input type="text" name="status" value="<?php echo $texts['status'] ?>" id="status">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM