DV_2900
DV_2900

Reputation: 23

passing value to hidden input value before submit

I am trying to run a form that stores an Id in a hidden input tag so that I can retrieve it in the next page using php. For some reason I can't retrieve the value using the php file. Echoing orderId.value and order number are working fine.

main_page.php

<script>
function EditValues(orderNumber) {
    var orderId = document.getElementById("orderId");
    orderId.value = orderNumber;
    document.forms["form1"].submit();
}
</script>

<body>
    <form action="edit-form.php" id="form1">
        <div class="container">

            <!--use the hidden input variable to save the order number clicked -->
            <input id="orderId" type="hidden" name="orderId"/>
<?php
    require("config.php");

    $con = new mysqli(DB_Host, DB_User, DB_Password, DB_Name);
    if ($con->connect_error) {
        die("Connection failed");
    }

    echo '<table id="tblOrders" name ="OrderTable" style="width: 100%">
            <tr>
                <th>Sno</th>
                <th>Order Number</th>
            </tr>';

    $displayTableDataQuery = "SELECT id, orderNumber, customerName, deliveryDate FROM orderTable";

    if ($tableData = $con-> query($displayTableDataQuery)) {
        while($row = $tableData-> fetch_assoc()) {
            $id = $row['id'];
            $orderNumber = $row["orderNumber"];
            echo '<tr >
                <td>'.$id.'</td>
                <td id = "orderNumber">'.$orderNumber.'</td> 
                <td><input type = "button" id ="editButton'.$id.'" value = "Edit" onclick = "EditValues('.$orderNumber.');"/> </td>
                <td><input type = "button" id = "printInvoice'.$id.'" value="Print" onclick = "PrintInvoice('.$orderNumber.');" /> </td>
                </tr>';
        }
    } else {
        echo $con->error;
    }

    $tableData->free();
?>
        </div>
    </form>
</body>

In edit-form.php

<?php

$xyzabc = $_POST['orderId'];
echo $xyzabc;

?>

There is nothing echoed for $xyzabc I would prefer if there was some way to do this without jQuery as I'm kind of new to this and haven't really gotten a hang of how everything works together as of now.

Upvotes: 2

Views: 175

Answers (1)

Nipun Tharuksha
Nipun Tharuksha

Reputation: 2567

You can store value directly to the hidden input field.

  <!--use the hidden input variable to save the order number clicked -->
        <input id="orderId" type="hidden" name="orderId" value="<?=$variable_name;?> />

So that when you submit the form

 <?php

$xyzabc = $_POST['orderId'];
echo $xyzabc;

?>

will fetch the data. Or you can pass the hidden value in url. For example

<a href="localhost:8000/edit-form.php?orderId="<?=$variable_name;?>

Then in you form-edit.php

<?php

    $xyzabc = $_GET['orderId'];
    echo $xyzabc;

    ?>

Upvotes: 1

Related Questions