0

I have an options form setup like this

When I press the submit button, I expect to call a function which draws a table from a SQL query

Table example here

Trying to call said function like so:

<?php
$id = $_POST['id'];
if($id > 0) {
    $sql2="SELECT * FROM data WHERE Id = '$id';";
    $sql_res = mysqli_query($d,$sql2) or die("<h1>".mysqli_error()."</h1>");
    tabula($sql_res);
}
?>

That results in an error - Undefined index: id

on line

$id = $_POST['id'];

Please help :(

1 Answer 1

1

From your screenshot, I can see that you have not specified a method attribute for the form tag.

The default method for form is GET.

Hence when the form is submitted, you will be getting the value of id in $_GET['id']

And since there was no $_POST['id'], it throwed you the undefined index id error.

Based on this, you can.

  1. Change the method of the form to POST.

Example <form method="POST">

  1. Fetch the value of the id from the $_GET array.

Example $id = $_GET['id'];

You can use $_REQUEST['id'] aswell.

$_REQUEST, by default, contains the contents of $_GET, $_POST and $_COOKIE.

Hope this helps.

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.