user12615060
user12615060

Reputation:

I am trying to send id through form but submit does not work

I created a category editing page on my site. But that I'm trying to submit the category I want to edit the ID was not sent through form. I'm unable to pull the ID.

edits_cat.php

  <!--Form-->
    <?php
 if(isset($_POST['submit'])){
  $id = $_GET['id'];
     }
          ?>
 <form action="edit_cat.php" method="post">
<label class="my-1 mr-2" for="inlineFormCustomSelectPref">Select Categorie</label>
 <select name="edit" style="width:auto;" class="custom-select my-1 mr-sm-1" id="inlineFormCustomSelectPref">
                                                                            
<?php
 while ($result = $query->fetch_assoc()){
 echo "<option name='id' value='{$result['id']}'>{$result['cat']}</option>";
 }
 ?>
 </select>
<button name="submit" class="btn btn-primary" type="submit">Select a category</button>
</form>
<!--END Form-->

edit_Cat.php

$id = $_GET['id'];
$query = $mysqli->con->query("SELECT * FROM `categories` where id = '$id'");
$result = $query->fetch_assoc();

Upvotes: 0

Views: 496

Answers (2)

Chhaileng Peng
Chhaileng Peng

Reputation: 2526

You are passing id via URL, so you can access it by $_GET['id']. If you want to access via $_POST['id'], you need to add a hidden input in your form.

<input type="hidden" value="<?php echo $id; ?>" />

Upvotes: 1

Burhan Kashour
Burhan Kashour

Reputation: 677

Replace this

<form action="edit_cat.php?id=<?php echo $id; ?>" method="post">

With this:

<form action="edit_cat.php" method="post">

And then Add Hidden Input Inside your form:

<input type="hidden" name="id" value="<?php echo $id; ?>" />

Upvotes: 1

Related Questions