-1

I'm trying to find my error in the following piece of code. First the user will have to fill out the following form. Within this php, there is a GET variable named 'code' (example: feedback_form.php?code=ABC):

<?php 
  session_start();
  include ("feedback_form_entry.php");
  if (!isset($_SESSION["feedback_ok"])) {
    header('Location: login_error.php');
    exit;
  }  
?>

<!doctype html>
<html>
<head>

</head>

<body>

    <form method="post" id="contact_form" action="feedback_form_entry.php">
      <textarea type="text" name="nps_question_2" rows="4" spellcheck="false" placeholder="What was the reason you gave us this rating?"></textarea>
      <textarea type="text" name="nps_question_3" rows="4" spellcheck="false" placeholder="What can we do to improve your experience?"></textarea>
      <input class="button" type="submit" name ="send_feedback" value="SEND FEEDBACK">
    </form>

</body>
</html>

Then the following script should follow (feedback_form_entry.php):

<?php
  $nps = "";
  $nps_question_2 = "";
  $nps_question_3 = "";

function test_input($data) {
    return htmlspecialchars(stripslashes(trim($data)));
}

if (isset($_POST["send_feedback"])) {

$nps_question_2 = test_input($_POST["nps_question_2"]);
include("db_connect.php");

$nps_question_2_update = $con->prepare("UPDATE codes SET `nps_question_2`=? WHERE `code`=" . $_GET["code"]);
$nps_question_2_update->bind_param("s", $nps_question_2);
$nps_question_2_update->execute();
$nps_question_2_update->close();

header('Location: feedback_form_success.php');
$con->close();
}

?>

Now I will get the following error message:

Notice: Undefined index: code in C:\xampp\htdocs\tours\feedback_form_entry.php on line 15

and I can't really find out, where the problem is. I tried defining the GET variable as $code, but it just won't work.

Thanks in advance for any suggestion!

sebjel
  • 13
  • 7
  • 1
    You will have to add the same query parameter to `action="feedback_form_entry.php?code=..."`, it isn't passed through automagically. – deceze Nov 08 '18 at 04:53
  • If you put a field `code` into your form, it would be `$_POST['code']`. If you add it to the querystring within the action of your form, it would be `$_GET['code']`. – Ultimater Nov 08 '18 at 04:53
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Mukyuu Nov 08 '18 at 04:53
  • you should pass the variable to the current script via the URL parameters. Then you can access it in the `$_GET` global variable. I would also recommend checking if it is set before consuming it ie: `$code = isset($_GET['CODE']) ? $_GET['code'] : null` – Kalu Nov 08 '18 at 04:58

2 Answers2

0

The problem is that you don't propagate the $code variable to when the button submit is clicked. To fix it, just add ?code= to your action form:

<?php $code = isset($_GET['code']) ? $_GET['code'] : ""; ?>
<form method="post" id="contact_form" action="feedback_form_entry.php?code=<?php echo $code; ?>">
ariefbayu
  • 21,849
  • 12
  • 71
  • 92
0

try

$_GET['code'] = (isset($_GET['code']) ? $_GET['code'] : 'default value');

this checks whether the $_GET['code'] is set or not, if not, it will set a default value.

Karma Blackshaw
  • 880
  • 8
  • 20