put country dropdown in phtml file in magento2












2















sayhello.phtml



<!DOCTYPE html>
<html>
<head>
<h2>Wholesale and Dealer Inquiries Form</h2>
</head>
<body>
<table align="center">
<form action="<?php echo $block-
>getBaseUrl().'wholesaleapplication/index/post/';?>" method="post">
<tr>
<td><label> Business Name </label></td>
<td> <input type="text" name="name"></td>
</tr>
<tr>
<td><label> Contact Name </label></td>
<td><input type="text" name="cname"></td>
</tr>
<tr>
<td> <label> E-mail </label></td>
<td><input type="email" name="email"></td>
</tr>
<tr>
<td> <label> Address </label></td>
<td><input type="text" name="address">
<label> Street Address </label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="address2">
<label> Street Address line 2</label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="city">
<label>city</label></td>
<td><input type="text" name="state">
<label>state/province</label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="zipcode">
<label>postal zip/code</label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="areacode">
<label>area code</label></td>
<td><input type="text" name="phonenumber">
<label>phone number</label></td>
</tr>
<tr>
<td> <label class="form-label form-label-left form-label-auto" id="label_9" for="input_9"> Website </label></td>
<td><input type="text" name="website"></td>
</tr>
<tr>
<td> <label> Reseller Permit </label></td>
<td><input type="text" name="reseller"></td>
</tr>
<tr>
<td> <label> Description of Business </label></td>
<td><textarea name="description" ></textarea></td>
</tr>
<tr>
<td> <label> Additional Questions and Comments </label></td>
<td><textarea name="additional" ></textarea></td>
</tr>
<tr>
<td> <label> click to edit </label></td>
<td><input type="file"></td>
</tr>
<tr>
<td><button type="submit" name="submit">
Submit
</button></td>
</tr>

</form>





country dropdowncode



<?php $_countries = 
Mage::getResourceModel('directory/country_collection')
loadData()
toOptionArray(false) ?>
<?php if (count($_countries) > 0): ?>
<select name="country" id="country">
<option value="">-- Please Select --</option>
<?php foreach($_countries as $_country): ?>
<option value="<?php echo $_country['value'] ?>">
<?php echo $_country['label'] ?>
</option>
<?php endforeach; ?>
</select>
<?php endif; ?>


i want drop down near zip code in phtml file where to keep this country code?










share|improve this question























  • you have tried Magento 1 code>

    – Amit Bera
    Feb 21 at 7:28











  • can you please share me magento2 code

    – Ashish Ramchandani
    Feb 21 at 7:29
















2















sayhello.phtml



<!DOCTYPE html>
<html>
<head>
<h2>Wholesale and Dealer Inquiries Form</h2>
</head>
<body>
<table align="center">
<form action="<?php echo $block-
>getBaseUrl().'wholesaleapplication/index/post/';?>" method="post">
<tr>
<td><label> Business Name </label></td>
<td> <input type="text" name="name"></td>
</tr>
<tr>
<td><label> Contact Name </label></td>
<td><input type="text" name="cname"></td>
</tr>
<tr>
<td> <label> E-mail </label></td>
<td><input type="email" name="email"></td>
</tr>
<tr>
<td> <label> Address </label></td>
<td><input type="text" name="address">
<label> Street Address </label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="address2">
<label> Street Address line 2</label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="city">
<label>city</label></td>
<td><input type="text" name="state">
<label>state/province</label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="zipcode">
<label>postal zip/code</label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="areacode">
<label>area code</label></td>
<td><input type="text" name="phonenumber">
<label>phone number</label></td>
</tr>
<tr>
<td> <label class="form-label form-label-left form-label-auto" id="label_9" for="input_9"> Website </label></td>
<td><input type="text" name="website"></td>
</tr>
<tr>
<td> <label> Reseller Permit </label></td>
<td><input type="text" name="reseller"></td>
</tr>
<tr>
<td> <label> Description of Business </label></td>
<td><textarea name="description" ></textarea></td>
</tr>
<tr>
<td> <label> Additional Questions and Comments </label></td>
<td><textarea name="additional" ></textarea></td>
</tr>
<tr>
<td> <label> click to edit </label></td>
<td><input type="file"></td>
</tr>
<tr>
<td><button type="submit" name="submit">
Submit
</button></td>
</tr>

</form>





country dropdowncode



<?php $_countries = 
Mage::getResourceModel('directory/country_collection')
loadData()
toOptionArray(false) ?>
<?php if (count($_countries) > 0): ?>
<select name="country" id="country">
<option value="">-- Please Select --</option>
<?php foreach($_countries as $_country): ?>
<option value="<?php echo $_country['value'] ?>">
<?php echo $_country['label'] ?>
</option>
<?php endforeach; ?>
</select>
<?php endif; ?>


i want drop down near zip code in phtml file where to keep this country code?










share|improve this question























  • you have tried Magento 1 code>

    – Amit Bera
    Feb 21 at 7:28











  • can you please share me magento2 code

    – Ashish Ramchandani
    Feb 21 at 7:29














2












2








2








sayhello.phtml



<!DOCTYPE html>
<html>
<head>
<h2>Wholesale and Dealer Inquiries Form</h2>
</head>
<body>
<table align="center">
<form action="<?php echo $block-
>getBaseUrl().'wholesaleapplication/index/post/';?>" method="post">
<tr>
<td><label> Business Name </label></td>
<td> <input type="text" name="name"></td>
</tr>
<tr>
<td><label> Contact Name </label></td>
<td><input type="text" name="cname"></td>
</tr>
<tr>
<td> <label> E-mail </label></td>
<td><input type="email" name="email"></td>
</tr>
<tr>
<td> <label> Address </label></td>
<td><input type="text" name="address">
<label> Street Address </label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="address2">
<label> Street Address line 2</label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="city">
<label>city</label></td>
<td><input type="text" name="state">
<label>state/province</label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="zipcode">
<label>postal zip/code</label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="areacode">
<label>area code</label></td>
<td><input type="text" name="phonenumber">
<label>phone number</label></td>
</tr>
<tr>
<td> <label class="form-label form-label-left form-label-auto" id="label_9" for="input_9"> Website </label></td>
<td><input type="text" name="website"></td>
</tr>
<tr>
<td> <label> Reseller Permit </label></td>
<td><input type="text" name="reseller"></td>
</tr>
<tr>
<td> <label> Description of Business </label></td>
<td><textarea name="description" ></textarea></td>
</tr>
<tr>
<td> <label> Additional Questions and Comments </label></td>
<td><textarea name="additional" ></textarea></td>
</tr>
<tr>
<td> <label> click to edit </label></td>
<td><input type="file"></td>
</tr>
<tr>
<td><button type="submit" name="submit">
Submit
</button></td>
</tr>

</form>





country dropdowncode



<?php $_countries = 
Mage::getResourceModel('directory/country_collection')
loadData()
toOptionArray(false) ?>
<?php if (count($_countries) > 0): ?>
<select name="country" id="country">
<option value="">-- Please Select --</option>
<?php foreach($_countries as $_country): ?>
<option value="<?php echo $_country['value'] ?>">
<?php echo $_country['label'] ?>
</option>
<?php endforeach; ?>
</select>
<?php endif; ?>


i want drop down near zip code in phtml file where to keep this country code?










share|improve this question














sayhello.phtml



<!DOCTYPE html>
<html>
<head>
<h2>Wholesale and Dealer Inquiries Form</h2>
</head>
<body>
<table align="center">
<form action="<?php echo $block-
>getBaseUrl().'wholesaleapplication/index/post/';?>" method="post">
<tr>
<td><label> Business Name </label></td>
<td> <input type="text" name="name"></td>
</tr>
<tr>
<td><label> Contact Name </label></td>
<td><input type="text" name="cname"></td>
</tr>
<tr>
<td> <label> E-mail </label></td>
<td><input type="email" name="email"></td>
</tr>
<tr>
<td> <label> Address </label></td>
<td><input type="text" name="address">
<label> Street Address </label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="address2">
<label> Street Address line 2</label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="city">
<label>city</label></td>
<td><input type="text" name="state">
<label>state/province</label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="zipcode">
<label>postal zip/code</label></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="areacode">
<label>area code</label></td>
<td><input type="text" name="phonenumber">
<label>phone number</label></td>
</tr>
<tr>
<td> <label class="form-label form-label-left form-label-auto" id="label_9" for="input_9"> Website </label></td>
<td><input type="text" name="website"></td>
</tr>
<tr>
<td> <label> Reseller Permit </label></td>
<td><input type="text" name="reseller"></td>
</tr>
<tr>
<td> <label> Description of Business </label></td>
<td><textarea name="description" ></textarea></td>
</tr>
<tr>
<td> <label> Additional Questions and Comments </label></td>
<td><textarea name="additional" ></textarea></td>
</tr>
<tr>
<td> <label> click to edit </label></td>
<td><input type="file"></td>
</tr>
<tr>
<td><button type="submit" name="submit">
Submit
</button></td>
</tr>

</form>





country dropdowncode



<?php $_countries = 
Mage::getResourceModel('directory/country_collection')
loadData()
toOptionArray(false) ?>
<?php if (count($_countries) > 0): ?>
<select name="country" id="country">
<option value="">-- Please Select --</option>
<?php foreach($_countries as $_country): ?>
<option value="<?php echo $_country['value'] ?>">
<?php echo $_country['label'] ?>
</option>
<?php endforeach; ?>
</select>
<?php endif; ?>


i want drop down near zip code in phtml file where to keep this country code?







magento2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 21 at 7:20









Ashish RamchandaniAshish Ramchandani

15510




15510













  • you have tried Magento 1 code>

    – Amit Bera
    Feb 21 at 7:28











  • can you please share me magento2 code

    – Ashish Ramchandani
    Feb 21 at 7:29



















  • you have tried Magento 1 code>

    – Amit Bera
    Feb 21 at 7:28











  • can you please share me magento2 code

    – Ashish Ramchandani
    Feb 21 at 7:29

















you have tried Magento 1 code>

– Amit Bera
Feb 21 at 7:28





you have tried Magento 1 code>

– Amit Bera
Feb 21 at 7:28













can you please share me magento2 code

– Ashish Ramchandani
Feb 21 at 7:29





can you please share me magento2 code

– Ashish Ramchandani
Feb 21 at 7:29










1 Answer
1






active

oldest

votes


















3














Magento already has a block class which is available to provide country dropdown.



On your phtml files call the block and you will get country dropdown



<?php $countryBlock = $block->getLayout()->createBlock('MagentoDirectoryBlockData');?>

<?= $countryBlock->getCountryHtmlSelect($defValue = null, $name = 'country_id', $id = 'country', $title = 'Country');?>


You can set default $defValue,$name,$id as per as your wise.






share|improve this answer
























  • i have to keep this code in sayhello.phtml file right?

    – Ashish Ramchandani
    Feb 21 at 7:38











  • yes, just you have to paste this code

    – Amit Bera
    Feb 21 at 7:40











  • thankyou it works

    – Ashish Ramchandani
    Feb 21 at 7:40











  • now i have to pass this in post varilable so what will be the name of the variable

    – Ashish Ramchandani
    Feb 21 at 7:42











  • +1 simple and nice @AmitBera

    – Prathap Gunasekaran
    Feb 21 at 7:47











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f262785%2fput-country-dropdown-in-phtml-file-in-magento2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Magento already has a block class which is available to provide country dropdown.



On your phtml files call the block and you will get country dropdown



<?php $countryBlock = $block->getLayout()->createBlock('MagentoDirectoryBlockData');?>

<?= $countryBlock->getCountryHtmlSelect($defValue = null, $name = 'country_id', $id = 'country', $title = 'Country');?>


You can set default $defValue,$name,$id as per as your wise.






share|improve this answer
























  • i have to keep this code in sayhello.phtml file right?

    – Ashish Ramchandani
    Feb 21 at 7:38











  • yes, just you have to paste this code

    – Amit Bera
    Feb 21 at 7:40











  • thankyou it works

    – Ashish Ramchandani
    Feb 21 at 7:40











  • now i have to pass this in post varilable so what will be the name of the variable

    – Ashish Ramchandani
    Feb 21 at 7:42











  • +1 simple and nice @AmitBera

    – Prathap Gunasekaran
    Feb 21 at 7:47
















3














Magento already has a block class which is available to provide country dropdown.



On your phtml files call the block and you will get country dropdown



<?php $countryBlock = $block->getLayout()->createBlock('MagentoDirectoryBlockData');?>

<?= $countryBlock->getCountryHtmlSelect($defValue = null, $name = 'country_id', $id = 'country', $title = 'Country');?>


You can set default $defValue,$name,$id as per as your wise.






share|improve this answer
























  • i have to keep this code in sayhello.phtml file right?

    – Ashish Ramchandani
    Feb 21 at 7:38











  • yes, just you have to paste this code

    – Amit Bera
    Feb 21 at 7:40











  • thankyou it works

    – Ashish Ramchandani
    Feb 21 at 7:40











  • now i have to pass this in post varilable so what will be the name of the variable

    – Ashish Ramchandani
    Feb 21 at 7:42











  • +1 simple and nice @AmitBera

    – Prathap Gunasekaran
    Feb 21 at 7:47














3












3








3







Magento already has a block class which is available to provide country dropdown.



On your phtml files call the block and you will get country dropdown



<?php $countryBlock = $block->getLayout()->createBlock('MagentoDirectoryBlockData');?>

<?= $countryBlock->getCountryHtmlSelect($defValue = null, $name = 'country_id', $id = 'country', $title = 'Country');?>


You can set default $defValue,$name,$id as per as your wise.






share|improve this answer













Magento already has a block class which is available to provide country dropdown.



On your phtml files call the block and you will get country dropdown



<?php $countryBlock = $block->getLayout()->createBlock('MagentoDirectoryBlockData');?>

<?= $countryBlock->getCountryHtmlSelect($defValue = null, $name = 'country_id', $id = 'country', $title = 'Country');?>


You can set default $defValue,$name,$id as per as your wise.







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 21 at 7:38









Amit BeraAmit Bera

58.8k1475174




58.8k1475174













  • i have to keep this code in sayhello.phtml file right?

    – Ashish Ramchandani
    Feb 21 at 7:38











  • yes, just you have to paste this code

    – Amit Bera
    Feb 21 at 7:40











  • thankyou it works

    – Ashish Ramchandani
    Feb 21 at 7:40











  • now i have to pass this in post varilable so what will be the name of the variable

    – Ashish Ramchandani
    Feb 21 at 7:42











  • +1 simple and nice @AmitBera

    – Prathap Gunasekaran
    Feb 21 at 7:47



















  • i have to keep this code in sayhello.phtml file right?

    – Ashish Ramchandani
    Feb 21 at 7:38











  • yes, just you have to paste this code

    – Amit Bera
    Feb 21 at 7:40











  • thankyou it works

    – Ashish Ramchandani
    Feb 21 at 7:40











  • now i have to pass this in post varilable so what will be the name of the variable

    – Ashish Ramchandani
    Feb 21 at 7:42











  • +1 simple and nice @AmitBera

    – Prathap Gunasekaran
    Feb 21 at 7:47

















i have to keep this code in sayhello.phtml file right?

– Ashish Ramchandani
Feb 21 at 7:38





i have to keep this code in sayhello.phtml file right?

– Ashish Ramchandani
Feb 21 at 7:38













yes, just you have to paste this code

– Amit Bera
Feb 21 at 7:40





yes, just you have to paste this code

– Amit Bera
Feb 21 at 7:40













thankyou it works

– Ashish Ramchandani
Feb 21 at 7:40





thankyou it works

– Ashish Ramchandani
Feb 21 at 7:40













now i have to pass this in post varilable so what will be the name of the variable

– Ashish Ramchandani
Feb 21 at 7:42





now i have to pass this in post varilable so what will be the name of the variable

– Ashish Ramchandani
Feb 21 at 7:42













+1 simple and nice @AmitBera

– Prathap Gunasekaran
Feb 21 at 7:47





+1 simple and nice @AmitBera

– Prathap Gunasekaran
Feb 21 at 7:47


















draft saved

draft discarded




















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f262785%2fput-country-dropdown-in-phtml-file-in-magento2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?