0

Im trying to get the taskid variable from the url:

Long story short the database never updated trying to echo $tasked is blank and im not sure why.

I have looked over all of the suggestions and many different websites I do not see what i'm missing

http://domain.com/ubxtask/addnote.php?taskid=163994

<!DOCTYPE html>
<html lang="en">
<head>
<title>Add Note to Task</title>
</head>
<body>

<form action="" method="post">
    <p>
<textarea name="notetoadd" rows="4" cols="50"></textarea>
    </p>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>

<?php
if ( isset( $_POST['submit'] ) ) {

$servername = "localhost";
$username = "dbusr";
$password = "dbpass";
$dbname = "db";

$notetoadd = $_POST['notetoadd'];

if (isset($_GET["taskid"])) {
//$taskid = $_GET['taskid'];
echo $_GET["taskid"];
//echo $taskid;
}

$sql = "INSERT INTO tasknotestbl (tasknum, tasknote)
VALUES ('$taskid', '$notetoadd')";

if ($conn->query($sql) === TRUE) {
    header('Location: http://domain.com/task/tasklist.php');
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}
?>
5
  • The form hasn't the taskid field... Commented Feb 24, 2017 at 15:32
  • And the form has no action set action to action="addnote.php" Commented Feb 24, 2017 at 15:36
  • Ok I tried both neither worked, I am using shellinbox to write the code and sometimes copy and paste inputs 'invisible' characters that I cannot see unless using putty, found that out yesterday after a couple hours of trouble shooting a very simple problem, I will check it later today and if still not working will come back Commented Feb 24, 2017 at 15:41
  • 3
    I hate all the downvotes I get on this site, wish you would tell me why its a bad question Commented Feb 24, 2017 at 15:46
  • Ok figured it out, the reason I was not seeing the 'taskid' was because it was not being set until I hit submit, @Philipp answer would have been the second problem I would have run into, by moving the 'if (isset($_GET["taskid"])) {' out of the 'if ( isset( $_POST['submit'] ) ) {' It works as expected, perhaps while you kind people were downvoting you might have picked up on that. Commented Feb 24, 2017 at 17:10

2 Answers 2

3

You should add the task id to your forms action, or it would be lost, if you submit the form

<form action="addnote.php?taskid=<?php echo $_GET['taskid']; ?>" method="post">
Sign up to request clarification or add additional context in comments.

Comments

0

You can add hidden field to form with taskid and use post method:

<?php   
if (empty($_GET['taskid'])) {
    $taskid = '1';
}else{
    $taskid = (int)$_GET['taskid'];
}

// your code submit code and
if (isset($_POST["taskid"])) {
echo $_POST["taskid"];
}

echo '<form action="" method="post">
    <p><textarea name="notetoadd" rows="4" cols="50"></textarea></p>
    <input type="hidden" name="taskid" value="'.$taskid.'" placeholder="taskID">    
    <input type="submit" value="Submit" name="submit">
</form>';
?>

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.