views:

775

answers:

2

Need help (or an example) as I don't seems to be able to invoke jquery datepick or nicEdit when using XMLHttpRequest.

My code comes with 4 php files:

Main page: main.php
++++++++++++++++++
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
<html xmlns="http://www.w3.org/1999/xhtml&quot;&gt;
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Education Centre</title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="mainjs.js"></script>
<script src="http://js.nicedit.com/nicEdit-latest.js&quot; type='text/javascript'></script>
<script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>

</head>
<body onLoad="init_table();">
<div id="page">
<form name='form1'>
<p>&nbsp;</p>
<div id="content">
<div id="showTable"> </div>
</div>
</form>
</div>
<!-- end page -->
</body>
</html>


mainjs.js
+++++++++
function init_table() {
requestInfo('showMain.php?mode=list&prev_cid=1','showTable','');
}

 

script.js [copied and tested to be ok with other pages]
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
function getHTTPObject() {
var xmlhttp;

if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (!xmlhttp){
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}

}
return xmlhttp;
}

var http = getHTTPObject(); // We create the HTTP Object

function requestInfo(url,id,redirectPage) {
var temp=new Array();
http.open("GET", url, true);
http.onreadystatechange = function() {
if (http.readyState == 4) {
if(http.status==200) {
var results=http.responseText;
if(redirectPage=="" || results!="1") {
var temp=id.split("~"); // To display on multiple div
var r=results.split("~"); // To display multiple data into the div
if(temp.length>1) {
for(i=0;i<temp.length;i++) {
document.getElementById(temp[i]).innerHTML=r[i];
}
} else {
document.getElementById(id).innerHTML = results;
}

} else {
window.location.href=redirectPage;
}
}
}
};
http.send(null);
}


showMain.php
++++++++++++
<?php
header('Content-Type: text/xml');

//include("config.php");
//include("mysql.lib.php");
//$obj=new connect;

$netwkinfo = "Some information pulled from database";
echo "<table>";
echo "<tr><td>My Data</td> <td><textarea id='netwkinfo' cols='75' rows='5' >" . $netwkinfo . "</textarea></td></tr>";
echo "</tr>";
echo "</table>";
?>

showMain.php is a file that extract data from a database to be displayed/edited etc... but for simplicity i assign a value to $netwkinfo.

What I will like to see is nicEdit feature (panel) shown at the editbox. Unfortunately I am not seeing it, just a plain edit box appears. What could be wrong and how should i correct it for it to work.

Similarly IF I used jquery datepicker on a textbox in the showmain.php, i am not seeing it work too. I think its the same concept but just couldn't figure out how to get them working.

Appreciate any help here. Thanks.

A: 

Since you already have jQuery on the page why not use its AJAX functionality Your script files will be a lot easier to read.

mainjs.js

function init_table() {
    requestInfo('showMain.php?mode=list&prev_cid=1','showTable','');
}

script.js

    function requestInfo(url,id,redirectPage) { 
    $.ajax({  type: "POST",
      url: url,
      data: id,
      success: function(msg){
      var temp = id.split('~');
      var r= msg.split("~")
      for (i=0;i<temp.length;i+=1){
         $(temp[i]).val(r[i]);
      }

      $(document).location.href = redirectPage;
   });
AutomatedTester
Thanks for the prompt reply, the modified scripts.js doesnt work. Also believe to have 2 } missing for success and function.
A: 

Found the hint to the answer here.

have not successfully implement nicedit yet but datepicker for jquery was resolved.

for datepicker I used:

<script type="text/javascript">
$(function(){
$('.inputDate').live('click', function() {
$(this).datepicker({
starts: 1,
position: 'right',
onChange: function(formated, dates){
$('#inputDate').val(formated);
$('#inputDate').DatePickerHide();
}
});});
});

</script>