Joe Morgan
Joe Morgan

Reputation: 175

PHP GET not working

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();
}
?>

Upvotes: 0

Views: 3529

Answers (2)

user7601056
user7601056

Reputation:

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>';
?>

Upvotes: 0

Philipp
Philipp

Reputation: 15629

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">

Upvotes: 3

Related Questions