Javascript Help

Status
Not open for further replies.

garycocs

New Member
Lads again my JS knowledge is letting me down in AJAX.

My ajax is returning the code

Code:
addPubNum( 5940, '1796 Wolfe Tone Bar' , 'pubid1' );
This function can be seen here:

Code:
function addPubNum( id, name, pubnum ) {document.forms['addpub'].elements[ pubnum ][ document.forms['addpub'].elements[ pubnum].length ] = new Option( name, id );}
The problem is that this function/code isn't populating the form:

Code:
<table width="100%">    <form name="addpub" action="userpage.php" method="GET">
                                                                
    <tr><td colspan="2">Please Enter Your 1st Pub</td></tr>
    <tr><td><select name="countyid" onChange="showPubNum(1,this.value);"><option value="0">Select County</option>
    <option value="1">Antrim</option>
    <option value="2">Armagh</option>
    </select></td>        
    <td><select name="pubid1"><option value=""></option></select> </td></tr>
    <tr><td colspan="2" align="right"><input type="submit" value="Add"></td></tr>
</form></table>


If anyone could see an error off the top of their head that would be great!

P.S. -> showPubNum(1,this.value); is working fine!
 

garycocs

New Member
where is this element on your form?

pubnum

I am using pubnum as a variable (Sorry about my naming schemes it was late last night doing this one)

basically I want to take a variable into the function to select which element to change.

in this case I want to change pubid1 so I take in variable pubid1.
 

louie

New Member
garry you are all over the place with that AJAX code.

What are you trying to do? Push options into the select menu or replace the select menu with a new one?
 

garycocs

New Member
How do you mean all over the place?

I run the ajax on a select, this pushes to the ajax.php which returns a set of javascript commands which I run to populate the options?
 

louie

New Member
try this solution:
PHP:
<script type="text/javascript">
var xmlHttp;
function addPubNum( id, name, pubnum ) {
 document.forms['addpub'].elements[ pubnum ][ document.forms['addpub'].elements[ pubnum].length ] = new Option( name, id )
}
function remPubNum( pubnum ) {
 while( document.forms['addpub'].elements[ pubnum ].length > 0 ) document.forms['addpub'].elements[ pubnum ][0] = null
}

function showPubNum(pubnum,countyid){ 
 xmlHttp=GetXmlHttpObject();
 if (xmlHttp==null) {
   alert ("Browser does not support HTTP Request");
   return;
  }
 var span_select = document.getElementById("pubnum");
 var url="ajax.php?";
 //url=url+"?pubnum="+pubnum
 url=url+"countyid="+countyid;
 url=url+"&sid="+Math.random();
 xmlHttp.onreadystatechange=stateChangedPubNum('span_select');
 xmlHttp.open("GET",url,true);
 xmlHttp.send(null);
}
function stateChangedPubNum('span_select') { 
 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
   //eval( xmlHttp.responseText  );
   document.getElementById("span_select").innerHTML= xmlHttp.responseText ;
  } 
}

function GetXmlHttpObject(){
 var xmlHttp=null;
 try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
 catch (e)
  {
  //Internet Explorer
  try
   {
   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
   }
  catch (e)
   {
   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
  }
 return xmlHttp;
}
</script>
</head>
<body>
 <select name="countyid" onChange="showPubNum('span_pubid1',this.value);">
     <option value="">Select County</option>
        <option value="1">Antrim</option>
        <option value="2">Armagh</option>
        <option value="3">Carlow</option>
        <option value="4">Cavan</option>
        <option value="5">Clare</option>
        <option value="6">Cork</option>
        <option value="7">Derry</option>
        <option value="8">Donegal</option>
        <option value="9">Down</option>
        <option value="10">Dublin</option>
        <option value="11">Fermanagh</option>
        <option value="12">Galway</option>
        <option value="13">Kerry</option>
        <option value="14">Kildare</option>
        <option value="15">Kilkenny</option>
        <option value="16">Laois</option>
        <option value="17">Leitrim</option>
        <option value="18">Limerick</option>
        <option value="19">Longford</option>
        <option value="20">Louth</option>
        <option value="21">Mayo</option>
        <option value="22">Meath</option>
        <option value="23">Monaghan</option>
        <option value="24">Offaly</option>
        <option value="25">Roscommon</option>
        <option value="26">Sligo</option>
        <option value="27">Tipperary</option>
        <option value="28">Tyrone</option>
        <option value="29">Waterford</option>
        <option value="30">Westmeath</option>
        <option value="31">Wexford</option>
        <option value="32">Wicklow</option>
    </select> 
 <span id="span_pubid1"><select name="pubid1" disabled="disabled"><option value=""></option></select></span>
 <input type="submit" value="Add">
 
Status
Not open for further replies.
Top