-1

I am getting the error: Undefined index: data when running the code below

Here are the key parts of the code including sections that work fine:

I create a form to select dates - this all works fine

<div class="dates">
<form method="POST" action="">
<label for="StartDate">Start Date:</span></label>
<input type="datetime-local" id="StartDate" name="StartDate" value="<?php 
if(isset($_POST['StartDate'])){echo $_POST['StartDate'];}?>"/>
<label for="EndDate">End Date:</span></label>
<input type="datetime-local" id="EndDate" name="EndDate" value="<?php if(isset($_POST['EndDate'])) 
{echo $_POST['EndDate'];}?>"/>
<input type="submit">
</form>
</Body>
</HTML>
</div>

I then run an sql query and add a checkbox to each line of data. I use lead_id as the value. This all displays fine and the value is being assigned correctly:

$result = mysqli_query($link,$query);

while ($row = mysqli_fetch_assoc($result))
{

?>
<form method="POST"> 
<?php

echo '<tr><td>'.$row['lead_id'].'</td>';
echo '<td>'.$row['status'].'</td>';
echo '<td>'.$row['campaign_id'].'</td>';
echo '<td>'.$row['call_date'].'</td>';
echo '<td>'.$row['user'].'</td>';
echo '<td>'.$row['status_name'].'</td>';

?>

<td><input type="checkbox" value='<?php echo $row['lead_id']?>' name="data[]"/></td></tr>";
</form> 
<?php

}

I then have a submit button which is used to trigger an email - that all works aprt from it needs to contain the lead ids from the checked boxes and it doesnt due to this error

 ?>

<div>
<form method="post"> 
<input type="submit" name="Button1"
value="Send Email"/>   
</form> 
</head>
</div>

The error I get is Undefined Index: data

I am using the below code to attempt to output the data on screen as well as var_dump. All come out NULL as clearly for some reason it is not picking up the POST.

if(isset($_POST['data']) && !empty($_POST['data']))   
foreach($_POST['data'] as $name) echo $name;

Id be grateful if anyone can see my error.

DRH09
  • 5
  • 3
  • Does this answer your question? ["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) – Nico Haase Jul 08 '20 at 09:01
  • You are creating invalid HTML to begin with. You can not nest a form into a table in such a place - it either has to go around the complete table, or be contained in a single table cell. – CBroe Jul 08 '20 at 09:01

1 Answers1

0

The issue is:

<form method="POST"> 
<?php
...
<td><input type="checkbox" value='<?php echo $row['lead_id']?>' name="data[]"/></td></tr>";
</form>

These checkboxes are in a different <form> tag and your submit button is in a different <form> tag which is below these checkboxes:

<div>
<form method="post"> 
<input type="submit" name="Button1"
value="Send Email"/>   
</form> 
</head>
</div>

That's why on form submit you are not getting those checkboxes. To resolve this, move checkboxes in the same <form> tag where submit button is.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59