How to apply switch case in ajax?

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.
designersxAsked:
Who is Participating?
 
kurijovConnect With a Mentor Commented:

:::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
 
kurijovCommented:
You talking about this?

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

Open in new window

0
 
designersxAuthor Commented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
brandonvmooreCommented:
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
 
CWS (haripriya)Commented:
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
 
kurijovCommented:
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
 
designersxAuthor Commented:
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
 
designersxAuthor Commented:
sir thanks, i am trying this i will let u know about this in a moment.
0
 
designersxAuthor Commented:
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
 
designersxAuthor Commented:
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
 
designersxAuthor Commented:
any response from ur side?? please tell me if u have got some problem in understanding this?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.