2

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.

1
  • if you need to change hidden field value before the submit, you can not use PHP, cause its a server side language. You must use javascript. Commented Jul 8, 2019 at 11:38

1 Answer 1

1

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;

    ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.