How to create a dropdown menu via ajax on change in another dropdown menu

please anybody solve this for me …

Do you have any code that you’ve attempted?

You have to provide more information for that.
How do you store menu items? Do you already have server-side script returning these items? How first menu coded? etc

its not a menu actually but just some dropdowns in form in a plugin i mtrying as i am a begginner in wordpress and trying to learn. The code is as follows:

<?php 


/*
 * Plugin Name: ajax Drop
 * Plugin URI: #
 * Description: ajax testing.
 * Version: 1.0
 * Author: Seven Digital Technology
 * Author URI: http://www.sevendigitaltechnology.com
 * License: A "Case Studies" license name e.g. GPL2
 */

//define('ACFSURL', WP_PLUGIN_URL."/".dirname( plugin_basename( __FILE__ ) ) );

//efine('ACFPATH', WP_PLUGIN_DIR."/".dirname( plugin_basename( __FILE__ ) ) );

function ajaxcontact_enqueuescripts()

{
	wp_localize_script('fetch_state', 'ajaxcontactajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('wp_enqueue_scripts', ajaxcontact_enqueuescripts);

register_activation_hook( __FILE__, 'callback_plug' );
register_deactivation_hook( __FILE__, 'remove_plug' );
//callback function
function callback_plug(){
global $wpdb;
$table_country = $wpdb->prefix . "ajacs_drops";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_country) {
    $wpdb->query("CREATE TABLE $table_name (
      entry_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      country_name VARCHAR(200) NOT NULL,
	  state_name VARCHAR(200) NOT NULL
    )");
	for($i=0;$i<count($countries);$i++)
	{$wpdb->query("INSERT INTO $table_country(country_name) VALUES('".$countries[$i]."')");}
}
/*$table_state = $wpdb->prefix . "ajacs_state";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
    $wpdb->query("CREATE TABLE $table_name (
      state_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      country_ID INT NOT NULL,
	  state_name VARCHAR(200) NOT NULL  
    )");
	$wpdb->query("INSERT INTO $table_country(country_name,state_name) VALUES('1','".$state."')");
}
else
{
}

$table_city = $wpdb->prefix . "ajacs_city";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
    $wpdb->query("CREATE TABLE $table_name (
      city_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      state_ID INT NOT NULL,
	  city_name VARCHAR(200) NOT NULL  
    )");
}
else
{
}*/
}


function remove_plug()
{
	global $wpdb;
	$table_name = $wpdb->prefix."ajacs_drops";
	$wpdb->query("DROP TABLE IF EXISTS $table_name");
}

if(is_admin())
{ 
	add_action('admin_menu', 'client_form_f1');
	function client_form_f1()
	{
		add_menu_page('ajacs','ajacs Data','administrator','ajacs','client_form_fetch');
		//add_submenu_page('case_studies','Display Clients','Display Clients','manage_options','case_slug','client_one_html_page');
		
	}
}

function client_form_fetch()
{
	$countries = array("India","america");
	$all = array("India"=>array("Gujarat","Madhya Pradesh","Rajasthan"));	
?>
	
	
	<table><tr><td><select name='country' id="country" onchange="state()">
	<option value=''>Select Country</option>"; 
	<?php
	for($i=0;$i<count($countries);$i++)
	{
		?>
		<option value="<?php echo $countries[$i]; ?>"><?php echo $countries[$i]; ?></option>
		<?php
	}
	?>
	</select> </td><td id='state'></td></tr></table>
 <script type="text/javascript">
	  alert('Hii');
	function state(){
    $.ajax
	({

	type: 'POST',
	url: ajaxcontactajax.ajaxurl,

	data: 
	{
		action:'fetch_state()',
		country:$('#country').val(),
	},
	success:function(res)
	{
		$('#state').html(res);
	},
	
	});}
	</script>
	<?php
	//return $send;
}

function fetch_state()
{
	//global $wpdb;
	//$table_name = $wpdb->prefix."ajacs_state";
	//$sel_state_data = $wpdb->get_results("SELECT * FROM $table_name WHERE country_ID = '".$_POST['country']."'");
	
	$send = "<select name='state'>";
	$send .="<option value=''>Select State</option>"; 
	
	for($i=0;$i<count($all);$i++)
	{
		if($all[i] == $_POST['country'])
		{
		for($j=0;$j<count($all[$i]);$j++)
		{
		
		$send .= "<option value=".$all[$i][$j].">".$all[$i][$j]."</option>";
		}}
	}
	
	$send .= "</select> ";

	
	return $send;
	
}
add_action( 'wp_ajax_nopriv_ajaxcontact_send_mail', 'fetch_state' );

add_action( 'wp_ajax_ajaxcontact_send_mail', 'fetch_state' );
/*function fetch_city()
{
	global $wpdb;
	$table_name = $wpdb->prefix."ajacs_city";
	$sel_state_data = $wpdb->get_results("SELECT * FROM $table_name WHERE state_ID = '".$_POST['state']."'");
	
	$send = "<select name='state' onChange='fetch_city(this.value)'>";
	$send .="<option value=''>Select Country</option>"; 
	
	foreach($sel_state_data as $sel)
	{
		
		$send .= "<option value=".$sel->city_ID.">".$sel->city_name."</option>";
		
	}
	
	$send .= "</select> ";

	
	return $send;
	
}*/


/*function get_city($sid)
{
	?>
    <script type="text/javascript">
    $.ajax
	({

	type: 'POST',
	url: ajaxcontactajax.ajaxurl,

	data: 
	{
		action:'fetch_city()',
		country:<?php echo $sid?>,
	},
	success:function(res)
	{
		$('#city').html(res);
	},
	
	});
	</script>
<?php 
}*/
?>

EDIT
This post has been reformatted by enclosing the code block in 3 backticks
```
on their own lines.

could you explain in detail…i am a newbie here …

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.