1

I've got a database already populated. I also have a function (in JavaScript) that "catches" the id of that element for which a button was pressed, something like this:

   Name of subject   |   Actions
-----------------------------------------
      Chemistry      | Edit / Delete
       Algebra       | Edit / Delete

So, if I pressed "Delete", a javascript function gets the the id of that subject, and sends a request with AJAX, in this way:

function confirmationDeleteSubject(boton){

  var materiaId = parseInt(boton.value);
  console.log(materiaId);

  $.ajax({
    url: 'http://localhost/path/showSubject.php',
    type: "POST",
    data: {"materiaId": materiaId},
    dataType: "html",
    beforeSend: function() {    
      console.log("Processing...");
    },
    success: function(data) {
      if( data == "OK") {
        //location.href = "http://localhost/path/main.php";
        return;
      }
      if( data == "ERROR") {
        alert("error, please try again");
        return;
      }
      alert("Server message: " + data);
    }
  });
}

There is then a php file that deals with getting the data for that specific id, and attempts to show it in a modal:

<?php

  include("conexionDatos.php");

  // Inicialization
  $materiaId = "";

  // This receives the data from the JavaScript function
  $materiaId = $_POST['materiaId'];

  // Query to get the data for that id
  $SQL = "SELECT m.id, m.name, m.description, m.hours, c.name AS carreer_name FROM subjects m JOIN carreers c ON (m.carreer_id = c.id) WHERE m.id = '$materiaId'";
  $result = $CONN->query($SQL);

  // This part "inserts" into the table in the modal
  if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()){
      echo "<tr>"; 
      echo "<td>{$row['name']}</td>";
      echo "<td>{$row['description']}</td>";
      echo "<td>{$row['hours']}</td>";
      echo "<td>{$row['carreer_name']}</td>";  
      echo "</tr>";
    }
  } else {
    echo "<tr><td>No data found<td></tr>";
  }

  $CONN->close();

?>

This code, when pressed actually brings the data for the id selected, but not in the modal. It brings all the data into an alert by localhost, following the code in the AJAX call, this part:

alert("Server message: " + data);

After that alert, a modal pop up, but the table is empty, and it reads No data found.

It has a warning that says:

Notice: Undefined index: materiaId in C:\blablabla\muestroMateria.php on line 12

Which I don't fully understand why, because the data pops up in the alert, but it's like the php file receives no id and so the modals reads "no data found"

EDIT: Even if my questions deals qith an Undefined Index warning, to me it goes further than that, somewhere between the AJAX code and the PHP file. I still can't realize what it is.

xragdollqueen
  • 113
  • 4
  • 14
  • check if the materialId is not null. – BugHunter Mar 20 '17 at 19:12
  • @Sachin PATIL Hi! yes, I've used var_dump(); to see its value, and returns NULL. I don't understand why the data displays well in the alert (telling me that the SELECT query works fine), but it doesn't populate the modal :/ – xragdollqueen Mar 20 '17 at 20:04
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – miken32 Mar 20 '17 at 20:04

1 Answers1

0

This line is problematic. Your POST array doesnt have the designated key.

$materiaId = $_POST['materiaId']

You should make sure that you are sending the right data (browser inspector / Network tab), and also protect from situations when, as has been mentioned, your variable is null.

$materiaId = isset($_POST['materiaId']) ? $_POST['materiaId'] : '';

or

$materiaId = array_key_exists('materiaId', $_POST) ? $_POST['materiaId'] : '';

You also need to check what data the PHP script is receiving (json variable, or normal array of sent variables - or nothing for some reason) - you can use

echo var_export($_POST, true); as your PHP script response, to debug what the script is receiving (and in what format).

tonino.j
  • 3,837
  • 28
  • 27
  • Hi! I see, I've tried that and keep returning Undefined Index or NULL (with only isset it returns bool false). I've been reviewing it and the query in the php file works ok, and also the javascript alone (using an alert to show the value of the variable) The AJAX code seems to be ok too, so I don't know, because the value is "caught", and it's not null, but the php can't receive it :/ – xragdollqueen Mar 20 '17 at 20:30
  • I edited my answer, with another test you can do to debug – tonino.j Mar 20 '17 at 21:02