I am working in PHP I have created a page here I have 2 combo box when I select first combo box item the second one is filled according the first combobox using JavaScript the problem I am facing is where I am trying to save the data of second combo box in database I found null value. transition_party_id is the combobox that fill dynamically here I code
function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getparty(category) {
var strURL="../admin/get_partyname.php?category="+category;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('partydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
req.open("GET", strURL, true);
req.send(null);
You send 'null' post data by "GET" method.
Or you waiting data in get?
Related
I want my program to work like this: When i press a hyperlink in html, i want it to load new content into the "content" section. I have an example code that does this. But I am trying to insert a dygraph inside and I find that the example only passes string. The graph that i was trying to insert did not appear in the content, only basic html appeared (button, background color, etc)
function loadPage(page)
{
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", page, true);
xmlhttp.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
xmlhttp.send();
xmlhttp.onreadystatechange = function ()
{
if((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
{
document.getElementById("content").innerHTML = xmlhttp.responseText;
}
}
}
If i'm not wrong, the loadPage function needs to be edited. But i'm not sure how. Hope you guys can answer me in a layman's term.
You need to pass your newly retrieved content to dygraph after you fetch.
function loadPage(page) {
if (window.XMLHttpRequest) {
var xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET", page, true);
xmlhttp.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
xmlhttp.send();
xmlhttp.onreadystatechange = function () {
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
var g = new Dygraph('content', xmlhttp.responseText, [rest of your parameters]);
}
}
}
}
I am checking some date whether available or not from DB using Ajax using below my code,
var url = "ValidateDateFromDB.jsp?cdate="+selecteddate.value;
if (window.XMLHttpRequest)
req = new XMLHttpRequest();
else if (window.ActiveXObject)
req = new ActiveXObject("Microsoft.XMLHTTP");
req.open("GET", url, true);
req.onreadystatechange =callback;
req.send(null);
function callback()
{
if (req.readyState == 4)
{
if (req.status == 200)
{
var message = req.responseText;
alert(message);
if(message.startsWith("Available"))
{
alert("Availabe");
}
else
{
alert("Not Availabe");
}
}
}
}
I am getting output like below image,
It is working fine in firefox but not working in chrome.I checked out that startsWith works only chrome above 40 version.So to fix this i used == operation like below,
if(message=="Available")
{
alert("Availabe");
}
else
{
alert("Not Availabe");
}
But this code always goes to else part that i know why.
someone tell me how to fix this?
Here is the scenario:
I have a text box where I can enter a text for "Status". Beside it is a label that should contain the latest "Status". After entering a new status, a button of type button is invoked and calls 2 things(a pop-up cor credentials and a function for XMLHttpRequest).
function a(url){
try {
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if (req != null) {
req.open("GET", url, true);
req.onreadystatechange = getData;
req.send();
} else {
alert("fail");
}
} catch (e) {
alert("Error");
}
}
function getData(){
if(req.readyState == 4){
...some code
}
}
After entering credentiols and click on ok, the latest status should be updated. This is working fine in firefox and chrome. Also the only time the status updates on IE is when the browser was closed then opened.
I have a function in JavaScript:
function login() {
user=document.getElementById("user_id").value;
pass=document.getElementById("password").value;
params="user="+user+"&pass="+pass;
url="check_login.php";
if(window.XMLHttpRequest) {
var http=new XMLHttpRequest();
} else {
var http=new ActiveXObject("Microsoft.XMLHTTP");
}
var http=new XMLHttpRequest();
http.onreadystatechange=function(){
if(http.status==200 && http.readyState==4) {
text=http.responseText;
alert(text.status);
}
}
http.open("POST",url,true);
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http.send(params);
return false;
}
And the server side returns the json data like this:
$return=array("status"=>"true","user"=>$user,"fname"=>$fname,"middle_name"=>$middle_name,"lname"=>$lname);
$return=json_encode($return);
echo $return;
Now the alert should give the alert true but it doesn't work, I hope I am able to make you clear
Use JSON.parse(text) before your alert(text.status) for encode string in JSON object
http.onreadystatechange=function(){
if(http.status==200 && http.readyState==4) {
text=JSON.parse(http.responseText);
alert(text.status);
}
}
One of my client had a form developed in component Mosets Tree. In the form when you select a main category it automatically displays the subcategories. Now the issue is; I had to hide some code to stop displaying a few things, after that the java script which was displaying subcategories after we select the main category is not working in IE.
code:
var xmlhttp;
function stateChanged(){
if (xmlhttp.readyState==4) {
document.getElementById("subCatId").innerHTML = xmlhttp.responseText;
}
}
function fnGetSubCategory() {
xmlhttp = GetXmlHttpObject();
var new_cat_id = document.getElementById("new_cat_id").value;
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return true;
}
var url="ps.php?cat_id="+new_cat_id;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function GetXmlHttpObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
It is working fine in all other browsers.
Thanks in advance.
Try creating the object xmlHttp with this code:
function createXmlHttpRequestObject(){
var xmlHttp;
try{
xmlHttp = new XMLHttpRequest();
}
catch (e) {
// If its IE 6 or other version before
var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
// We try all versions
for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++){
try {
//Try creating xmlHttp object
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch (e){
xmlHttp = false;
}
}
}
// If object doesn't exist sends error
if (!xmlHttp){
alert("Error creating XMLHttpRequest object");
}
else{
return xmlHttp;
}
}