Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by mano on Jul 03, 2009 22:28
open dhtmlx forum
Connecting Serverside code with dhtmlxscheduler

i want to call a servlet by scheduler.attachEvent("onClick", function (event_id, native_event_object){});
but by ajax code not executed or called.Here i m sendind my code.


function init() {

scheduler.config.xml_date="%Y-%m-%d %H:%i";        
            scheduler.config.date_step = "5"        
            scheduler.init('scheduler_here');
            scheduler.load("http://192.168.1.51:8990/CalenderApp/addapointments");    
scheduler.addEvent("16-06-2009","16-06-2009","Some","some id",{custom_data:"some data"});
scheduler.attachEvent("onClick", function (event_id, native_event_object){

var enddate = scheduler.getEvent(event_id).end_date;

var edate=enddate+"";
var endDateYear=enddate.getFullYear()
var endDateMonth=edate.substring(4,7);
var endDateFinal=edate.substring(8,10);
endDateFinal=RTrim(endDateFinal);
var hr=enddate.getHours();
var mm=enddate.getMinutes();
var sec=enddate.getSeconds();

var endDateTime=hr+":"+mm+":"+sec;
var finalEndDate=endDateFinal+"-"+endDateMonth+"-"+endDateYear+" "+endDateTime;

var startdate = scheduler.getEvent(event_id).start_date;

var sdate=startdate+"";
var startDateYear=startdate.getFullYear()
var startDateMonth=sdate.substring(4,7);
var startDateFinal=sdate.substring(8,10);
startDateFinal=RTrim(startDateFinal);
var startDateTime=sdate.substring(10,19);
var hr1=startdate.getHours();
var mm1=startdate.getMinutes();
var sec1=startdate.getSeconds();
var startDateTime=hr1+":"+mm1+":"+sec1;

var finalStartDate=startDateFinal+"-"+startDateMonth+"-"+startDateYear+" "+startDateTime;

var text=scheduler.getEvent(event_id).text;
alert(endDateYear);
alert(endDateMonth);
alert(endDateFinal);
alert(endDateTime);
alert(finalEndDate);
alert(finalEndDate.length);

alert(startDateYear);
alert(startDateMonth);
alert(startDateFinal);
alert(startDateTime);
alert(finalStartDate);
alert(finalStartDate.length);
alert(edate);
var url="http://192.168.1.51:8990/CalenderApp/saveappointmentdata";
var param = "startDate="+finalStartDate+"&endDate="+finalEndDate+"&text="+text+"&eventId="+event_id+"";
document.getElementById("txt1").value=param;
alert(url+"?"+param);
loader=dhtmlxAjax.postSync(url,param);


});

i m linked a js file i.e
<script src="http://appserver:7778/IMAGES/js/dhtmlx.js" type="text/javascript"></script>which i m always using to call ajax and it will working well.But here it is not working.

My server side code is like that..

public class SaveAppointmentData extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

DataSource ds = null;
public void init(ServletConfig config) throws ServletException {
try {
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("jdbc/HRMSDS");
} catch (Exception exp) {
exp.printStackTrace();
}
}

public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{response.setContentType(CONTENT_TYPE);
Connection connnection = null;
PreparedStatement pstatement = null;

response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String evId = request.getParameter("eventId");
String sDate = request.getParameter("startDate");
String eDate = request.getParameter("endDate");
String textNote = request.getParameter("text");
String detailsNote ="";
String statusNote ="A";
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date startDate=null;
try {
startDate = (Date)df.parse(sDate);
} catch (ParseException e) {
e.printStackTrace();
}
Date endDate=null;
try {
endDate = (Date)df.parse(eDate);
} catch (ParseException e) {
e.printStackTrace();
}
try {
connnection = ds.getConnection();

pstatement= connnection.prepareStatement("INSERT INTO EMP_APPOINTMENTS (EVENT_ID,START_DATE,END_DATE,TEXT,DETAILS,STATUS) VALUES (?,?,?,?,?,?)");
pstatement.setString(1,evId);
pstatement.setDate(2, startDate);
pstatement.setDate(3,endDate);
pstatement.setString(4,textNote);

pstatement.setString(5,detailsNote);
pstatement.setString(6,statusNote);
pstatement.executeUpdate();

} catch (SQLException sqe) {
sqe.printStackTrace();
} finally {
try {

if (pstatement != null) {
pstatement.close();
pstatement = null;
}
if (connnection != null) {
connnection.close();
connnection = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}

out = response.getWriter();
out.flush();
out.close();
}
}


Answer posted on Jul 06, 2009 01:53
>>which i m always using to call ajax 
It is not necessary. dhtmlxscheduler.js already contains code, which need for ajax calls. ( but will not cause a problem in any case )

The code which you are using looks correct and must not cause any problems
If some of date may contain special chars it will be good to escape it 
var param = "startDate="+finalStartDate+"&endDate="+finalEndDate+"&text="+encodeUriComponent(text)+"&eventId="+event_id+""; 


by the way - code, which you are using for date formating a bit complex, it can be changed to the 
scheduler.attachEvent("onClick", function (event_id, native_event_object){ 
           var format = scheduler.date.date_to_str("%Y-m-d H:i:s");
           var enddate = format(scheduler.getEvent(event_id).end_date);
           var startdate = format(scheduler.getEvent(event_id).end_date);
          ... ajax call code here ...
});


Attachments (1)