[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to apply switch case in ajax?

Posted on 2009-04-15
11
Medium Priority
?
268 Views
Last Modified: 2012-05-06
i want to apply the switch case in ajax in the very simple application. could u illustrate any example for this?

means when we open the connection and when we send request to the server with ajax, i want that in a single function, there should be a switch case in which particular statement works according to the id coming from php page.
0
Comment
Question by:designersx
11 Comments
 
LVL 3

Expert Comment

by:kurijov
ID: 24154990
You talking about this?

switch (response) {
	case 1:
		doSomething();
		break;
		
	case 2:
		doAnotherThing()
		
	default:
		doNothing();
}

Open in new window

0
 

Author Comment

by:designersx
ID: 24155049
thanks for this

Sir can i apply switch case inside the function, please help me out


below in a php page i am calling function process with its id.
now in another page this function is being defined

now inside the process function, i want to apply switch case , how can i do this?

i want to execute first if loop when i write something in first text box event    and
second if loop when i write something in second text box event.

Company name: &nbsp;&nbsp;<input type="text" id="myName" name="myName" onKeyUp="process(myname)"/><br><br>
Company message: <input type="text" id="myName1" name="myName1" onKeyUp="process(myname1)"/>
 
 
another js file
 
function process(id)
{
  
  // id is myname
 
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    name = document.getElementById("myName").value;
    xmlHttp.open("GET", "quickstart.php?name=" + name, true);  
    xmlHttp.onreadystatechange = handleServerResponse;
    xmlHttp.send(null);
  }
  
  // id is myname1
 
  if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
	name = document.getElementById("myName1").value;
    xmlHttp.open("GET", "quickstart1.php?name=" + name, true);  
    xmlHttp.onreadystatechange = handleServerResponse1;
    xmlHttp.send(null);  
  }
  
}

Open in new window

0
 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24155053
Unfortunately you're description is a little vague.  If you know how to do AJAX at all it would seem obvious how to do what kurijov just suggested.  Is that in fact what you meant, and if so what issue are you running into?  If not then maybe you could explain what you're trying to accomplish so that we would better understand.  Or do you just need help with how to make an AJAX call in the first place?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 24155086
Try like this:
Company name: &nbsp;&nbsp;<input type="text" id="myName" name="myName" onKeyUp="process(myname)"/><br><br>
Company message: <input type="text" id="myName1" name="myName1" onKeyUp="process(myname1)"/>
 
 
another js file
 
function process(id)
{
  
  // id is myname
switch(id){
case "mynmae":
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    name = document.getElementById("myName").value;
    xmlHttp.open("GET", "quickstart.php?name=" + name, true);  
    xmlHttp.onreadystatechange = handleServerResponse;
    xmlHttp.send(null);
  }
  
case "myname1":
  // id is myname1
 
  if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
	name = document.getElementById("myName1").value;
    xmlHttp.open("GET", "quickstart1.php?name=" + name, true);  
    xmlHttp.onreadystatechange = handleServerResponse1;
    xmlHttp.send(null);  
  }
 
}
}

Open in new window

0
 
LVL 3

Expert Comment

by:kurijov
ID: 24155091
This is how it should be

function process(id)
{
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
  		name = document.getElementById(id).value;
	    xmlHttp.open("GET", "quickstart.php?name=" + name, true);
	    switch (id) {
	    	case 'myName':
	    		xmlHttp.onreadystatechange = handleServerResponse;
	    		break;
	    		
	    	case 'myName1':
	    		xmlHttp.onreadystatechange = handleServerResponse1;
	    		break;
	    }
	    
	    xmlHttp.send(null);
  }
}

Open in new window

0
 

Author Comment

by:designersx
ID: 24155105
sir i simply want to apply switch case inside the functions. ajax is of no issue here please. if u see above u will notice that i am calling the same function on onKeyUp event of both text boxes and in another page this will be stored in id as argument.

ok can u please tell me how should i do this?





name:<input type="text" id="myName" onKeyUp="process(myname)"/> 
message:<input type="text" id="myName1" onKeyUp="process(myname1)"/>
 
function process(id)
{
// here i want to apply switch case // output should be 1 if i write something in text box 1 and 2 if i write something in text box2
 
echo $a=1;
 
echo $a=2;
 
}

Open in new window

0
 

Author Comment

by:designersx
ID: 24155117
sir thanks, i am trying this i will let u know about this in a moment.
0
 

Author Comment

by:designersx
ID: 24155163
ok sir the code u  have is really cool.thanks for that

function process(id)

{ alert(id) // here id id alerted properly
 
name = document.getElementById(here i want to bring the id, can i do this  ).value;

i tried with        document.getElementById(this.id) but not clicked

yogesh
           

0
 
LVL 3

Accepted Solution

by:
kurijov earned 600 total points
ID: 24155175

:::wrong
 
name:<input type="text" id="myName" onKeyUp="process(myname)"/> 
message:<input type="text" id="myName1" onKeyUp="process(myname1)"/>
 
:::nice
name:<input type="text" id="myName" onKeyUp="process('myName')"/> 
message:<input type="text" id="myName1" onKeyUp="process('myName1')"/>

Open in new window

0
 

Author Comment

by:designersx
ID: 24155210
i have done this with this id gets alerted .ok

relating to the kurijov answer, i would say the problem is in document.getElementById argument.

function process(id)

{ alert(id) // here id id alerted properly
 
name = document.getElementById(here i want to bring the id, can i do this  ).value;

i tried with        document.getElementById(this.id) but not clicked
0
 

Author Comment

by:designersx
ID: 24165433
any response from ur side?? please tell me if u have got some problem in understanding this?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.NET AJAX callbacks to web service are becoming very popular and unavoidable these days. The feel that the user gets when only a part of the page is updated without even hindering the UI is the advantage that the callbacks offer. The wait for the…
Today I am discussing AJAX problems in IE7 and I bet this will helps many guys out here who have problems with AJAX work. Lets start with the discovery of problem and then we will talk about its different solutions. My last two projects included …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question