Search code examples
phphtmlmysqlhidden-field

How to POST data to query in Form without any hidden-fields by PHP?


My php query code

<?php 
  if (isset($_POST['btn_add'])) 
      {
          $query_insert = "INSERT INTO Calc_Tbl(id_Customer,Flname_Customer, )
          VALUES (N'$_POST[id_Hidden]', N'$_POST[flname_Hidden]' )";
          mysqli_query($db, $query_insert);
      }
?>

And my html form code to send data to above query

<form method="post" action="" >
    <input type="hidden" name="id_Hidden">
    <input type="hidden" name="flname_Hidden">
    <button type="submit" class="btn btn-danger" name="btn_add"></button>
</form>

Now you know the hidden fields can edit in Inspect Source in Web browsers by every body, How Can I send data in form to query in same page without any input fields as hiddens? How can use variables instead of input hidden fields?

Thank you


Solution

  • Put your variable data into $_SESSION global variables.
    For that first thing to do is to start your sessions like this

    <?php
    session_start();
    //now you have created $id and $flname somewhere in your code
    //which you want to submit
    $_SESSION['id']=$id;
    $_SESSION['flname']=$flname;
    ?>
    

    Rest of your form HTML code will remain as it is

    The PHP program where your form is posted to will begin with

    <?php
    session_start();
    $id=$_SESSION['id'];
    $flname=$_SESSION['flname'];
    

    If any other field were submitted, they will be inside $_POST, naturally
    Until $_SESSION is destroyed using session_destroy;, your variables will be found in any other program you use subsequently.