I am new to PHP. I have a problem. I have a simple HTML form where when I select the country from select / option list provided, i want PHP to echo message on country I selected. Also I want PHP include function to list the form specific to that country name (by concatanating). Can someone please help me by pointing where I went wrong. I am new to PHP, self taught and have no idea if this approach is correct. Thanks in advance for anticipated help.
<pre>
<form name="form1" method="post" action="component_project_in_countries.php">
<label for="country">View outlets in which country? </label>
<select name="country_chosen" onchange="document.form1.submit()" id="country">
<option value="">All Middle Eastern Countries</option>
<option value="Iraq">Iraq</option>
<option value="Kuwait">Kuwait</option>
<option value="Bahrain">Bahrain</option>
<option value="Saudi">Saudi Arabia</option>
<option value="Qatar">Qatar</option>
<option value="Oman">Oman</option>
<option value="Yemen">Yemen</option>
<option value="Jordan">Jordan</option>
<option value="Israel">Israel</option>
</select>
</form>
<br>
<?php
$country_selected = '';
$country_selected = $_POST['country_chosen'];
echo "You have chosen to view outlets in " . $country_selected;
include ("inc/OutletsIn" . $country_selected .".php";)
?>
</pre>
Thank you for your help on my earlier post. I am able to echo out and concatenate file name in PHP include. But I have a new problem. The option value i selected (country name) does not get displayed in the box. How can i hold the chosen value for the session. Please advise.
Thank you again.
First check that a value has been given to $_POST['country_chosen'] with isset(). Then remove that extra semi-colon that is causing a syntax error (in your include()) statement:
if(isset($_POST['country_chosen'])){
$country_selected = $_POST['country_chosen'];
echo "You have chosen to view outlets in " . $country_selected;
include("inc/OutletsIn" . $country_selected .".php");
}
Includes something (like) inc/OutletsInIsrael.php
if(!empty($_POST['country_chosen'])){
$country_selected = $_POST['country_chosen'];
echo "You have chosen to view outlets in " . $country_selected;
require "inc/OutletsIn".$country_selected.".php";
//no brackets required
} else { /* throw error */ }
Here is your updated code, just copy & replace :
component_project_in_countries.php
<pre>
<form name="form1" method="post" action="component_project_in_countries.php">
<label for="country">View outlets in which country? </label>
<select name="country_chosen" onchange="document.form1.submit()" id="country">
<option value="">All Middle Eastern Countries</option>
<option value="Iraq">Iraq</option>
<option value="Kuwait">Kuwait</option>
<option value="Bahrain">Bahrain</option>
<option value="Saudi">Saudi Arabia</option>
<option value="Qatar">Qatar</option>
<option value="Oman">Oman</option>
<option value="Yemen">Yemen</option>
<option value="Jordan">Jordan</option>
<option value="Israel">Israel</option>
</select>
</form>
<br>
<?php
if(isset($_POST['country_chosen']))
{
$country_selected = '';
$country_selected = $_POST['country_chosen'];
echo "You have chosen to view outlets in " . $country_selected;
include ("inc/OutletsIn" . $country_selected .".php");// here you have added `;` on wrong place.
}
?>
</pre>
New answer, Just replace the code of select box :
<select name="country_chosen" onchange="document.form1.submit()" id="country">
<option value="">All Middle Eastern Countries</option>
<option value="Iraq" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Iraq')) echo 'selected' ?> >Iraq</option>
<option value="Kuwait" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Kuwait')) echo 'selected' ?> >Kuwait</option>
<option value="Bahrain" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Bahrain')) echo 'selected' ?> >Bahrain</option>
<option value="Saudi" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Saudi')) echo 'selected' ?> >Saudi Arabia</option>
<option value="Qatar" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Qatar')) echo 'selected' ?> >Qatar</option>
<option value="Oman" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Oman')) echo 'selected' ?> >Oman</option>
<option value="Yemen" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Yemen')) echo 'selected' ?> >Yemen</option>
<option value="Jordan" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Jordan')) echo 'selected' ?> >Jordan</option>
<option value="Israel" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Israel')) echo 'selected' ?> >Israel</option>
</select>
Related
I want to select a target country in html.
And this code should return for example "Turkey" from my db.
<?php echo $_SESSION['user']['www']?>
I have a country list, and I use it here in settings_myinfo.php :
<select id="profile-country" name="profile_country">
<option value="Select your Country">Select your Country</option>
<?php include($root."countries_optionlist.php");?>
</select>
countries_optionlist.php :
<option value="Afganistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
<option value="Andorra">Andorra</option>
<option value="Angola">Angola</option>
<option value="Anguilla">Anguilla</option>
<option value="Antigua & Barbuda">Antigua & Barbuda</option>
<option value="Argentina">Argentina</option>
<option value="Armenia">Armenia</option>
<option value="Aruba">Aruba</option>
<option value="Australia">Australia</option>
<option value="Austria">Austria</option>
<option value="Azerbaijan">Azerbaijan</option>
<option value="Bahamas">Bahamas</option>
<option value="Bahrain">Bahrain</option>
<option value="Bangladesh">Bangladesh</option>
<option value="Barbados">Barbados</option>
and more...
I want to add selected tag to target country like this:
<option value="Turkey" selected>Turkey</option>
I did some research on Google and StackOverflow but I couldn't find any solution.
Could you provide me with a suggestion on how to achieve this?
In each option, you can check if the session variable matches that country and add the selected attribute.
<?php $user_country = $_SESSION['user']['www']; ?>
<option value="Afganistan" <?php if ($user_country == 'Afghanistan') echo 'selected'; ?>>Afghanistan</option>
<option value="Albania" <?php if ($user_country == 'Albania') echo 'selected'; ?>>Albania</option>
<option value="Algeria" <?php if ($user_country == 'Algeria') echo 'selected'; ?>>Algeria</option>
...
You can simplify this by putting all the country names in an array. Then you can loop like this:
<?php
$user_country = $_SESSION['user']['www'];
foreach ($countries as $country) { ?>
<option value="<?php echo $country; ?>" <?php if ($user_country == $country) echo 'selected'; ?>>
<?php echo htmlspecialchars($country); ?>
</option>
<?php
}
I try to find the way how to make option tag selected if contain current day.
For example if my string $currentDay=03; called by PHP date('d'); I would like my select like this:
<select>
<option value="01">01</option>
<option value="02">02</option>
<option value="03" selected>03</option>
<option value="04">04</option>
...
...
</select>
<?php $day=date('d');?>
<select>
<option value="01" <?=($day=='01')?'selected':'';?> >01</option>
<option value="02" <?=($day=='02')?'selected':'';?> >02</option>
<option value="03" <?=($day=='03')?'selected':'';?> >03</option>
<option value="04" <?=($day=='04')?'selected':'';?> >04</option>
...
...
</select>
You can use the following code:
$day = date('d');
$selected = ' selected="selected" ';
<select>
<option value="01" <?php if($day=='01') echo $selected; ?>>01</option>
<option value="02" <?php if($day=='02') echo $selected; ?>>02</option>
<option value="03" <?php if($day=='03') echo $selected; ?>>03</option>
<option value="04" <?php if($day=='04') echo $selected; ?>>04</option>
...
...
</select>
<select>
<? foreach ($options as $option) : ?>
<option value="<?= $option ?>" <? if ($selected_option==$option) echo 'selected' ?>>
<?= $option ?>
</option>
<? endforeach ?>
</select>
I'm trying to create a form with text fields and dropdowns. With the text fields, I'm using this code:
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?php echo $user->name;?>" style="width:95%;"/>
The form then checks if the user has filled out the "name" field with this:
$name = $input->get('name', null);
if($name == null)
return false;
This works fine for the multiple text fields that the form uses, but I don't know how to check for the user input on the dropdowns. How can I send the option selected by the user, similar to the way I did it with the text field, so that I can check to make sure the user selected something? Example:
<label for="department">Department</label>
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4"><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
$department1 = $input->get('department1', null);
$department2 = $input->get('department2', null);
Etc........
This is set up the exact same way as the text fields as far as I know, but doesn't work and seems like a bad way to do it anyways.
if you want to verify and pre-select one option you should do something like this:
<option value="6" <?php if($params->get('department6')==true){ echo 'selected'; } ?>><?php echo $params->get('department6');?></option>
First of all, instead of having 8 lines like that for your departments, you could use a for loop.
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>"><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
Now, inside your for loop, you can add a check if the $input->get is true.
<?php if($input->get('department' . $i, null)) echo 'selected'; ?>
So if you mix both together, the result would be
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>" <?php if($input->get('department' . $i, null)) echo 'selected'; ?>><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
And if you want to a "pre selected" option use "selected" in the option you want:
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4" selected><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
As explained by W3CSchools.
I have a dropdown list. However i want to set a default based on user selection. Therefore the default value is not consistent. What can i do to achieve this? I have set $country = $_POST['country']; as user selection.
<td>Country:</td>
<td colspan="2"><select name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
To add on. There are 200 over options (I never list all down) so i hope to get a convenience way to achieve this
You can use 'selected' for relevant option
<option value="93" <?php if($country==93) echo "selected" ?> >93-Afghanistan</option>
<option value="355" <?php if($country==355) echo "selected" ?> >355-Albania</option>
like this add if conditon for each option.
Try using session for it.
The php code:
session_start();
$_SESSION['selectedCountry'] = $_POST['country'];
Then you can use JQuery to make dropdown selected:
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_SESSION['selectedCountry'];?>");
});
})
Please try out this ..
HTML :-
<td>Country:</td>
<td colspan="2"><select id="country" name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
</td>
JQuery :-
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_POST['country'];?>");
});
})
Try this...
<?php
(isset($_POST["country"])) ? $country = $_POST["country"] : $country=93;
?>
<form action='stackover.php' method='POST'>
<select id="country" name="country">
<option <?php if ($country == 93 ) echo 'selected' ; ?> value="93">93-Afghanistan</option>
<option <?php if ($country == 355 ) echo 'selected' ; ?> value="355">355-Albania</option>
<option <?php if ($country == 213 ) echo 'selected' ; ?> value="213">213-Algeria</option>
</select>
<input type="submit" value="Submit">
</form>
This is a simple one, i know it is i just cant think of a good logical way to do this.
I have the following code:
<select name="Title[]" class="form-control">
<option value="">Select title...</option>
<option value="Mr">Mr</option>
<option value="Miss">Miss</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Prof">Prof</option>
<option value="Doctor">Doctor</option>
</select>
What i want to do is on my edit client page, have their previously selected option displayed. So for example, if Doctor was selected at sign up, it would be selected by default on the edit page. I know i could do this like:
<option value="Doctor" <?php if($client_title == 'Doctor'){ echo 'selected'; } ?>>Doctor</option>
But it seems like a bit of a redundant way to do it. Can this be done easily with a do while statement?
Sorry for the simple request, having a bit of a slow day today! haha
<select name="title[]" class="form-control">
<?php
$titles = array('Mr', 'Miss', 'Mrs', 'Ms', 'Prof', 'Doctor');
foreach ($titles as $title) {
$selected = $client_title == $title ? ' selected="selected"' : null;
?>
<option value="<?php echo $title; ?>"<?php echo $selected; ?>><?php echo $title; ?></option>
<?php
}
?>
</select>