PHP Form - Highlight multiple selection for SQL query - php

I'm trying to keep multiple options selected in a dropdown form after being submitted but I can't get it to work. The options don't stay selected. I've seen answers for this using foreach() (Highlighting multiple selections on a form after submitting), however my data comes from a SQL query so I believe I need to use while() to loop through the rows. Any thoughts?
$id7 = $_REQUEST['id7'];// Interest Level
<?php
$getParameter_sql4 = 'SELECT Funds.[Interest Level] FROM Funds GROUP BY Funds.[Interest Level] ORDER BY Funds.[Interest Level]';
$getParameter4 = sqlsrv_query($conn,$getParameter_sql4);
?>
<form action="/Reports/FundStatistics.php" method="get">
<select name="id7[]" multiple size="4">
<?php while ($row4 = sqlsrv_fetch_array($getParameter4, SQLSRV_FETCH_ASSOC)) { ?>
<option <?php if (in_array($row4['Interest Level'],$id7)) { echo 'selected="selected"';}?> value="'<?php echo $row4['Interest Level']; ?>'"><?php echo $row4['Interest Level']; ?></option><?php }?>
</select>
<input type="submit" VALUE="Update" /></form>

Related

PHP Loop assistance

I am trying to find out online how to make a form loop based on a dropdown value. So for example, If the dropdown is selected on 2, it will loop a preset form twice, if the option was 3 it would display the form 3 times. This would be helpful because I am trying to get those forms into an array and then use the array to send to phpmyadmin etc.
Another question is how would I be able to get the value of each form being displayed, unless I can have the code differentiate between the forms, it might just see the values as one instead of two separate if that makes sense.
<html>
<?php
session_start();
?>
<?php include 'AdminHeader.php' ?>
<?php include '../Includes/dbh.inc.php' ?>
<body bgcolor="grey">
<div class="main">
<form method="POST">
<select name="RaceNum">
<option> Number of Horses </option>
<option> 1 </option>
<option> 2 </option>
</select><br />
Horse Number: <input type="text" name="HorseNumber"><br/>
Horse Name: <input type="text" name="HorseName">
<button type="Submit" name="submit"> Submit </button>
</form>
<?php
if(isset($_POST['submit'])) {
$RaceNum = $_POST['RaceNum'];
$HorseNumber = $_POST['HorseNumber'];
$HorseName = $_POST ['HorseName'];
while($RaceNum < 2){
$stack = array(
1 => array( 'Horse Number' => $HorseNumber, 'Horse Name' => $HorseName )
);
echo "<pre>";
print_r($stack);
echo "</pre>";
}
}
?>

How can I prevent the ID from showing in the datalist element?

I am trying to utilize the datalist element. Everything is working with 1 little hitch. The selectable list is showing 2 columns, both the street_id and street columns. I need the street_id that will be submitted but dont want the street_id to show in the datalist.
<?php
require 'connect_mysqli.php';
$sql = "SELECT * FROM streets";
$result = mysqli_query($con, $sql) or die ("Error " . mysqli_error($con));
?>
<form action="test.php" name="test" method = "post">
<datalist id="street" name="streets">
<?php while($row = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row['street_id']; ?>"><?php echo $row['street']; ?></option>
<?php
}
?>
</datalist>
<input type="text" name="street_val" id="test" autocomplete="off" list="street">
<input type="submit" value="Submit">
</form>
<?php
mysqli_close($con);
//test the output value
echo $_POST['street_val'];//
?>
You have coded a select list - which has separate values for display and returned values. In the datalist, you only need value="" for options and then it will only return that value. Also better to keep the server code and display code separate: i.e. populate or build the array in the PHP with your query, then in the HTML only display it.

populating a value field of a combo-box from a MySQL query

This question is relating to a problem that a previous questions answer yielded! (as is the way)
I have set up a simple script to access a MySQL database and populate a dropdown combobox with usernames, but it wont accept the values from my php, it creates the names however so its some form of syntax error but not one that causes a crash.
<?php
# parameters for connection to MySQL database
$hostname="";
$database="";
$ausername="";
$apassword="";
mysql_connect ("$hostname","$ausername","$apassword");
mysql_select_db ("$database");
$query = mysql_query("SELECT DISTINCT username FROM dbusers") or die(mysql_error());
?>
<div id="select_users" style="position:absolute;width:466px;height:108px;">
<form name="select_user" method="POST" action="./page5.php" id="Form1">
<div id="select_a_user" style="position:absolute;left:0px;top:20px;width:100px;height:20px;z-index:11;text- align:left;">
<span style="color:#000000;font-family:Arial;font-size:15px;">Select User</span></div>
<select name="users[]" multiple = "multiple" id="users" style="position:absolute;left:93px;top:15px;width:200px;height:75px;z-index:12;">
<option value="test"> Select a user</option>
<?php
while($row = mysql_fetch_assoc($query)){
$username = $row["username"];
?>
<option value= <?php $username ?> > <?php echo $username ?> </option>
<?php
}?>
</select>
<input type="submit" id="Button1" name="" value="Submit" style="position:absolute;left:93px;top:100px;width:96px;height:25px;z-index:13;">
</form>
</div>
the section
<option value= <?php $username ?> > <?php echo $username ?> </option>"
is what is causing the issue i think as its not assigning the variable value
Im using the following script to debug and display what is supposed to be inside the array after POST
<?php
if(isset($_POST['users']))
{
$ausers = $_POST['users'];
if(!isset($ausers))
{
echo("<p>You didn't select any users!</p>\n");
}
else
{
$nusers = count($ausers);
echo("<p>You selected $nusers user: ");
for($i=0; $i < $nusers; $i++)
{
echo($ausers[$i] . " ");
}
echo("</p>");
}
}
?>
It will output the number of data files in the array but wont display their content, which after force echo'ing the variables has lead me to believe that the values of the array are all empty.
The end result of this selection is to store each entry of the array as a variable for another MySQL query if that any help with the correct code to achieve the result.
Thanks!
<option value= <?php $username ?>
^----you forgot 'echo' here
No echo, no output, and your generated html becomes
<option value=>foo</option>
I am not sure what the issue is here, sometimes when dealing with a database though it will create a multidimensional array. If this is the case you aren't actually looking at any values. To help you check try doing a var_dump on the variable to see everything about the data. It will even tell you if it is truly empty it should look like this:
echo var_dump($varName);
That should help you with debugging and maybe help you post some better information.

List option values fail to insert into the database

I've created a php form to insert values into a database.
One of my form options is a dynamic list populated with fields from another table.
I first created the form without the dynamic option, and all data inserted just fine (and still does).
Now I'm attempting to include the code below, and while it displays the option values properly, the value fails to insert. Any advice?
<?php
/*
* LIST ALL CATEGORIES
****************************************/
include('../dbconnection.php');
$query = 'SELECT category_id, category_name FROM ingredient_categories';
$result = mysql_query($query);
echo '<select>';
while ($ingredientCategoryOption = mysql_fetch_array($result)) {
echo '<option value="'.$ingredientCategoryOption[category_id].'">'.$ingredientCategoryOption[category_name].'</option>';
}
echo '</select>';
?>
I had created something similar yesterday. The $polls array is passed to the view in CodeIgniter in the $this->load->view('poll.php', $data['polls']), while you do it in the page itself. However, you can have the general idea.
<FORM id="formPoll" class="question" name="createpoll" action="<?php echo base_url()?>index.php/poll/selectOption/" method="POST">
<select name="poll_list">
<?php
foreach($polls as $poll){
echo "<option name='poll_table'>$poll->name</option>";
}
?>
</select>
<div id="input">
Poll name: <input type="text" name="name"></input>
Title: <input type="text" name="title"></input>
</div>
<div id="options">
Option: <input type="text" name="option"></input>
</div>
<input type="submit"></input>
</FORM>
Some ideas:
Check if $results is not empty before using it
Give your <select> form a name, as I showed above
Check if $ingredientCategoryOption is not null or is something returned.
Check your database connection

Related select inputs

Can anybody help me on this. For showing the rezults from an MySql database i have to select school, class, subject, and exam. But listing all the classes or all the exams is not very practical so i want to do another function that when i choose some schools in the first select box it shows me in the second select box only the classes of the selected schools.
My code is:
<div id="allselects">
<form action="viewing.php" method="post" name="filt">
<div class="multsarrange">
<h1 class="choosetext" >Chose Schools</h1>
<select class="multipleselect" name="schools[]" size="8" multiple="multiple" id="shkll">
<?php
$sql = "SELECT * FROM schools ";
$scc=mysql_query($sql);
while ($db_f = mysql_fetch_assoc($scc)) {
$schcd=$db_f['schoolcode'];
$schc=$db_f['schoolname'];
echo "<option value=$schcd >$schc</option>";
}
?>
</select>
</div>
<div class="multsarrange" id="clasaajax">
<h1 class="choosetext" >Chose an Classes</h1>
<select class="multipleselect" name="classes[]" size="8" multiple="multiple" ">
<?php
$c = "SELECT * FROM classes ";
$cl=mysql_query($c);
while ($db_f = mysql_fetch_assoc($cl)) {
$clsc=$db_f['schoolID'];
$claid=$db_f['classID'];
$clay=$db_f['year'];
$clanm=$db_f['className'];
$name=schoolidton($clsc)." ".$clay." ".$clanm;
echo "<option value=$claid >$name</option>";
}
?>
</select>
</div>
<div class="multsarrange">
<h1 class="choosetext" >Chose Subjects</h1>
<select class="multipleselect" name="subjects[]" size="8" multiple="multiple">
<?php
$sb = "SELECT * FROM subjects ";
$sbi=mysql_query($sb);
while ($db_f = mysql_fetch_assoc($sbi)) {
$sbnm=$db_f['subjectName'];
$sbid=$db_f['subjectID'];
echo "<option value=$sbid >$sbnm</option>";
}
?>
</select>
</div>
<div class="multsarrange">
<h1 class="choosetext" >Chose Exams</h1>
<select class="multipleselect" name="exams[]" size="8" multiple="multiple">
<?php
$e = "SELECT * FROM exams ";
$ex=mysql_query($e);
while ($db_f = mysql_fetch_assoc($ex)) {
$id=$db_f['examID'];
$sub=$db_f['subjectID'];
$desc=$db_f['description'];
$year=$db_f['year'];
$data=$db_f['data'];
$exnam=subidton($sub)." - ".$year." - ".$desc." - ".$data;
echo "<option value=$id >$exnam</option>";
}
?>
</select>
</div>
<div id="longsubmit">
</br></br>
<input name="submit" type="submit" value="View" />
</div>
</form>
</div>
What you need to do is the following :
Setup an event listener on the select to listen for the change event - see here
Process the change event by sending the selected value to the PHP script - see here
Using PHP get the selected value and query the database as required (you already do that bit)
Send the associated HTML output or JSON or XML if you just creating a new select list - this is a simple echo
Write the output to the screen using JavaScript - this is either creating a new element based on the reply from the PHP function or inserting the HTML response
There are lots of things within this process - each with multiple options - i suggest you attempt to tackle each one and come back with specific questions if you get stuck
use ajax on abc.php get values or ids of school and make the tags you need and return back the results to the relevant id of html
function get_options(table,id_field,name_field,where_field,field_value,select_id,edit_id){
$('#'+select_id).html('<option>Loading...</option>');
$.ajax({
type: "POST", url: "abc.php", data: "&table="+table+"&id_field="+id_field+"&name_field="+name_field+"&where_field="+where_field+"&field_value="+field_value+ "&edit_id=" + edit_id +"&get_option=1",
complete: function(data){
//alert(data.responseText);
$('#'+select_id).html(data.responseText);
}
});
}
You have to use AJAX inorder to achieve this.
check this
http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php

Categories