I'm back and this one might be a little tricky but we will find out! :D
Okay I am using php to self populate my select fields so I don't have to continuously update the options when new agents join. I am using ajax (javascript) for this part of the page and I have had no problems with this except with the company select field.
Live site
All my select fields work with out any problems but the Company field which will display nothing only on companies that seem to have "&" in the name. (Two companies still work with "&" in the name)
List of non-functioning Companies:
-Anthem
-Bogart
-Burnham
-Church Insurance
-Fawcett
-JRM
-Kenneth B. Brown
-Newton
-Sam F.
-Sherrill
-Wallace & Turner
PHP Company select code: (The if/else statement in the companies field would ideally be if ($row['Company'] !== NULL) {
echo '<option value="'.$row['Company'].'">'.$row['Company'].'</option>';
} but for some reason it will echo in a blank space in the option dropdown.)
<label for="company">Company</label><br />
<select id="company" name="users" onChange="showUser(this.value)">
<?php include 'login.php';
$result = mysqli_query($con, "SELECT DISTINCT Company FROM `roster` ORDER BY Company ASC;");
echo '<option value="">' . 'Select a Company' .'</option>';
while ($row = mysqli_fetch_array($result)) {
if ($row['Company'] == NULL) {
}
else {
echo '<option value="'.$row['Company'].'">'.$row['Company'].'</option>';
}
}
?>
</select>
PHP for other select fields:
<label for="last">Last Name</label><br />
<select id="last" name="Last_Name" onChange="showUser(this.value)">
<?php include 'login.php';
$result = mysqli_query($con, "SELECT DISTINCT Last_Name FROM `roster` ORDER BY Last_Name ASC;");
echo '<option value="">' . 'Select an Agent' .'</option>';
while ($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['Last_Name'].'">'.$row['Last_Name'].'</option>';
}
?>
</select>
Any Ideas on this one? If you need to see the process.php page which echos in the results then just ask! Thanks a million to anyone who helps out.
How the HTML looks when populated through php:
<select id="company" name="users" onchange="showUser(this.value)">
<option value="">Select a Company</option><option value="A.R.T. Group">A.R.T. Group</option><option value="ALPHA Benefits">ALPHA Benefits</option>
</select>
I only included a few since 80ish of them is one massive line of html.
You need to urlencode the parameter that is being passed via ajax. The ampersand is telling PHP that $_GET['q'] is complete and a new $_GET variable is starting.
Notice the %26 for the ampersand returns the desired result.
http://healthbenefitsohio.com/process.php?q=Anthem%20Blue%20Cross%20%26%20Blue%20Shield
Your company options should look like this:
echo '<option value="'.urlencode($row['Company']).'">'.$row['Company'].'</option>';
For good measure, I would also encode the display
echo '<option value="'.urlencode($row['Company']).'">'.htmlspecialchars($row['Company'],ENT_QUOTES).'</option>';
Related
I have a form with a multi select which presents the user with a list of groups to chose from,which is then saved in the database. My aim is that when the user wants to edit his information, the groups saved in the database to be selected and the others not to be selected. Please, see my code and advise.
I populate the saved groups to a variable
$groups = array($row['groups']); // This outputs the groupId from the db eg 1,2,3,6
Populate groups to a multi select
<div class="form-group">
<!-- <label>Church Groups: </label> -->
<select name="groups[]" class="form-control mandatory" id="groups" multiple="multiple" >
<?php
$sql="SELECT ID,UCASE(groupName) AS groupName FROM tblgroups WHERE (congregationId=?)";
$stmt=mysqli_stmt_init($con);
mysqli_stmt_prepare($stmt,$sql);
mysqli_stmt_bind_param($stmt,'i',$congId);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_array($result)) {
$selected = "";
if(in_array($row['ID'],$groups)){
$selected = "selected";
}
echo '<option value='.$row['ID'].' '.$selected.'>'.$row['groupName'].'</option>';
}
?>
</select>
Thats my code but it doesn't work. If the first group in the list was saved, only that group is selected all others remain unselected whether in the db or not.
<div class="form-group">
<!-- <label>Church Groups: </label> -->
<select name="groups[]" class="form-control mandatory" id="groups" multiple="multiple" >
<?php
$sql="SELECT ID,UCASE(groupName) AS groupName FROM tblgroups WHERE (congregationId=?)";
$stmt=mysqli_stmt_init($con);
mysqli_stmt_prepare($stmt,$sql);
mysqli_stmt_bind_param($stmt,'i',$congId);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_array($result)) {
$selected = "";
if(in_array($row['ID'],$groups)){
$selected = "selected";
}
echo '<option value='.$row['ID'].' '.$selected.'>'.$row['groupName'].'</option>';
}
?>
</select>
Using your code, I have uncommented the line you commented out and introduced a variable that will be blank if the value is not in the group, otherwise it will have the value "selected", which achieves the same functionality as selected = "selelected"
First of all, assuming that your $result element is correct, you need something like this.
<?php
while ($row = mysqli_fetch_array($result)) {
$isSel = in_array($row['ID'],$groups)? "selected" : "";
echo "<option value=\"".$row['ID']."\" $isSel >".$row['groupName']."</option>";
}
?>
It is just a matter of order and linearity:
Populate a $isSel variable that contains the selected string only when your conditions are met
Print all the options, and always append $isSel. It will just contain selected only when it is required
But there are other two issues preventing your drop-down menu to be shown as expected:
Remove the square brackets [] from the select name. They are preventing the correct parsering of the multiple attribute
You don't need to specify multiple="multiple". Just multiple is enough (see here). You did also the same mistake with selected="selected": just selected is enough
So, your <select> tag definition becomes
<select name="groupsABC" class="form-control mandatory" id="groups" multiple >
instead of
<select name="groups[]" class="form-control mandatory" id="groups" multiple="multiple" >
I am new to PHP and trying to make dynamic list box using SQL
<label> Select Sport</label>
<select name = "Sport">
<option value = "">Select Sport</option>
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"></option>
<?php
}
?>
</select>
But its printing BLANK space
Try this:
<select name="">
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"><?php echo $Sport['Sport']; ?></option> // The value between <option> value </option> is the value which is to be shown in the dropdown, without this it is showing blank
<?php
}
</select>
You probably shouldn't use SELECT *, but since you're learning, I digress. Also, I personally have a hard time reading a lot of opening and closing php tags scattered throughout plus they often give weird results than what you're expecting, so this is how I'd write it.
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $sport)
{
echo "<option value =" . $sport['Sport'] . ">" . $sport['Sport'] . "</option>";
}
?>
The blank spaces indicates that you are actually hitting the loop and iterating, but the values for that array item are null. Are you sure the field name isn't 'sport' instead of 'Sport'?
So I am currently having some difficulty using PHP to populate a select option on an HTML form. I need to have the select options filled with the data from my sql table and then I need to populate the selections into a different table in sql. First I will desribe the sql table that I am using. This is my first time using PHP and SQL so please keep that in mind if the code is completely off.
Note, I am able to connect to my database without any issues
SQL Data:
Table Name = "All Animals"
Column Name = "Group"
PHP Code:
$query = "SELECT *, dbo.All Animal.Group as allAnimal_Group
LEFT JOIN dbo.All Animal on dbo.Response.animalGroup = dbo.All Animal.Group
WHERE dbo.Response.animalGroup = %s";
$db->query($query, $Group);
$r = $db->fetch();
HTML and PHP Code:
<p>
<label for="animalGroup">Type</label>
<select name="Group" id="Group">
<option>Select a type:</option></select></p>
<?php
foreach($Group as $m)
{
?>
<option value="<?php echo $m['Group'];?>"><?php echo $m['animalGroup'];?></option>
<?php
}
?>
Thank you for looking and helping, I appreciate the assistance!
Your drop down list is closed before the php code. So edit it as below first.
<p>
<label for="animalGroup">Type</label>
<select name="Group" id="Group">
<option>Select a type:</option>
<?php
foreach($Group as $m)
{
?>
<option value="<?php echo $m['Group'];?>"><?php echo $m['animalGroup'];?></option>
<?php
}
?>
</select></p>
For the second part of your question, put this code inside html form and submit it to the server once the selection is made. So the complete code will be
<p>
<form action="your_file_name.php" method="post">
<label for="animalGroup">Type</label>
<select name="Group" id="Group">
<option>Select a type:</option>
<?php
foreach($Group as $m)
{
?>
<option value="<?php echo $m['Group'];?>"><?php echo $m['animalGroup'];?></option>
<?php
}
?>
</select>
<input type="submit" name="submitBttn" value="Submit">
</form></p>
in your_file_name.php file, write this to retrieve what is selected as,
$_POST['Group']
Then you can write sql code like "INSERT INTO table_name (column_name) VALUES ($_POST['Group'])"
More on html forms read this... http://php.net/manual/en/tutorial.forms.php
Update your query to use LIKE instead of = sign for wild character matching
$query = "SELECT *, dbo.'All Animals'.Group as allAnimal_Group
FROM dbo.'All Animals'
LEFT JOIN dbo.'All Animals' on dbo.Response.animalGroup = dbo.'All Animals'.Group
WHERE dbo.Response.animalGroup LIKE '%s'";
P.S You should change the foreach to use $r as iterator because you are storing database result in $r and not in $Group
foreach($r as $m){
}
< input id="cc" class="easyui-combobox" name="dept"
data-options="valueField:'id',textField:'text',url:'get_data.php'">
This is a code used in creating a jquery easyui combobox. Can anyone help me with how to write the php code in get_data.php. That means for example lets say there is a table called DEPT with two colums dep_id and dep_name.I want to diplay dep_name in the combobox and valueField to be dep_id.
I use this all the time, and there is no call to a seperate php file to get the results:
<label for="cc">Name of DDB here</label>
<select id="cc" name="cc" style="width: 300px">
<option value="">--Select ?--</option>
<?php
$query = "SELECT dep_id, dep_name FROM YOUR TABLE HERE ORDER BY dep_name";
$result = $conn->query($query);
while ($row = $result->fetch_assoc()) {
echo '<option value="' . $row['dep_id'] . '" >' . $row['dep_name'] . '</option>';
}
?>
</select>
and all of this goes right where you want your drop down box.
I've got a table that populates data from a MYSQL database and populates a drop-down menu from the same database. I have the drop down menu and table just fine, I would like to be able to choose which data I show in the table however.
<select name = 'peer-id' method='post' style = 'position: relative'>
<?php
while ($content = mysql_fetch_array($peer)) {
echo "<option value='" . $content['Peer'] . "'>" . $content['Peer'] . "</option>";
}
$results = mysql_query("SELECT Destination FROM rate ");
?>
</select>
That's what I have for the select box. How can I get the choice from that and save that as a variable and refresh the table data?
I need to clarify that this will change that current data
#Data#Data#Data
#Data#Data#Data
#Data#Data#Data
Then choose drop down choice and I want it to show new data
#Data2#Data2#Data2
#Data2#Data2#Data2
#Data2#Data2#Data2
So it's going to need to load a new page or refresh some how because it's changing via PHP and not javascript.
I think form may be better, for example
<form id="myform" method="post">
<select name = 'peer-id' style = 'position: relative' onchange="change()">
<option value="1">12</option>
<option value="2">15</option>
<option value="3">16</option>
<option value="4">18</option>
</select>
</form>
<script>
function change(){
document.getElementById("myform").submit();
}
</script>
In the above code, whenever you change the value of select, it will post to the backend, then according to the posted value, you can do want you want, to get the peer-id in php, you can use the following code
$peer-id = $_POST['peer-id'];
Hope helps!
apply this code in select tag hope this works
<select onchange="location = this.options[this.selectedIndex].value;" style="text-decoration:none;">
<option value="customers.php"></font></option>
</select>
insted of the static options, you can do it like this :) here you get all the options from the database. Just replace it with the static options
$peer = mysql_query("SELECT Peer FROM rate Group By Peer Where peer = 'variable'");
$result_peer = mysql_query($peer);
if($result_peer){
while($row_peer = mysql_fetch_array($result_peer)){
echo'<option value='.$row_peer['Peer'].'>'.$row_peer['Peer'].'</option>';
}
I agree in using form, and with this you can echo back onto the page with a submit button (code tested):
<form id="myForm" method="POST">
<select name="select" onchange="<?php echo $_SERVER['PHP_SELF'];?>">
<option value="N">No</option>
<option value="Y">Yes</option>
</select>
<input type="submit" name="formSubmit" value="Submit" >
</form>
<?php
if(isset($_POST['formSubmit']) ){
$var = $_POST['select'];
$query = "SELECT * FROM table_name WHERE DesiredField='$var'";
$result = mysql_query($query)
or die(mysql_error());
while($row = mysql_fetch_array($result)){
$var2 = $row['FieldName'];
echo "First field: " . $var2 . "<br>";
// and so on for what you want to echo out
}
}
?>