Display drowdown values after user has selected 3 option - php

I have 3 drop downs I want to display whatever the user will select after he/she has selected a function or a scrpt will do but it must be within the script
<?php
$resource_names = mysql_query("SELECT DISTINCT NAME FROM selections ORDER BY id ASC");
$names = array();
while($row = mysql_fetch_row($resource_names)){
$names[] = $row[0]
}
$resource_surnames = mysql_query("SELECT DISTINCT SURNAME FROM selections ORDER BY id ASC");
$surnames = array();
while($row = mysql_fetch_row($resource_surnames)){
$surnames[] = $row[0];
}
$resource_emails = mysql_query("SELECT DISTINCT EMAIL FROM selections ORDER BY id ASC");
$emails = array();
while($row = mysql_fetch_row($resource_emails)){
$emails[] = $row[0];
}
if(count($emails) <= 0 || count($surnames) <= 0 || count($emails) <= 0){
echo 'No results have been found.';
} else {
// Display form
echo '<form name="form" method="post" action="test.php">';
//Names dropdown:
echo '<select name="id" id="names">';
foreach($names as $name) echo "<option id='$name'>$name</option>";
echo '</select>';
//Surnames dropdown
echo '<select name="id" id="surnames">';
foreach($surnames as $surname) echo "<option id='$surname'>$surname</option>";
echo '</select>';
//Emails dropdown
echo '<select name="id" id="emails">';
foreach($emails as $email) echo "<option id='$email'>$email</option>";
echo '</select>';
echo "<button id='write_in_div'>Click me!</button>";
echo '</form>';
}
?>
Something that will call the write_in_div When Click me! button is press or any other method that can be used to display 3 selection user selected
The Output should be something like You select 1) Name 2)Surname and Email

You have an error in your html selects each select has the same name "id" they each need to be unique so you can detect then.
You need to detect if the user has submitted the form
if(isset($_POST["select_name"])) {
echo $_POST["select_name"];
}

There is a big mistake on you form.
In a form, each select and input MUST have a unique name. You need this name to retrieve the submitted value back in your php script.
I suppose you have this:
<?php
$resource_names = mysql_query("SELECT DISTINCT NAME FROM selections ORDER BY id ASC");
$names = array();
while($row = mysql_fetch_row($resource_names)){
$names[] = $row[0]
}
$resource_surnames = mysql_query("SELECT DISTINCT SURNAME FROM selections ORDER BY id ASC");
$surnames = array();
while($row = mysql_fetch_row($resource_surnames)){
$surnames[] = $row[0];
}
$resource_emails = mysql_query("SELECT DISTINCT EMAIL FROM selections ORDER BY id ASC");
$emails = array();
while($row = mysql_fetch_row($resource_emails)){
$emails[] = $row[0];
}
if(count($emails) <= 0 || count($surnames) <= 0 || count($emails) <= 0){
echo 'No results have been found.';
} else {
// Display form
echo '<form method="post" action="test.php">';
//Names dropdown:
echo '<select name="names">';
foreach($names as $name) echo "<option id='$name'>$name</option>";
echo '</select>';
//Surnames dropdown
echo '<select name="surnames">';
foreach($surnames as $surname) echo "<option id='$surname'>$surname</option>";
echo '</select>';
//Emails dropdown
echo '<select name="emails">';
foreach($emails as $email) echo "<option id='$email'>$email</option>";
echo '</select>';
echo '<button id="write_in_div">Click me!</button>';
echo '</form>';
}
When the form is submitted, test.php will have the posted data: $_REQUEST['names'], $_REQUEST['surnames'] and $_REQUEST['emails'].
You just have to check the content of thoses vars and print them if not null.
Note1: ?> is useless at the end of a file.
Note2: be carefull about ' and " when writing an html file. The value of an html attribute is written with ", not '.

Related

Populate dropdown from database and set default value

Right now I have a working solution for populating an HTML <select>/<option>-dropdown with the content through PHP/MYSQLI from my database: listoption.
The database:
DATABASE NAME:
# listoption
TABLES:
# ID INT(11) *Primary AI
# listoption_item VARCHAR(255)
Here's the other code (not the mysqli connect but everything afterwards..)
<?php
$result = $mysqli->query("select * from listoption");
echo "<select id='list' name='list'>";
while ($row = $result->fetch_assoc()) {
$listoption_item = $row['listoption_item'];
echo '<option value="'.$listoption_item.'">'.$listoption_item.'</option>';
}
echo "</select>";
?>
But the problem is now that I want to have one of these options that are populated through that query to be selected. And the option to be selected should be determed by a parameter in the URL, for example: index.php?id=1.
So now I need to somehow add a IF/ELSE and a $_GET['id']; into the code to make it identify if the ID from the database is the same as the populated item and then set it to selected.
Any idéas? Thanks!
You can do that like given below:
<?php
$result = $mysqli->query("select * from listoption");
$id = ($_GET['id'])? $_GET['id'] : '';
echo "<select id='list' name='list'>";
while ($row = $result->fetch_assoc()) {
$listoption_item = $row['listoption_item'];
$sel = ($id == $row['id'])? 'selected="selected"':'';
echo '<option value="'.$listoption_item.'" '.$sel.'>'.$listoption_item.'</option>'; // $sel will deside when to set `selected`
}
echo "</select>";
?>
You can rewrite the code as follows:
<?php
$id = $_GET['id'];
$select = "";
$result = $mysqli->query("select * from listoption");
echo "<select id='list' name='list'>";
while ($row = $result->fetch_assoc()) {
$row_id = $row['ID'];
if($row_id == $id){
$select = "selected";
}
$listoption_item = $row['listoption_item'];
echo '<option value="'.$listoption_item.'" selected="'.$select.'">'.$listoption_item.'</option>';
}
echo "</select>";
?>
Use the following code:-
<?php
$selectedId = isset($_GET['id'])?$_GET['id']:0;
$result = $mysqli->query("select * from listoption");
echo "<select id='list' name='list'>";
while ($row = $result->fetch_assoc()) {
$listoption_item = $row['listoption_item'];
echo '<option value="'.$listoption_item.' .(($selectedId>0)?:" selected ":"").'">'.$listoption_item.'</option>';
}
echo "</select>";
?>

Php query based on selected/default dropdown and output on same page

I have managed to create dropdown from Mysql column and also get query result with get method but here webpage is directed to other page when hit button.
I am looking to get query output with some default option set in dropdown when page loads or want query result on same page reloading it again when user changes option.
Any help will be appreciated.
Code on main page for dropdown:
$result = $conn->query("SELECT DISTINCT nx_version FROM workflow1 ORDER BY id");
echo "<form action='process.php' method='get'>";
echo "<html>";
echo "<body>";
echo "<p></p>";
echo "<center>";
echo "<strong> Select Base Verison To Compare With : </strong>";
echo "<select name=nx_version>";
while ($row = $result->fetch_assoc()) {
unset($nx_version);
$nx_version = $row['nx_version'];
echo '<option value>'.$nx_version.'</option>';
}
echo "</select>";
echo " <button type='submit'>See items</button>";
echo "</center>";
echo "</body>";
echo "</html>";
echo "<p></p>";
echo "<form>";
Code I wrote when hit button and gives query output (in process.php):
$nx_version = $_GET['nx_version']; // The name attribute of the select
$query = "SELECT step1 FROM workflow1 WHERE nx_version = '$nx_version' ORDER BY id DESC";
$query1 = mysqli_query($conn, $query);
$array = Array();
while($result1 = $query1->fetch_assoc()) {
$array[] = $result1['step1'];
}
print_r($array);
process.php file should be like this -
<?php
session_start();
$nx_version = $_GET['nx_version']; // The name attribute of the select
$query = "SELECT step1 FROM workflow1 WHERE nx_version = '$nx_version' ORDER BY id DESC";
$query1 = mysqli_query($conn, $query);
$array = Array();
while($result1 = $query1->fetch_assoc()){
$array[] = $result1['step1'];
}
$_SESSION['data'] = $array;
// storing the data as session
header("location:main_page.php");
?>
Now get back the data from session in your main page by adding this-
$array = $_SESSION['data'];

How to insert multiple rows by select or checkbox

I've got a problem with inserting multiple row to one table.
I've got a 3 tables:
1. student with id_student
2. ankieta with id_ankieta
3. student_ankieta with id_student and id_ankieta
I want to choose students from database using select or checkbox and choose one id_ankieta. After confirming, there are rows created in table (student_ankieta).
Now I can choose students but when I confirm, only one student gets added to the database.
Can anyone help me corect the code?
<?php
echo'<form method="post" action="student_ankieta.php">
<div class="box" style="margin:0 auto; top:0px;">
<h1>Student - ankieta:</h1>
<label>
<span><br/>Ankieta:</span>
</label>
<select class="wpis" name="id_ankieta">
<option>wybierz ankiete</option>';
$query = "SELECT * FROM ankieta";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo '<option value="'.$row['id_ankieta'].'">' . $row{'rok_akademicki'}.' '. $row{'semestr_akademicki'}.' '.$row{'active_ankieta'} .'</option>';
}
echo '
</select>';
$query = "SELECT * FROM student";
$result = mysql_query($query);
echo'
<label>
<span><br/>Wybierz stundentów:</span>
</label>
<select multiple="multiple" name="id_student[]" size="10">';
while ($row = mysql_fetch_assoc($result))
{
echo '<option class="wpis" value="'.$row['id_student'].'" />'.$row{'pesel'}.' '. $row{'nazwisko'}.' '.$row{'imie'} .'</option>';
}
echo'<br/><input class="button" type="submit" value="Dodaj ankiete" name="dodaja">';
if(isset($_POST['dodaja']))
{
$id_ankieta = $_POST['id_ankieta'];
if(empty($_POST['id_ankieta']))
{
echo '<p style="margin-top:10px; font-size:75%; font-family: Calibri; color: red; text-align:center;">Musisz wypełnić wszystkie pola.</p>';
}
else
{
$id_student = $_POST['id_student'];
for ($i = 0; $i < count($id_student); $i++)
{
$id_student = $id_student[$i];
mysql_query("INSERT INTO student_ankieta (id_student, id_ankieta) VALUES ('" . $id_student . "','$id_ankieta')");
}
}
}
echo'</div></form>';?>
Put all students in to an array with the key = id_student
$query = "SELECT * FROM ankieta";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
students[$row['id_student']] = array($row['pesel'],$row['nazwisko'],$row['imie'];
}
If the form was posted the confirm will = 1 (from hidden input)
When first enter script "confirm will = 0
When zero, display all student with a check box with a name which includes the id_student in the format of n-i_student.
if intval($_POST['confirm']) = 0){
echo '<form action = "confirm.php" method="post"><input type="hidden" name="confirm" value="1"/><table>';
foreach ($students as $id => val){
echo "<tr><td><input type=\"checkbox\" name=\"n-$id\" value=\"1\" /> Select </div></td>$val[0]<td>$val[0]</td><td>$val[1]</td><td>$val[2]</td></tr>";
}
echo '</table></form>';
}
When confirm = 1
The checkboxes that were checked are inserted.
Check each post value for a key the starts with "n-"
get the rest of the key value after the n- for the id_student value.
Still 1 Major Problem, I do not know where to get the $id_ankieta'
And match it with the id_student.
I left that value as $val[???]
elseif intval($_POST['confirm']) = 1){
foreach ($_POST as $k =>$val){
if (inval($val) == 1 && substr($k,0,2) == 'n-'){
$id = substr($k,2);
$sql = mysql_query("INSERT INTO student_ankieta (id_student, id_ankieta) VALUES ('$id','" . $students[$id][$val[???]] . "')");
}
}
}

Output of autocomplete populated from mysql tables

My textboxes get autocomplete populated from mysql tables.
I want to display the output list from the textbox into a selectable option instead of an list item.
echo '<li onClick="fill(\''.$result->naam_klant.'\');">'.$result->naam_klant.'</li>';
My code so far:
'<select onClick="fill(\''.$result->naam_klant.'\');"><option value=$result->naam_klant></option></select>';
Can you guys help me with this ?
UPDATE
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
// Is the string length greater than 0?
if(strlen($queryString) >0) {
$query = $db->query("SELECT naam_klant FROM overboekingen WHERE naam_klant LIKE '$queryString%' LIMIT 10");
if($query) {
while ($result = $query ->fetch_object()) {
echo '<li onClick="fill(\''.$result->naam_klant.'\');">'.$result->naam_klant.'</li>';
'<select onClick="fill(\''.$result->naam_klant.'\');"><option value=$result->naam_klant></option></select>';
}
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
} // There is a queryString.
} else {
echo 'There should be no direct access to this naam_klant script!';
}
}
?>
You don't give enough information, how you is your results structured?
<select onchange="fill(this.value);" onfocus="this.selectedIndex = -1;">
<?
for ($i=0; $i < count(results); $i++) {
$result = results[i];
echo "<option value='{$result->naam_klant}'>option {$result->naam_klant}</option>\r\n";
}
?>
</select>
Update
Since you want a selectable option instead of an list item
if ($query) {
echo '<select onchange="fill(this.value);" onfocus="this.selectedIndex = -1;">\r\n';
while ($result = $query->fetch_object()) {
echo '<option value={$result->naam_klant}>{$result->naam_klant}</option>\r\n';
}
echo '</select>\r\n';
}

Retaining value of query generated dropdown box after submission

I populated the options for a dropdown box using the results of a query. How do I retain the selected value after the user submits?
Here's the code:
$query="SELECT trainingName,trainingID FROM training ORDER BY trainingName";
$result = mysql_query ($query);
echo "<select name='training' value=selected>Training Name</option>";
$training = strip_tags(#$_POST['training']);
echo "<option>---------------------Select---------------------</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[trainingID]>$nt[trainingName]</option>";
}
Thanks!
Try this:
$query="SELECT trainingName,trainingID FROM training ORDER BY trainingName";
$result = mysql_query ($query);
echo "<select name='training'>";
echo "<option>---------------------Select---------------------</option>";
while($nt=mysql_fetch_array($result)){
$selected = false;
// check if the current value equals the value submited
if($_POST['training'] == $nt['trainingID']){
$selected = true;
}
// show selected attribute only if $selected is true
echo "<option value='{$nt['trainingID']}' ". ($selected ? "selected" : "") .">{$nt['trainingName']}</option>";
}
echo '</select>';

Categories