0

I have to edit my question just to make it clear. What I need to do is attach the value of startdate and enddate on this jsonData.php.

If the text field is empty, the defaul values will show, but if not..startdate and endate values should be what was entered by the user.

    <script type="text/javascript">

var startdate = "2012-01-01";
var enddate = "2012-01-06";


var jsonData = $.ajax({
          url: "jsonData.php?startdate="+ startdate +"&enddate="+ enddate,
          dataType:"json",
          async: false
          }).responseText;

MY Text field code:

Start Date: <input type="text" name="startdate" id="startdate"/>
End Date: <input type="text" name="enddate" id="enddate"/>

I tried using this code, but it didnt work:

var startdate = "2012-01-01";
var enddate = "2012-01-06";

if (document.getElementById('startdate').value == ''){

startdate = "2012-01-01";
}
else{
startdate = document.getElementById('startdate').value;
}


if (document.getElementById('enddate').value == ''){
 enddate = "2012-01-06";
}

else{
enddate = document.getElementById('enddate').value;
}

Im getting that error because I have declared the variables on separate script tags. How will i Fix this. enter image description here

UPDATE 2: Whole code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>


 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">

    google.load('visualization', '1', {'packages':['corechart','piechart']});
          </script>




<script type="text/javascript">



  $(document).ready(function() {
    $('.date').datepicker({dateFormat: "yy-mm-dd"});

            var startDate = $('#startdate');
            var endDate = $('#enddate');


            if (startDate.val() == '') {
                startDate.val('2012-01-01');
            }

            if (endDate.val() == '') {
                endDate.val('2012-01-06');
            }

  });





        var pieJsonData = $.ajax({
          url: "overall_calls_forms_pos_pie.php",
          dataType:"json",
          async: false
          }).responseText;

    var pieData = new google.visualization.DataTable(pieJsonData);



      var pieJsonData2 = $.ajax({
          url: "overall_ban_pos_pie_date.php?startdate="+ startdate +"&enddate="+ enddate,
          dataType:"json",
          async: false
          }).responseText;

    var pieData2 = new google.visualization.DataTable(pieJsonData2);

function pieChart() {



pieChartWrapper = new google.visualization.ChartWrapper({
          'chartType': 'PieChart',
          'containerId': 'pie_div',
      'dataTable':pieData,
          'options': {
    chartArea:{left:10,top:40,height:200,width:300},
    width:360, 
    height:260,
    title: "Positive Contacts Percentage", 
    titleTextStyle:{fontSize:14},
    tooltip:{showColorCode:true},
    legend:{textStyle:{fontSize: 12},position:'left'},
    pieSliceTextStyle:{fontSize: 12}

          }
        });

       google.visualization.events.addListener(pieChartWrapper, 'ready', pieReady);

pieChartWrapper.draw();
}

 function pieReady() {

        // Change the pie chart rendering options when clicked.
        document.getElementById('optionsButton').onclick = function() {
          pieChartWrapper.setDataTable(pieData2);
          pieChartWrapper.draw();
        };
        document.getElementById('optionsButton2').onclick = function() {
           pieChartWrapper.setDataTable(pieData);
           pieChartWrapper.draw();
         };

      }



google.setOnLoadCallback(pieChart);


</script>
  </head>
  <body>



Start Date: <input type="text" name="startdate" class="date" id="startdate"/>
End Date: <input type="text" name="enddate" class="date" id="enddate"/>

            <div id="buttons">
  <button style="margin: 2em" id="optionsButton">Get My Data 2</button> 
  <button style="margin: 2em" id="optionsButton2">Get My Current Data</button>      
    </div>
    <div id="pie_div" style="float:left;"></div>     


  </body>
</html>

I USED: to correct my mistake. But the chart doesnt redraw unless the page refreshes.

  $(document).ready(function() {
    $('.date').datepicker({dateFormat: "yy-mm-dd"});


            var startDate =  document.getElementById('startdate').value;

            var endDate = document.getElementById('enddate').value;



            if (startDate == '') {
                startDate = ('2012-01-01');
            }

            if (endDate == '') {
                endDate = ('2012-01-13');
            }
1
  • You could just put the default values as value="2012-01-01" in your html... Commented Mar 23, 2012 at 3:31

1 Answer 1

1

If I understand this correctly, this is what you want probably.

Start Date: <input type="text" name="startdate" id="startdate" class="date" />
End Date: <input type="text" name="enddate" id="enddate" class="date" />

<script>
    $(document).ready(function() {
        $('.date').datepicker({dateFormat: 'yy-mm-dd'});

            var startDate = $('#startdate');
            var endDate = $('#enddate');

            if (startDate.val() == '') {
                startDate.val('2012-01-01');
            }

            if (endDate.val() == '') {
                endDate.val('2012-01-06');
            }
    });
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for you help, but unfotunately it didnt work. I edited my post just to make myself clear on what I want to happen. Thanks!
just remove the first </script> and the second <script text="text/javascript"> but make sure at the very end you have </script> and at the very top <script text="text/javascript">
still getting error "stardate is undefined. I'll post my whole code here for update.
take the first }); and put it all the way at the bottom within the script tags
Thanks, I finally got it to work, but I have problems with the google visualzation..im guessing you dont have knowledge about it.\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.