Search code examples
phphtmlarraysvariablespost

how to call value $_POST['variable+array'] from name that contain array in input type


iam a newbie, i have a problem with this code, please help.

<input type="text" id="hargautama<?php echo $row['idharga']; ?>" name="hargautama<?php echo $row['idharga']; ?>">

my problem is on this

if(isset($_POST['updatehargautama'])){
    
    $hargautama = $_POST['????'];

i want to insert this value to database from this input type

<input type="text" id="hargautama<?php echo $row['idharga']; ?>" name="hargautama<?php echo $row['idharga']; ?>">

thanks for the help guys. i owe u all.


Solution

  • Assuming the form is submitted to the same page, you can modify your code like this:

    <?php
    if (isset($_POST['updatehargautama'])) {
        $idharga = $_POST['idharga'];
        $hargautama = $_POST['hargautama'.$idharga];
    
        // Now you have the value from the input field with the name 'hargautama<idharga>'
    }?>
    
    //Your HTML form
    <form method="post" action="">
        <input type="hidden" name="idharga" value="<?php echo $row['idharga']; ?>">
        <input type="text" id="hargautama<?php echo $row['idharga']; ?>" name="hargautama<?php echo $row['idharga']; ?>">
        <input type="submit" name="updatehargautama" value="Update">
    </form>
    

    In this code, I've added a hidden input field named "idharga" to store the value of $row['idharga']. When the form is submitted, it will send both the value from the input field with the name 'hargautama' and the 'idharga' value in the $_POST array. then access the 'idharga' value to construct the correct name of the input field and retrieve the corresponding value.