Undefined index: datetimepicker1

Hi,
here’s my code for modal.php

<?php
    require_once 'dbconnect.php';
?>
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>#</title>
  
  
  <link rel='stylesheet prefetch' href='http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css'>
  <link rel='stylesheet prefetch' href='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2708/bootstrap-datetimepicker.min.css'>

  
  
</head>

<body>

  <!-- Button to trigger modal -->
<a href="#myModal" role="button" class="btn" data-toggle="modal">Launch demo modal</a>
 
<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Modal header</h3>
  </div>
  <div class="modal-body">
    <p>Select the time range and interval that you wish to export: </p>
    
        <table class="table">
        <tbody><tr>
            <td>
                <p><b>Start date &amp; time</b></p>
    <div id="datetimepicker1" class="input-append">
    <input id="datetimepicker1" data-format="MM/dd/yyyy HH:mm:ss PP" type="text" class='dateFilter' name='datetimepicker1' value='<?php if(isset($_POST['datetimepicker1'])) echo $_POST['datetimepicker1']; ?>'></input>
    <span class="add-on">
      <i data-time-icon="icon-time" data-date-icon="icon-calendar">
      </i>
    </span>
  </div>
            </td>
            <td>
                <p><b>End date &amp; time</b></p>
                <div id="datetimepicker2" class="input-append">
    <input id="datetimepicker2" data-format="MM/dd/yyyy HH:mm:ss PP" type="text" class='dateFilter' name='datetimepicker2' value='<?php if(isset($_POST['datetimepicker2'])) echo $_POST['datetimepicker2']; ?>'></input>
    <span class="add-on">
      <i data-time-icon="icon-time" data-date-icon="icon-calendar">
      </i>
    </span>
  </div>
            </td>
            </tr>
            <tr>
                <td>
                <p><b>Interval</b></p>
                <select id="export-interval">
                    <option value="">Select interval</option>
                    <option value="10">10s</option>
                    <option value="30">30s</option>
                    <option value="60">1 min</option>
                    <option value="300">5 mins</option>
                    <option value="600">10 mins</option>
                    <option value="900">15 mins</option>
                    <option value="1800">30 mins</option>
                    <option value="3600">1 hour</option>
                    <option value="21600">6 hour</option>
                    <option value="43200">12 hour</option>
                    <option value="86400">Daily</option>
                    <option value="604800">Weekly</option>
                    <option value="2678400">Monthly</option>
                    <option value="31536000">Annual</option>
                </select>
            </td>
            <td>
                <p><b>Date time format</b></p>
                <div class="checkbox">
                  <label><input type="checkbox" id="export-timeformat" value="" checked="">Excel (d/m/Y H:i:s)</label>
                </div>
                <label>Offset secs (for daily)&nbsp;<input id="export-timezone-offset" type="text" class="input-mini" disabled=""></label>
            </td>
            </tr>
            </tbody></table>
                <div class="alert alert-info">
                <p>Selecting an interval shorter than the feed interval will use the feed interval instead.</p>
                <p>Date time in excel format is in user timezone. Offset can be set if exporting in Unix epoch time format.</p>
                </div>
  <div class="modal-footer">
  </div>
</div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
        <button onclick="Export()" class="btn btn-primary" id="export" export-type="feed" feedid="" name="current">Export</button>
        <script>
        function Export()
        {

                window.open("get_data2.php", '_blank');
            
        }
    </script>
    </div>
    
<script src='ajax/jquery-2.1.3/jquery.min.js'></script>
<script src='js/bootstrap.min.js'></script>
<script type="text/javascript" src="js/moment.js"></script>
<script src='js/bootstrap-datetimepicker.min.js'></script>
<script  src="js/index.js"></script>

</div>
</body>

</html>

and here’s my code for get_data2.php

<?php
$conn = new mysqli("localhost", "root", "", "dbname");
if(!$conn){
    die("Fatal Error: Connection Error!");
}

// get current
$datetimepicker1 = date("MM/dd/yyyy HH:mm:ss PP", strtotime($_POST['datetimepicker1']));
$datetimepicker2 = date("MM/dd/yyyy HH:mm:ss PP", strtotime($_POST['datetimepicker2']));
$query = "SELECT * FROM `current` WHERE `time` BETWEEN '$datetimepicker1' AND '$datetimepicker2' ORDER BY `time` ASC";
if (!$result = mysqli_query($conn, $query)) {
    exit(mysqli_error($conn));
}

$current = array();
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $current[] = $row;
    }
}

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=current.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('Time', 'Current'));

if (count($current) > 0) {
    foreach ($current as $row) {
        fputcsv($output, $row);
    }
}
?>

when i hit button export for downloading my data from database, this is what i get

what is undefined index? how to solve this problem?

thank you in advanced.

well, the error tells you wherethings went wrong. Line 8.

Also this is a PHP error, not a Javascript error.

$datetimepicker1 = date("MM/dd/yyyy HH:mm:ss PP", strtotime($_POST['datetimepicker1']));

i’m going to assume this is Line 8. $_POST did not contain a value with the key ‘datetimepicker1’. This is probably because you have erroneously given the id ‘datetimepicker1’ to the DIV above your input. ID’s can only be used once on a given page.

1 Like

… except ID doesnt correspond to the thing that gets sent through. derp.

Instead, it would seem your problem is… there is no Form tag anywhere in your code… all your Export function does is open a window, it doesnt send any of the form data to the page…

hi m_huntley,
thank you for answering my question.

i’ve removed id=“datetimepicker1” and id=“datetimepicker2” from
and run the file again. i still get the same result.

how do i send data to the get_data2.php?

<form action="get_data2.php" method="post" target="_blank">

EDIT: That was NOT the button i pushed, Discus…

Wrap your inputs in a form. Your submit button should be an actual submit button, or else have the javascript call the .submit() method on your form.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.