I've seen this question come up a few times. One of the first things you need to consider is how is the user going to interact with this page.
- Are they going to access it via links? http://example.com/index.php?user=1
- Or are they going to submit a form via post? http://example.com/index.php
This distinction is important because it changes the way you handle their request.
Let's look at each one:
- Via a link: http://example.com/index.php?user=1
This will issue a get request to your server so you need to handle it via $_GET
.
if (isset($_GET['user'])) {
$userId = $_GET['user'];
// ... query your DB and output result
}
- Via a form using post: http://example.com/index.php (body with contain "user=1").
This will issue a post request to your server so you need to handle it via $_POST
if (isset($_POST['user'])) {
$userId = $_POST['user'];
// ... query your DB and output result
}
On a side note, it is important to know that an HTML <form>
can submit either a post or get request. You can control this via it's method attribute. If you don't specify the method attribute, then it defaults to get.
<form method="post">
...
</form>
<form method="get">
...
</form>
So, to answer your question. You're likely getting an undefined error because you're trying to access the post array using the key mailuid ($_POST['mailuid']
) but that does not exist. It doesn't exist because:
- you're receiving a get request and the post is empty OR
- you are receiving a post request but it doesn't contain the key mailuid
To debug, go back to the basics - use $_GET
Change your code to use $_GET['mailuid']
and then make sure you access your page with the corresponding query string - ...?mailuid=1
.
Finally, turn on error reporting - https://stackoverflow.com/a/5438125/296555. You should always do this and correct all errors and warnings.