Jump to content

Help with Javascript inside a PHP function


spires

Recommended Posts

Hi Everyone.

 

I'm working on a form, that allows the user to select the country / county / area where they are from.

I am doing this using:

 

- mysql database store the locations in

- PHP OOP, to pull this information from the database

- AJAX/Javascript to only show the next dropdown, once the first dropdown has been selected.

 

Example:

select country then the county dropdown will appear, select county then the area/town will appear.

 

 

However, the first drop down for country is placed directly on to the page. So the javascript works fine.

but the second dropdown is placed in to a PHP function, and the javascript seems to no longer want to work.

 

CODE:

   //////////////// CODE FOR COUNTRY ///////////
   if(isset($_REQUEST['country'])) {
  		echo '
		<div class="labelleft">County / State:</div>
		  <div class="fieldleft">
			<div >	
			  <select name="county" id="county" onchange="javascript:AJAXQuery("lib/search.php?town="+this.value, "tw", "")">
			  <option value="0">Select County</option>
			  <option value="0">-------</option>';
			  $countyList = Area_county::find_country_id($_REQUEST['country']);
			  foreach($countyList as $countyLists){
				  $county_id = $countyLists->id;
				  $county_list = $countyLists->county;
				  
			  echo '<option value="'.$county_id.'">'.ucwords($county_list).'</option>';
			  }
			  echo '
		      </select>
		    </div>
	  	  </div>
	    <div class="clear"></div>';	  
}

 

 

Any ideas?

 

Thanks

Dude,

 

The issue is Default JS wont work with the Dynamic form Elements....... I mean to say that, the second drop down is dynamic here. You cant apply any Loaded JS like on change of the values, do something or blah blah...... You can try with the following manner....

 

  While you are loading the second drop down, add an event function on Change of that... like render the second drop down as follows:

<select name="second" onchange="return call_JS_function_for_AJAX_Call_Or_Something_Else()" >

<option>1</option>

<option>2</option>

</select>

 

In that JS function write ur logic......

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.