Yes and no. The British East India Company was a direct participant in both wars, but it is important not to overstate its role in the Opium War in particular. It was compelled by the British government to send troops and ships as part of the expeditionary force, but was not itself responsible for initiating the conflict – indeed, the EIC had virtually zero direct commercial interests in China following the end of their monopoly on Anglo-Chinese trade in 1833. At the same time, we can argue that both wars had similar origins in the post-Napoleonic ideologies of the major world empires (i.e. Britain and Russia). My speciality is definitely not Afghanistan, though I have summarised British attitudes to war with China in this recent answer which you should read first for context.
As for Afghanistan, the invasion of 1839 was prompted in the main by British paranoia over the threat posed to India by growing Russian interests in the Qajar Persian empire via the Caucasus – not, as is often presumed, Central Asia. The Russians had been acquiescent to, if not encouraging of, Qajar aggression in Afghanistan, culminating in a siege of Herat that began in 1837, and the presence at this siege of a Russian envoy, Count I. O. Simonich, helped legitimise the fears expressed by alarmist elements in the diplomatic corps like Sir John McNeill. At the same time, there were longer-term reasons for desiring a pliant Afghan state, particularly as regards diplomatic interests within India. The Sikh Empire under Ranjit Singh was considered a key British ally for advancing its interests in India, and the threat of either an Afghan invasion of the Sikhs, or of the Sikhs becoming embroiled in Afghanistan and unable to support the British in India, led to attempts to resolve the Afghan-Sikh disputes (the most significant being the Sikh annexation of Peshawar in 1833) in the lead-up to what became the 1839 invasion. Their failure was due in large part to a general misapprehension – often through genuine self-delusion on the British part – as to the interests of the Afghan ruler Dost Muhammad Khan and the extent of his desires for an alliance with Britain, to the point where one envoy even developed a plan to backstab the Sikhs to secure such an alliance.
With the British having bungled any diplomatic solution, they inched further along the road to war. Russian movements in the general region further escalated British anxieties – aside from the Russian presence at the siege of Herat, they also deployed a military officer as envoy to Kabul in late 1837. Among the final triggers, though, was the mission of a Polish envoy in Russian service, Jan Witkiewicz (Ivan Vitkevich), who spent most of 1837 and the first half of 1838 gathering intelligence in Afghanistan, before destroying all his notes and taking his own life on returning to St. Petersburg in May 1839. The suspicious circumstances of Witkiewitcz's death further convinced the alarmists that there were sinister Russian motives in Afghanistan. Around the same time, it became known that the Russians began building up forces and supplies for an invasion of the Central Asian state of Khiva (now mostly part of Uzbekistan) in early 1838. It is worth noting that while Russian officials operating in Persia via the Caucasus often did harbour notions of a displacement of British power in India (Count Simonich especially), the eventual invasion of Afghanistan was perceived as disastrous to Russian interests; in Central Asia however, the Russians were in many cases realising far more longstanding interests in the region, and even planned to delay the execution of the Khiva expedition so as to offer the British a freer hand in Afghanistan (this in the event did not transpire).
The British, however, did not know this, and this in turn led to seeking a means of justifying an invasion of Afghanistan in order to create a strong buffer state against Russia. As part of this, the British began to delude themselves that the Barazkai dynasty of Dost Muhammad Khan was deeply unpopular, and the old Durrani line would be welcomed back with open arms. Preparations began in early 1838 to escalate what had been a British-backed proxy war between the Sikhs and Afghans into a direct British intervention, and the rest, as they say, is history.
As such, there was no direct link between the First Anglo-Afghan War and the Opium War. While the British in India would have heard of the unfolding crisis in Canton that began in March 1839 sooner than anyone in Britain itself, the ramping-up of paranoia about Russia had led to concrete moves towards war in Afghanistan a whole year earlier. Moreover, the invasion of Afghanistan, while acquiesced to by the Foreign Secretary, was left largely to East India Company remit, whereas the war with China was a matter of direct concern to the British state.
At the same time, we can say that both wars had comparable motivations. The following quote from Alexander Morrison's 'Twin Imperial Disasters: The invasions of Khiva and Afghanistan in the Russian and British official mind, 1839–1842' applies mainly in the context of the two invasions mentioned in the title, but to a great extent also fits for China:
On the British side, debates regarding the annexation of Afghanistan and the development of trade along the Indus certainly were informed by contemporary ideas of political economy, which so many East India Company officials of that generation had imbibed at Haileybury. While the Russians also referred piously to the importance of trade, officials of both states saw it primarily as an instrument of policy, a means of projecting power by non-military means, and in both cases they had only the vaguest idea of what the real prospects for trade in Central Asia actually were. Economic theories of imperial expansion are thus of little relevance here. Of much greater importance are questions of institutional, and in particular military bureaucratic, culture; 'Great Power' prestige; personal ambition (for honours and promotion, rather than better investment returns); growing European prejudice against 'barbarous Asiatics'; and the concomitant belief that they had the means and duty to spread 'civilization' in Asia.
The Opium War arguably did have more commerce in mind considering the lucrative trade at stake, but we see on the British side a misapprehension of the mechanics of that commerce in the presumption that increasing the number of available ports would increase the overall quantity of trade, rather than spread the same volume of trade out across more regions (which is more or less what happened); and you can go to the linked answer for detail on the British focus on matters of 'civilisation' and 'barbarism'. The notion that Dost Muhammad was deeply resented by his people and that the restoration of the Durranis would be desirable arguably also finds a parallel in British presumptions – which began with the Macartney mission of 1793 – that the Manchu Qing were deeply unpopular and that the Han Chinese would welcome British support towards their 'liberation'. This, to be fair, turned out to actually be true when the Taiping rocked up in the 1850s, but that does not mean the conclusion was legitimately achieved: correct as it turned out to be, it was nevertheless based on self-reinforcing assumptions and not a real assessment of the situation on the ground.
For Afghanistan, see
For China, see the reading section in the linked answer.