$_POST method fetches from array and displays result - php

So I want the form to find what the user searches for and then return results for that, through the array down below. Is this possible? I've tried to write the php myself, but I'm clueless.
<form action="result.php" method="post">
<input type="text" name="country" size="30" id="autocomplete-ajax" placeholder="Search for country, timezone or city and hit enter!" maxlength="30">
</form>
<?php
$countries = array(
"AD" => "Andorra",
"AE" => "United Arab Emirates",
"AF" => "Afghanistan",
"AG" => "Antiqua and Barbuda",
"AI" => "Anguilla",
"AL" => "Albania",
"ZZ" => "Unkown or Invalid Region");
if (isset($_POST[$countries[0]])){
echo "<p>Correct!</p>";
}
if (isset($_POST[$countries[1]])){
echo "<p>Correct2</p>";
}
?>

You could replace the other issets with this - it will check the post values against the array
if (isset($_POST["country"])){
foreach($countries as $k => $c){
if(strtolower($c) == strtolower(trim($_POST["country"]))){
echo "<p>Correct!</p>";
}
}
}

Related

PHP - Search through PHP array through input field?

<?php
$allBookings = [
"IMD002" => [
"client" => "James Holden",
"paid" => "yes",
"email" => "james.holden#gmail.com"
],
"IMD003" => [
"client" => "Harold P. Redman",
"paid" => "yes",
"email" => "HaroldPRedman#dayrep.com"
],
"IMD004" => [
"client" => "Marcus C. Nelson",
"paid" => "no",
"email" => "MarcusCNelson#armyspy.com"
]
];
// DONT EDIT ANYTHING BELOW HERE
if(!empty($_POST)) {
$clientName = $booking['client'];
}
else {
$clientName = null;
};
?>
<!-- DONT EDIT ANYTHING BELOW HERE -->
<div class="myApp">
<form method="post">
<input type="text" name="bookingReference" placeholder="Booking Reference">
<input type="submit" value="Find Client">
</form>
<h2 id="result_name">Client name: <em><?php echo $clientName; ?></em></h2>
</div>
How can I showup the credentials when I type into the input field (booking reference) for example "IMD002"? As it should show up the array listed in php under "IMD002". I'm assuming its something to do with a $_POST statement?
Thanks alot
After $allBookings = [ ... ]
Try adding something like :
if(isset($_POST['bookingReference']) &&
isset($allBookings[$_POST['bookingReference']])) {
$booking = $allBookings[$_POST['bookingReference']];
}
else {
$booking = ['client' => 'Invalid Ref/Not found'];
};
To search, Use $_POST['bookingReference'] as index to, $allBookings, your data array;
Try:
$allBookings[$_POST['bookingReference']];
Print the content:
print_r( $allBookings[$_POST['bookingReference']] );

how to change select values when select an option from other drop down box in codeigniter

I have two drop down boxes its values are fetched from utility libray
public $email_hooks = array(
"create_user" => array(
"name" => "New user creation",
"keys" => array(
"site_url" => "Site URL",
"current_date" => "Current Date",
"user_name" => "User name",
"name" => "Name of the user",
"password" => "Password of user",
"user_email" => "Email address of new user"
),
"content_type" => array("html","text"),
"to" => array("User", "Site Admin"),
"cc" => array("Site Admin"),
"bcc" => array("Site Admin")
),
"register_user" => array(
"name" => "New user registration",
"keys" => array(
"site_url" => "Site URL",
"current_date" => "Current Date",
"user_name" => "User name",
"name" => "Name of the user",
"activation_url" => "Url for activation registered account",
"user_email" => "Email address of the user"
),
"content_type" => array("html","text"),
"to" => array("User", "Site Admin","new user"),
"cc" => array("Site Admin"),
"bcc" => array("Site Admin")
));
following are view code
<div class="form-group col-lg-12">
<label class="control-label col-lg-2">Name</label>
<div class="col-lg-4">
<select id ="choose" class="form-control" name="name">
<option value="">Select One</option>
<?php
foreach($email_hooks as $key=>$val)
{
echo '<option value="'.$key.'">'.$val['name'].'</option>';
}
?>
</select>
</div>
<div class="col-lg-6" id="email_tmpl"></div>
</div>
<div class="form-group col-lg-12">
<label class="control-label col-lg-2">Notification to</label>
<div class="col-lg-10">
<select id ="notification" class="form-control" name="notification">
<option value="">Select One</option>
</select>
</div>
</div>
When select first drop down box value,i want to change 2nd dropdown values ,ie "to" => array("User", "Site Admin","new user").these array values change according with the first selection box.
Write a javascript/jquery function and get the value from your first drop-down through java script or jquery. Then use onchange to call the function and then append the array accordingly to the second drop-down menu. Hope this works...
Your first Dropdown:
<select id ="choose" class="form-control" name="name" onchange="your_javascript_function()">
Use javascript function and use jquery append function and each function in it:
function your_javascript_function
{$.each(your_array, function() {
$('#notification').append( $('<option value="' + this.value_from_array + '">' + this.value_from_array + '</option>' ));
});
}

if(in_array) not executing correctly

I am trying to put together a geodetection for altering slight language variables.
I have the detection working perfectly, but the array check seems to not be working, I need to know if its from a list of countries. If I echo the country then I get the correct name so I know that parts working.
//Get User Country
$country_arr = array(
"Canada" => "ca",
"United States" => "us",
"United Kingdom" => "uk",
"Australia" => "au",
"South Africa" => "za",
"Unknow" => "shot"
);
$country=visitor_country();
if (in_array($country, $country_arr)) {
//include ("languages/" . $lang . ".php");
//echo $country_arr[$country];
echo "yes
";
} else {
//include ("languages/en.php");
echo "no
";
}
echo $country;
Have a functioning sandbox with all the related code working and edible http://sandbox.onlinephpfunctions.com/code/714d5105012f28cada695a6f11dc61516722e6d7
Also not working with a standard 1 dimensional array
$count_array = array("South Africa", "Unknow");
Use array array_key_exists in place of in_array
//Get User Country
$country = visitor_country();
$country_arr = array(
"Canada" => "ca",
"United States" => "us",
"United Kingdom" => "uk",
"Australia" => "au",
"South Africa" => "za",
"Unknown" => "shot"
);
//$count_array = array("South Africa", "Unknown");
if ( array_key_exists($country, $country_arr) ) {
//include ("languages/" . $lang . ".php");
//echo $country_arr[$country];
echo "yes<br>";
} else {
//include ("languages/en.php");
echo "no<br>";
}
echo $country;
For in_array function your $country_arr array should be like this
/* For IN Array */
$country_arr = array(
"Canada",
"United States",
"United Kingdom",
"Australia",
"South Africa",
"Unknown"
);
your $count_array = array("South Africa", "Unknow"); is not working because $country returns Unknown and you had Unknow that's not matching with with the value..
With in_array you check values not keys.
//Get User Country
$country_arr = array(
"Canada" => "ca",
"United States" => "us",
"United Kingdom" => "uk",
"Australia" => "au",
"South Africa" => "za",
"Unknow" => "shot"
);
$country = 'Canada';
if ( isset($country_arr[$country]) )
{
echo "yes";
}
else
{
echo "no";
}
echo "\n$country";
BTW
Keep in mind that PHP even with 'regular' arrays - without keys - have implicit keys so for in_array to work you would have to have:
$country_arr = array( "Canada", "United States", "United Kingdom" );
Above all countries have their keys (but implicit) so countries are values here. On your original code countries are keys.
You have a typo - Unknow / Unknown, also, you're not searching against keys with in_array(), you need to use array_key_exists() or array_flip($country_arr)
Your GeoIP service returns countryName and countryCode fields. Just use countryCode instead of countryName and your code will work:
if($ip_data && $ip_data->geoplugin_countryCode != null)
{
$result = $ip_data->geoplugin_countryCode;
}

PHP array_key_exists does not work; array is not multi-dimensional

I have an array of all the countries in the world as such:
$countries = array(
"GB" => "United Kingdom",
"US" => "United States",
"AF" => "Afghanistan",
"AL" => "Albania",
"DZ" => "Algeria",
"AS" => "American Samoa",
"AD" => "Andorra",
"AO" => "Angola",
"AI" => "Anguilla",
"AQ" => "Antarctica",
"AG" => "Antigua And Barbuda",
"AR" => "Argentina",
"AM" => "Armenia",
"AW" => "Aruba",
"AU" => "Australia",
"AT" => "Austria",
"AZ" => "Azerbaijan",
"BS" => "Bahamas",
"BH" => "Bahrain",
"BD" => "Bangladesh",
"BB" => "Barbados",
"BY" => "Belarus",
"BE" => "Belgium",
"BZ" => "Belize",
"BJ" => "Benin",
"BM" => "Bermuda",
"BT" => "Bhutan",
"BO" => "Bolivia",
"BA" => "Bosnia And Herzegowina",
"BW" => "Botswana",
"BV" => "Bouvet Island",);
And so on for all countries; I am 100% positive every country is listed properly.
I have an application form which stores the result in a file stored on the server. Currently the review page for the application is a basic text version and I am now in the process of putting it into a mock-form for my client to have a more visually appealing method of reviewing applications.
So an array named $in_data stores the results that come from the file. This array is structured as such "emergency_medical_insurance" => "value_user_entered". Each key is the name of the HTML element it came form and the value is what the user put in.
The country select list on the form returns a two-letter code of the country. So what I am trying to do is search $countries with the value of $in_data['country_select'] and then return the name of the country.
echo $in_data['country_select']; returns 'CA' the letter code for Canada and the test country I have entered.
echo $countries['CA']; returns 'Canada'
if (array_key_exists($in_data['country_select'], $countries)){
echo "Country Found";
}
else { echo "failed"; }
returns nothing.
if (array_key_exists('CA', $countries)){
echo "Country Found";
}
else { echo "failed"; }
Also returns nothing. And when I say nothing I mean nothing, not null, not true, not false; just doesn't even run.
My question is simple; how is the code below (taken from the offical PHP manual) which does EXACTLY the same thing my code does, working, but my code won't even return anything?
<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
?>
since you are reading from a file, you may be getting other characters, try trim():
if (array_key_exists(trim($in_data['country_select']), $countries)){
echo "Country Found";
}
else { echo "failed"; }
I had a similar problem while reading from a file.
The solution was to remove the BOM from the first line.
function remove_utf8_bom($text) {
$bom = pack('H*','EFBBBF');
$text = preg_replace("/$bom/", '', $text);
return $text;
}

Maintain select box submission value on validation fail - what am I doing wrong?

I've read many tutorials on this subject and concluded with the below code. The problem is, the Selection box state value is not maintained when my validation fails.
I have two files:
application.php - this file processes the request.
and
form.php - which is where my select code is:
<select name="textfield6" id="textfield6" class="textbox" style="width:80px;">
<?
$states = array('AL' => "Alabama",
'AK' => "Alaska",
'AZ' => "Arizona",
'AR' => "Arkansas",
'CA' => "California",
...
'WA' => "Washington",
'WV' => "West Virginia",
'WI' => "Wisconsin",
'WY' => "Wyoming");
foreach ($states as $abr => $full) {
$selected = ($textfield6 == $abr) ? " select=\"selected\"" : "";
echo "<option value=\"$abr\"$selected>$full</option>\n";
}
?>
</select>
in the $selected variable, it has to be selected="selected" instead of select="selected". (plus the escaping backslashes, of course!)

Categories