How to retain user generated data on form - php

Hello guys I'm new to OOP PHP and I want to imitate the retaining of user generate data in form from procedural
here is my code:
<?php
class View_LType extends Config{
public function view_ltype(){
$con = $this->con();
$sql = "SELECT * FROM `tbl_leave_type`";
$data = $con->prepare($sql);
$data->execute();
$result = $data->fetchAll();
foreach ($result as $data){
echo "
<tr class='table-head'>
<td class='numbers'>$data[leave_id]</td>
<td>$data[leave_code]</td>
<td>$data[leave_name]</td>
<td class='small'>$data[leave_description]</td>
<td><a href='#'></a></td>
</tr>";
}
}
public function select_ltype(){
$con = $this->con();
$sql = "SELECT * FROM `tbl_leave_type`";
$data = $con->prepare($sql);
$data->execute();
$result = $data->fetchAll();
foreach ($result as $data){
echo '<option value="' . $data['leave_name'] . '">' . $data['leave_name'] . '</option>';
}
}
}
html page:
<div class="form-group">
<label for="ltype">Leave Type</label>
<select name="ltype" id="ltype" name="ltype" required >
<option>Select leave...</option>
<?php $view_leave->select_ltype(); ?>
</select>
</div>
$data[leave_name] is what the user will select what I want is to echo selected when the user select its prefered leave name. please help me guys

If the current $data['leave_name'] is the same as the submitted "ltype", then select that option:
echo '<option value="' . $data['leave_name'] . '"' . (($data['leave_name'] == $_REQUEST['ltype']) ? ' selected' : '') . '>' . $data['leave_name'] . '</option>';

Related

PHP Query set combobox for another combobox

how to fix this syntax for this logic ?
i want to select my select option to select the another select option
<?php
$query_string = "SELECT * FROM products";
$query_string1 = "SELECT * FROM suppliers where ProductID = // firstSelectoption(value)";
$query_string2 = "SELECT * FROM categories";
$query = mysql_query($query_string);
$query1 = mysql_query($query_string1);
$query2 = mysql_query($query_string2);
?>
and in the body i make
<select name="first" id="first" onchange="childrenOnChange(this.value)">
<?php
while ($row = mysql_fetch_array($query)) {
echo '<option value=' . $row["ProductID"] . '>';
echo $row['ProductID'];
echo '</option>';
}
?>
</select>
<select name="second" id="second">
<?php
while ($row = mysql_fetch_array($query1)) {
echo '<script>';
echo 'var arr = array(';
$row['SupplierID'] . ',';
echo ')';
echo '</script>';
}
?>
</select>
i want to set the second select option value with $query1;
If you get the value from the query with $val = mysql_fetch_array($query1), then you can include the following in your loop:
$selected = '';
if ($row['ProductID'] == $val) {
$selected = "selected";
}
echo '<option value="'.$row['ProductID'].'" '.$selected.'>'.$row['ProductID'].'</option>';

Unable to connect using mysqli_real_connect. And why is HTML not being parsed even if any error with my PHP code?

<?php
$link = mysqli_init();
$db = mysqli_real_connect($link, 'localhost', 'bp6am', 'bp6ampass', 'expense_report') or
die('Unable to connect');
mysqli_select_db($link, 'expense_report');
?>
<html>
<head>
<title>Expenditure Log Form</title>
</head>
<body>
<form action="commit.php" method="post">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name"/><br></td>
</tr>
<tr>
<td>Category</td>
<td><select name="category">
<?php
//get the expenses categories
$query = 'SELECT cat_id, cat_name
FROM exp_category
ORDER BY cat_name';
$result = mysqli_query($query, $db) or
die(mysqli_connect_error($db));
//populate the dropdown menu for expenses category
while($row = mysqli_fetch_assoc($result)) {
foreach ($row as $value) {
echo '<option value="' . $row['cat_id'] . '">';
echo $row['cat_name'] . '</option>';
}
}
?>
</select></td>
</tr>
<tr>
<td>Amount</td>
<td><input type="text" name="amount"/></td>
</tr>
<tr>
<td>Payment Mode</td>
<td><select name="payment">
<?php
//retrieve and populate
$query = 'SELECT pay_id, pay_type
FROM pay_mode
ORDER BY pay_id';
$result = mysqli_query($query, $db) or
die(mysqli_connect_error($db));
//populate
while($row = mysqli_fetch_assoc($result)) {
foreach ($row as $value) {
echo '<option value="' . $row['pay_id'] . '">';
echo $row['pay_type'] . '</option>';
}
}
?>
</select></td>
</tr>
</table>
</form>
</body>
</html>
When I run the file in browser, only the Name field is constructed. After that there is the Category text with the select icon but no menu drops down on clicking it. Rest of the page is blank!! Recently started with PHP please help.
I'm wondering whether the connection is established or not. Even if no connection was established why other HTML elements are not being displayed after the Category field?
I checked my connection parameters. Even if they were wrong I should get the message "Unable to connect" right?
If you had turned error_reporting on, you'd know this from looking at the serverlogs, take a look at How to get useful error messages in PHP (StackOverflow).
Also, take a look at the mysqli_query documentation at PHP.net, it'll tell you that the connection comes first, then the query-string.
You currently use this:
$result = mysqli_query($query, $db)
while it should be
$result = mysqli_query($db, $query)
And to answer your question, yes you would get a Unable to connect if it couldn't connect (wrong parameters).
You also don't need a foreach-loop in your while-loop.
while($row = mysqli_fetch_assoc($result)) {
foreach ($row as $value) {
echo '<option value="' . $row['cat_id'] . '">';
echo $row['cat_name'] . '</option>';
}
}
The code above should be this instead:
while($row = mysqli_fetch_assoc($result)) {
echo '<option value="' . $row['cat_id'] . '">';
echo $row['cat_name'] . '</option>';
}

Display search form results using PHP to pull data in mysql

Once users fill out the "main_search" form, a list of results should populate, depending on their selection on the previous page. Either the entire list pops up or nothing at all. Below, my HTML starts here and then I need the results to populate on the next page. Please, please, please help!!
For a reference, check this site out: www.lemassif.com
<form name="main_search" action="displaydata.php" id="nl-form" class="nl-form" method="post">
<select name="main1">
<option value="featured" selected>any city</option>
<option value="city1">City 1</option>
</select>
<select name="reason">
<option value="family" selected>family</option>
<option value="romantic">romantic</option>
<option value="business">business</option>
<option value="leisure">leisure</option>
</select>
<select name="budget">
<option value="modest" selected>modest</option>
<option value="moderate">moderate</option>
<option value="lavish">lavish</option>
</select>
<div class="nl-submit-wrap">
<button class="nl-submit" type="submit" name="submit">Create my trip</button>
</div>
Here is my displaydata.php page that should populate the filtered results on the next page.
<?php
// Include the connection file.
include("func.inc.php");
$sql = mysql_query("SELECT * FROM venues");
if(isset($_POST['submit'])) {
$search_term = mysql_real_escape_string($_POST['$venues']);
$sql .= "WHERE city = '{$search_term}'";
$sql .= "OR reason = '{$search_term}'";
$sql .= "OR budget = '{$search_term}'";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<table cellpadding="5" cellspacing="5">
<tr>
<td><strong>Venue Name</strong></td>
<td><strong>Image</strong></td>
<td><strong>Description</strong></td>
</tr>
<?php
while($row = mysql_fetch_array($sql)) { ?>
<tr>
<td><?php echo $row['venue_name']; ?></td>
<td><?php echo $row['image']; ?></td>
<td><?php echo $row['description']; ?></td>
</tr>
<?php }
?>
</table>
Here is my func.inc.php file.
<?php
include_once 'connection.php';
function close(){
mysql_close();
}
function city_query(){
$myData = mysql_query("SELECT city FROM venues");
while($record = mysql_fetch_array($myData)){
echo '<option value="' . $record['city'] . '">' . $record['city'] . '</option>';
}};
function reason_query(){
$myData2 = mysql_query("SELECT reason FROM venues");
while($record2 = mysql_fetch_array($myData2)){
echo '<option value="' . $record3['reason'] . '">' . $record2['reason'] . '</option>';
}};
function budget_query(){
$myData3 = mysql_query("SELECT budget FROM venues");
while($record3 = mysql_fetch_array($myData3)){
echo '<option value="' . $record3['budget'] . '">' . $record3['budget'] . '</option>';
}};
function search_venues() {
$city = $_POST['city'];
$reason = $_POST['reason'];
$budget = $_POST['budget'];
//Do real escaping here
$query = "SELECT * FROM venues";
$conditions = array();
if($city !="") {
$conditions[] = "city='$city'";
}
if($reason !="") {
$conditions[] = "reason='$reason'";
}
if($budget !="") {
$conditions[] = "budget='$budget'";
}
$sql = $query;
if (count($conditions) > 0) {
$sql .= " WHERE " . implode(' AND ', $conditions);
}
$result = mysql_query($sql);
return $result;
}
?>
What am I missing? Thanks in advance!
I think you're builting a malformed query in displaydata.php
It should be:
<?php
// Include the connection file.
include("func.inc.php");
$sql = "SELECT * FROM venues "; //<----note here: no mysql_query() and a blank at the end
if(isset($_POST['submit'])) {
$search_term = mysql_real_escape_string($_POST['$venues']);
$sql .= "WHERE city = '{$search_term}' "; //<----note here: blank at the end
$sql .= "OR reason = '{$search_term}' "; //<----note here : blank at the end
$sql .= "OR budget = '{$search_term}' "; //<----note here: blank at the end
}
[....]

how to display the selected value from combo box in php

I have select (combo boxes) in PHP and after one was selected i fill the second one with data from the database.
the question is how to display my selection from the first combo box after the page reload.
I have the following code:
<select name="category" id="category" maxlength="30" onchange="this.form.submit();">
<option value =""></option>
<?php
require_once("config.php");
// Connect to server
$con = mysql_connect($ServerAddress,$ServerUser,$ServerPassword);
//choose DB
mysql_select_db($DbName, $con);
$sql = "SELECT * FROM `category` ORDER BY `name`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)){
echo '<option value ="'.$row1['name'].'">'.$row1['name'].'</option>';
}
?>
</select>
</span>
<label>race:</label>
<span>
<select name="race" id="race" maxlength="30" >
<option value =""></option>
<?php
if (isset($_POST['category'])) {
$var = $_POST['category'];
// Connect to server
$con = mysql_connect($ServerAddress,$ServerUser,$ServerPassword);
//choose DB
mysql_select_db($DbName, $con);
$sql = "SELECT * FROM `race` WHERE `category`='" . sqlSecure($var) . "'ORDER BY `race`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)){
echo '<option value ="'.$row1['race'].'">'.$row1['race'].'</option>';
}
}
?>
</select>
Method how to get values are defined in form tag in attribute method
In your case you need to get echo $_POST['category'] and compare it with name of option
Your code are not perfect. Look follows: :)
<?php
require_once("config.php");
// Connect to server
$con = mysql_connect($ServerAddress, $ServerUser, $ServerPassword);
//choose DB
mysql_select_db($DbName, $con);
?>
<select name="category" id="category" maxlength="30" onchange="this.form.submit();">
<option value=""></option>
<?php
$val = $_POST['category']?:'';
$sql = "SELECT * FROM `category` ORDER BY `name`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)) {
$selected = ($val == $row1['name'] ? 'selected="selected"' : '');
echo '<option value ="' . $row1['name'] . '" '. $selected .'>' . $row1['name'] . '</option>';
}
?>
</select>
</span>
<label>race:</label>
<span>
<select name="race" id="race" maxlength="30">
<option value=""></option>
<?php
if (isset($_POST['category'])) {
$var = $_POST['category'];
$sql = "SELECT * FROM `race` WHERE `category`='" . sqlSecure($var) . "'ORDER BY `race`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)) {
echo '<option value ="' . $row1['race'] . '">' . $row1['race'] . '</option>';
}
}
?>
</select>

How do I select value from DropDown list in PHP??? Problem

I want to know the error in this code
The following code retrieves the names of the members of the database query in the
dropdownlist
But how do I know who you selected.... I want to send messages only to the members that selected form dropdown list
<?php
include ("connect.php");
$name = $_POST['sector_list'];
echo $name ;
?>
<form method="POST" action="" >
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option size ="40" value="default">send to </option>
<?php
$result = mysql_query('select * from members ')
or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option size ="40" value=" '. $row['MemberID'] . '" name="' . $row['MemberName']. '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
I hope somebody can help me
This should do the trick.
<?php
$member_id = intval($_POST['sector_list']);
if($member_id == 0) {
// Default choice was selected
}
else {
$res = mysql_query("SELECT * FROM members WHERE MemberID = $member_id LIMIT 1");
if(mysql_num_rows($res) == 0) {
// Not a valid member
}
else {
// The member is in the database
}
}
?>
<form method="post" action="">
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option value="0">send to</option>
<?php
$result = mysql_query('SELECT * from members') or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="' . $row['MemberID'] . '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
To get an input to change when you select someone try this:
<select onchange="document.getElementById('text-input').value = this.value;">
<!-- Options here -->
</select>
<input type="text" id="text-input">

Categories