Php drop down menu - php

i am using following code to insert data into mysql database
<?php
$refselect = $_POST['refselect'];
$refname = $_POST['refname'];
$refemail = $_POST['refemail'];
$refcnt = $_POST['refcnt'];
$refdes = $_POST['refdes'];
$referror = $cberror = "<h1>Data not Added</h1><br/><br/><h3>Please Follow The Instructions</h3>";
$urefdb = "INSERT INTO refdb(reftype,refname,refemail,refcnt,refdes) VALUES ('$refselect','$refname','$refemail','$refcnt','$refdes');";
include_once("db.php");
if ($refselect == "Select Type") die ("$referror");
if (empty ($refname)) die ("$referror");
if (mysql_query("$urefdb"))
{
echo "<h3>One Record Updated Successfully with the following Details </h3><br/>";
echo "Reference Type =$refselect <br/><br/>";
echo "Reference Name = $refname <br/><br/>";
echo "Reference E-Mail = $refemail <br/><br/>";
echo "Reference Description = $refdes <br/><br/>";
}
else
{
echo mysql_error();
}
?>
"refselect" data is coming from a drop down menu at the page now i want that as i add data through this form another form located at another page pic "refname" from database as i update "refdb" that drop down menu pick data accordingly
what to do now ?

You have to fill the dropdown menu on that another page using MySQL select like:
$request = mysql_query("SELECT refselect FROM refdb");
then iterate through all the results to fill your combobox:
echo "<SELECT>";
while ($drow = mysql_fetch_assoc($request))
{
echo "<OPTION>" . $drow['refselect'] . "</OPTION>";
}
echo "</SELECT>";
Or something like this

Related

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'];

Print selected value from dynamic dropdown php mysql

I have the below code where I can get dynamic value into drop down list from mysql db but i can't print selected value when i click on submit button.
can anyone help me urgntly ?
<?php
include("includes/config.inc.php");
$query = "SELECT * FROM category";
$result = mysql_query ($query);
echo "<select class='turnintodropdown' name='CategoryID' ><option value=''>All</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['CategoryID'].">".$r['CategoryName']."</option>";
}
echo "</select>";
if (isset($_POST['submit'])) {
$selected_val = $_POST['CategoryID']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
?>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
If you want to preselect the selected value of the then you can use the following code. Also check your form method attrivute to see if it set to post (note that mysql_* functions are deprecated, it's better to use PDO using prepared statements).
while($r = mysql_fetch_array($result)) {
if (!empty($_POST['CategoryID']) && $_POST['CategoryID'] == $r['CategoryID']) {
$selected = 'selected="selected"';
} else {
$selected = '';
}
echo "<option ".$selected." value=".$r['CategoryID'].">".$r['CategoryName']."</option>";
}
from above:
echo "you have selected :" . $selected_val;
from what you are echoing, based on what I'm exp[laining], it echos the ID, not the name of the category.

Search results displaying all information in table

I am trying to search some data from a database and display it. However whenever I click on 'search' all the results in the table are displayed. Any way I can display only the information the user is searching?
This is the html code for the form.
<h2> Search </h2>
<form action = "search.php" method = "post" >
Search for: <input type = "text" name ="find" placeholder="Animal Type/Date"><span class="required"</span> in
<select NAME = "field">
<Option VALUE = "Animal Type"> Animal Type</option>
<Option VALUE = "dateseen"> Date Required</option>
</Select>
<input type= "hidden" name = "searching" value ="yes"/>
<input type= "submit" name = "search" value ="Search"/>
</form>
This is the PHP code I'm using.
$link=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysqli_select_db($link,"AnimalTracker1");
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysqli_error($link));
}
//$find = strtoupper($find);
//$find = strip_tags($find);
//$find = trim($find);
$sql=mysqli_query($link, "Select * FROM Locations ");
if ($sql == FALSE)
{
die($sql." Error on query: ".mysqli_error($link));
}
while($result = mysqli_fetch_array($sql))
{
echo $result ['Animal Type'];
echo "<br>";
echo $result ['Latitude'];
echo "<br> ";
echo $result ['Longitude'];
echo " <br>";
echo $result ['Seen'];
echo " <br> ";
echo $result ['Time'];
echo "<br> ";
echo "<br> ";
}
//}
?>
For that you need to first grab the find field of your text box like this..
$searchKeyword = $_POST['find']; // Sanitize this value first !!
Next you need to pass it to your query... Change your yourcolumn to suit your column name.
$sql=mysqli_query($link, "Select * FROM Locations WHERE `yourcolumn` LIKE '%$searchKeyword%' ");
EDIT :
You could grab both fields and do a check..
if(!empty($_POST['find']) && !empty($_POST['field']))
{
//do your query like..
$searchKeyword = $_POST['find']; // Sanitize this value first !!
$searchKeyword2 = $_POST['field']; // Sanitize this value first !!
$sql=mysqli_query($link, "Select * FROM Locations WHERE `yourcolumn` LIKE '%$searchKeyword%' AND `yourcolumn2` LIKE '%$searchKeyword2%' ");
}
else
{
echo "The search criteria cannot be empty !";
}

php combobox & button should hide once updated into mysql db and show success message instead of combobox & button place.

here i updated one field value through user input form (combo box type). update process is working perfectly. but, after data stored in db it should show the value what i'm selected in combo box and then that combox box & update button must hide once updated and it should show message "updated successfully" instead of combo box & button. NOT USING JAVASCRIPT ALERT METHOD. THIS IS NOT A ALERT MESSAGE. once updated data in db that combo box should hide and then success message should show in that particular .
this is my main page coding :
while($a_row = mysql_fetch_array($sql))
{
echo "\t<td>" . $a_row['guestname'] . "</td>";
echo "\t<td>" . $a_row['agentname'] . "</td>\n";
echo "\t<td><form action=statusdb.php method=post>
<select name=update><option value=empty></option><option value=Confirm>Confirm</option><option value=Processing>Processing</option><option value=Pending>Pending</option><option value=Cancelled>Cancelled</option></select>
<input name=id type=hidden value='".$a_row['slno']."';>
<input type=submit value=Update>
</form>
</td>\n";
echo "</tr>\n";
}
this is my statusdb coding :
if (isset($_POST['id']))
{
$id = mysql_real_escape_string($_POST['id']);
$update= mysql_real_escape_string($_POST['update']);
$sql = mysql_query("UPDATE guest_details SET status = '$update' WHERE slno = '$id'");
if(!$sql)
{
die("Error" .mysql_error());
}
else
{
echo "<html><body onload=\"alert('Status Updated Successfully');\"></body></html>";
}
}
I'm thinking this would work.
Try adding the following after the line echoing the agentname:
if ( $a_row['status'] != 'empty' ) {
echo "\t<td>" . $a_row[$status] . "</td>\n";
} else {
// here's where you'd put the next 6 lines
}
PS: I don't see an opening <tr> tag.

Passing dropdown data which is gatherd from a mysql table

I need a script that loads data form mysql and show on a drop down list. From there, I need to pass the selected data to another page.
I have done the first step. I am now able to load data from mysql table and to show them on a drop down menu. The exact is given bellow.
<?php
include("config.php");
$result= mysql_query("SELECT folder_name FROM folders");
echo '<select name="directory">'; // Open your drop down box
while ($row = mysql_fetch_array($result)) {
//echo "<option>" . $row['folder_name'] . "</option>";
echo '<option value="'.$row['folder_name'].'">'.$row['folder_name'].'</option>';
}
echo '</select>';// Close your drop down box
?>
Now I need help to pass the selected data to another page. Any suggestion please?
Let me consider the form is posted to page2.php from page1.php
page1.php
<form method="post" action="page2.php">
//your dropdown code here
<?php
include("config.php");
$result= mysql_query("SELECT folder_name FROM folders");
$str = '';
$str .= '<select name="directory">'; // Open your drop down box
while ($row = mysql_fetch_array($result)) {
$str .= '<option value="'.$row['folder_name'].'">'.$row['folder_name'].'</option>';
}
$str .= '</select>';// Close your drop down box
echo $str;
?>
<input type="submit" value="submit" />
</form>
in page2.php you can access the dropdown selected value as
$selVal = '';
if(isset($_POST['directory']))
{
$selVal = $_POST['directory'];
}
create a javascript function to handle the redirect with the folder name data:
function changePage(folder){
window.location.href= 'http://www.yourdomain.com/page2.php?folder=' + folder;
}
onchange option, trigger changePage javascript function with folder name as input:
include("config.php");
$result= mysql_query("SELECT folder_name FROM folders");
echo '<select name="directory">'; // Open your drop down box
while ($row = mysql_fetch_array($result)) {
echo '<option value="'.$row['folder_name'].'" onchange="changePage(\''.$row['folder_name'].'\')">'.$row['folder_name'].'</option>';
}
echo '</select>';// Close your drop down box
page2.php
$folder_name = strip_tags($_GET['folder']);

Categories