Undefined index for $_GET

591 views php
3

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!

answered question

You will have to add the same query parameter to action="feedback_form_entry.php?code=...", it isn't passed through automagically.

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'].

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

2 Answers

7

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:

<form method="post" id="contact_form" action="feedback_form_entry.php?code=<?php echo $_GET['code']; ?>">

posted this
10

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.

posted this

Have an answer?

JD

Please login first before posting an answer.