Auto filling --Dropbox - php

i have two text type forms(id,name) and another form dropbox type(school name)(select option type).
when user enters the id form, the remaining form (name) and the drop box(school name) needs to be filled based on the values in mysql.
filling form text type (name) is not a problem which i am able to do using json and jquery ,but i do not know how to auto select the dropbox.
any tutorials or code snippets suggestions are appreciated.

if your drop box is in xhtml, you can use the "selected" param.
with jquery, you could write semething like :
$("#optiontoSelect").attr("selected","selected");
More info in the jquery doc

I may have misunderstood the question. If you wish the focus to move from the input field to the select you would use .focus(). I would imagine you want to call focus on successful population of the select.
$('#select').focus();
or
$(this).focus();
jQuery Docs : focus()

This'll get you started:
<?php
$options = Array('red', 'green', 'blue');
$opt_from_db = 'green'; // for example
echo '<select name="fieldName">';
foreach ($options as $opt) {
$selected = ($opt_from_db == $opt) ? ' selected' : '';
echo "<option{$selected}>{$opt}</option>";
}
echo '</select>';
?>
The key is to write the selected attribute (in some form or another) into the programmatically-rendered HTML.

Related

Hide html value inside input field

I would like to know if it is possible to help me please.
It's a live ajax search that retrieves information in the database. This is the php code :
<?php
$key=$_GET['key'];
$array = array();
$connection=mysqli_connect("localhost","root","","visitor_signin_app");
$query = mysqli_query($connection, "SELECT * FROM visitors WHERE visitor_first_name LIKE '%{$key}%' AND visitor_visit_status = 'Signed Out'");
while($row = mysqli_fetch_assoc($query)) {
$array[] = '<span style="display:none; visibility:hidden">'.$row['visitor_id'].'</span>' . ' ' . $row['visitor_first_name'] . ' ' . $row['visitor_last_name'];
}
echo json_encode($array);
mysqli_close($connection);
?>
In the array I am trying to hide the 'visitor_id' and when I start typing the name in the input field it works perfect where it only shows the first name and last name, but once I select the name that displays in the dropdown then it inserts the
<span style="display:none; visibility:hidden">1</span>
So my main question is, would it be possible to hide the html that I dont want to display in the input field but the value needs to be added with the name that gets chosen. Any advice would be gladly appreciated. Thank you
This is the link where I received the code : https://codeforgeek.com/2014/09/ajax-search-box-php-mysql/
I would like to retrieve all the information from the registered person that gets selected in the dropdown list and add them as a new entry to the database.
Why not use the HTML inputwith type hidden ? See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden
I think it's made exactly for your purpose.
Your question is a bit confusing to understand, but if I'm correct, what you want to do is have a hidden input field, but set the value of the input field based on the value of a different input field.
To do that, you should give your hidden field a display: none style, and use JavaScript to set it's value. Something like this:
document.getElementById('inputId').value = 'your data';

Post multiple values from database, in one Option

I am looking to populate a drop down field with a list of names from a database, and when an option is selected from that drop down, it will post all its values in the row, belonging to that chosen option.
This is probably hard to describe/understand, so to help illustrate, this is my table row:
I then proceed to populate the dropdown, and associate its value to whatever the selected option is posted
//////db_conx is db connection //////main_meal is table name
<form action="#" method="post">
<?php
$dropdown = $db_conx->query("SELECT * FROM main_meal") or die ("somethings broken");
while($array[] = $dropdown->fetch_object());
//echo '<option value ="'.$record['Mname'].'">'.$record['Mname'].'"</option>';
array_pop($array);
?>
<select name="changeCal">
<?php foreach($array as $option) :?>
<!--//get chosen value in drop down, and get its calories-->
<option value="<?php echo $option->calories;?>"><?php echo $option->Mname; ?></option>
<?php endforeach; ?>
</select>
This works great for one value, such as calories, in the above code, but I need more values.
For example, if choose Healthy egg and chips, the value will post 218, as the loop only associates calories and names at the moment.
I attempted various things, like this post:How to get multiple values from a single <select> variable in HTML/PHP?
But the foreach errors.
How can I something similar to what I have done, but store multiple values from one chosen option?
Thank you
Well, I think I understood your problem, but I think that the way you want to use is not usable, maybe I recommend that you put the id in the value of the option in the < select> and then with php you can get all the data from the data base.
For me that is the best way, but if you want do it like the example that you show, you can make an string, for example:
<option value="id:5_calories:258_protein:11g">Healthy eggs & Chips</option>
with your php should look like:
echo '<option value="id:'.$option->id.'_calories:'.$option->calories.'_protein:'.$option->protein.'">'.$option->Mname.'</option>';
you can make bigger the string with others values that you want to put.
In the backend when you send the select you can catch the data with a:
$myArray = explode("_", $_POST["changeCal"]);
Then you will get an array with values like:
$myArray[0]; // id:5
$myArray[1]; // calories:258
$myArray[2]; // protein:11g
Then if you need for example the calories you can make an explode like:
$calories = explode(":",$myArray[1]);
And you will have:
$calories[0]; //calories
$calories[1]; //258 <= Here are the calories.
Maybe if you want to do it of that way, this can be the easiest way, but I recommend send the ID.
Let me know if you need more help. Regards, Have a nice day.

Wordpress - how to define cities dropdown values

I am working on an existing wordpress website.
Users has field "user-country" (actually, I do not know how this field is created in wordpress, but it works).
In the registration form, user can choose one specific country.
However, now this country list is note defined "anywhere". It is created explicitly in the code:
<option value="Afghanistan" <?php if($current_user->user_country == 'Afghanistan') echo 'selected';?>>Afghanistan</option>
<option value="Albania" <?php if($current_user->user_country == 'Albania') echo 'selected';?>>Albania</option>
<option value="Algeria" <?php if($current_user->user_country == 'Algeria') echo 'selected';?>>Algeria</option>
<option value="American Samoa" <?php if($current_user->user_country == 'American Samoa') echo 'selected';?>>American Samoa</opt
etc.
The client wants to changed this list (from country to city). So i need to add other values. I do not want to write all values in the code. I would like to create some list with these values in wp-admin.
What is the best way to create a predefined values list? And these are not custom fields for posts.
EDIT:
I want to store values in DB, so admin can modidfy these values from wp-admin.
Actually, it is not so important whether it is DB or other option like XML.
I just want this list to appear as dropdown when user is registering and also to wp-admin to modify values of this list.
Also, a question come to my mind - is it a normal practice to store user custom fields like country or city in DB? Or maybe it is ok to define them in code explicitly?
Well, if you want the administrator to be able to modify the list, then DB is likely the best option here.
I would do something like this (in WordPress):
// put a default (initial) list in the database, if there isn't one there yet
if(!get_option('my_country_list')){
// store it as a |-delimited string, because WP serializes arrays,
// and this would be too much here
$data = 'Albania|Algeria|Disneyland|etc';
update_option('my_country_list', $data);
}
Now, later where you need that list, simply get it from the db:
$countries = get_option('my_country_list');
// turn it into an array
$countries = implode('|', $countries);
// generate the select field
$html = '';
foreach($countries as $country){
$checked = '';
if($current_user->user_country == $country)
$checked = 'selected="selected"';
$html .= sprintf('<option value="%1$s" %2$s> %1$s </option>', $country, $checked);
}
printf('<select> %s </select>', $html);
I guess you'll also have some kind of administration form for the options, where the administrator can modify entries from this list. This could be a textarea. When it gets submitted you update_option() again (replace new lines with |)

How to make a drop down field (populated from mysql db) appear based on user selection

I have a form. The first field is a drop down select option. The user will select the name of a company. Based on the name a second drop down select option needs to be generated showing the addresses of all the company's branches.
The company data is stored in a mysql database.
How can I make the above happen?
If you are saying about <select> tag, the you can use this.
<select name="option">
<option value="one"<?php echo ($data["option"] == "one") ? ' selected="selected"' : ""; ?>>One</option>
<option value="two"<?php echo ($data["option"] == "two") ? ' selected="selected"' : ""; ?>>Two</option>
<option value="three"<?php echo ($data["option"] == "three") ? ' selected="selected"' : ""; ?>>Three</option>
</select>
For the sub select, see this demo: http://jsfiddle.net/ah4rx/
You have two options here
post the form on change of first dropdown and fetch the data based on posted value and display.
use ajax functionality to fetch the data based on value selected in dropdown and display
As basic as it gets - Send the company name via ajax to php, query the database to get the address, populate the next drop down in the same php script whilst in the loop and echo out. Receive back in ajax
I would use that code for the first generation of the first list. Then the second list would have to be done with AJAX (JQUERY can help you here).
You want JQUERY to get
var client=$('#client_name').find("option:selected");
Then make a normal AJAX request.
I would recomend you to use a PDO to connect to your databse instead of using that generic connection.
Based on the code that you posted in the reply to Vamsi then the code would be
<label>Client Name</label><br>
<select id="client_name">
<?php
// PDO connect to the DB
$db = new PDO ('mysql:host=localhost;dbname=DB_NAME','DB_USER','DB_PASS');
$sql = 'SELECT name FROM client'
$result = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
if(count($results)>0){ echo "<option></option>";}
foreach( $result as $key => $val)
{
// could also be $val['name'] depending on your db
echo "<option>".$key['name']."</option>";
}
?>
<select>

'Remember' form drop list value when submitting to SELF?

I know how to 'remember' some form values whenever submitting the form to itself, in this case because of a picture upload function which requires the form to be submitted to itself. I simply want it so that if the user has filled out all fields and then uploads an image, the form doesn't get resetted (cleared).
I have solved this in regular fields and checkboxes like this:
<input type="text" name="headline" id="headline" value="<?php echo #$_POST['headline'];?>">
But how can I do this with drop lists? or radio buttons? There is no value option in a 'SELECT' list, even though I have tried writing in value anyways in the SELECT statement. Didn't work!
So, how can I set the SELECT (drop down lists) value with PHP (OR JAVASCRIPT) ?
If you need more input let me know, thanks!
For selects, you need to compare each option to your posted value, and handle it individually. Simply print out your options in a loop, and test each value against the value was was previously posted. If it maches, add selected to the attributes of that particular option.
$color = $_POST["colors"];
$colors = array("red","green","blue");
<select name="colors">
<?php foreach ($colors as $option) { ?>
<option<?php print ($option == $color) ? " selected" : ""; ?>>
<?php print $option; ?>
</option>
<?php } ?>
</select>
Actually, found out that it is possible to set the selectedIndex with javascript...
So I could put the selectedIndex in a hidden input before submitting the form, and then get that selectedIndex and set it with a javascript function... tricky but suits me better in this case...
document.getElementById("select").selectedIndex=nr;
Thanks though Jonathan!

Categories