Dropdown filter submitted on page load - php

I am using the following code to select a variable for a query. I need a value/variable to be automatically selected when the page loads.
$ID_SOCIEDAD = $_POST['Country'];
echo "<form name='country_list' method='POST' action='http://opben.com/colombia/familias-de-carteras' >";
echo "<select name='Country' >";
while($row = mysql_fetch_array($result))
{
echo " <option selected value='". $row['Fund_Manager_Company_Code'] ."'>". $row['Fund_Manager_Company_Name'] ."</option>";
}
echo " </select>
<input type='submit' value='Filter' />";
echo "</form>";

use this code
<?=($_POST['country']== $row['Fund_Manager_Company_Code']) ?"selected='selected'" : "" ?>
or
echo ($_POST['country']== $row['Fund_Manager_Company_Code']) ?"selected='selected'" : "";

Related

update mysql datarow from php form with button

I am trying to update a php form that holds a few rows of mysql data. I have a button next to each row and when i click on that I want to update the row. The issue im having below is the ID is only set as the last row. How do i get this to push the ID to the button? So basically no matter what button i press i always get the same ID which is the last one to load.
if($result){
while($row = mysqli_fetch_array($result)){
$id = $row["ID"];
$beername = $row["BeerName"];
$beertype = $row["BeerType"];
$beerpercent = $row["BeerPercent"];
$beerdescription = $row["BeerDescription"];
$nowpouring = $row["NowPouring"] =='0' ? '' : 'checked=\"checked\"';
$glutenreduced = $row["GlutenReduced"] =='0' ? '' : 'checked=\"checked\"';
$beertogo = $row["BeerToGo"] =='0' ? '' : 'checked=\"checked\"';
echo "<form action='' method='POST'>";
echo "<tr><td><h6><input type=\"text\" size=\"5\" name=\"id\" value=\"$id\"></h6></td>";
echo "<td><h6><input type=\"text\" size=\"30\" name=\"BeerName\" value=\"$beername\"></h6></td>";
echo "<td><h6><input type=\"text\" size=\"30\" name=\"BeerType\" value=\"$beertype\"></h6></td>";
echo "<td><h6><textarea size=\"90\" style=\"width:250px;height:150px;\" name=\"BeerDescription\" value=\"\">$beerdescription</textarea></h6></td>";
echo "<td><h6><input type=\"text\" size=\"5\" name=\"Percent\" value=\"$beerpercent\"></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"NowPouring\" value=\"true\" $nowpouring></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"GlutenReduced\" value=\"true\" $glutenreduced></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"BeerToGo\" value=\"true\" $beertogo></h6></td>";
#echo "<td><h6> <a href=\". $_SERVER["PHP_SELF"] .?id=".mysql_result($result,$j,'id')."\" onclick=\"\"></h6></td>";
echo "<td><h6> <button name=\"submit\" type=\"submit\" value=\"$id\">Save</button></h6></td>";
echo "</tr>";
echo "</form>";
}
}
if (isset($_POST['submit'])) {
$user = $_POST['submit'];
echo "<p style=\"color:#ffffff\">$id</p>";
#$delet_query = mysqli_query($mysqli, "UPDATE NowPouring SET NowPouring = '1' WHERE ID = '4'") or die(mysql_error());
if ($delet_query) {
echo '<p style="color:#ffffff">Beer with id '.$id.' is updated. To refresh your page, click ' . ' <a href=' . $_SERVER["PHP_SELF"] . ' > here </a></p>';
}
}
?>
The main problem I see here is that the while loop your code has is generating the same name for the inputs...
All of your "<button name=\"submit\" type=\"submit\" value=\"$id\">Save</button>" will have the same name, that's why it always has the last id as value.
Maybe you should try something such as..
<button name=\"$id_submit\" type=\"submit\" value=\"$id\">Save</button>
or if you want you can store it in an array..
<button name=\"submit[]\" type=\"submit\" value=\"$id\">Save</button>
You are seeing this result because the 'name' of each of your inputs is the same, so essentially you have a form with a bunch of elements that have the same names. You need to add a dynamic aspect to each name.
For example, you could update your output to something like this:
echo "<tr><td><h6><input type=\"text\" size=\"5\" name=\"id_$id\" value=\"$id\"></h6></td>";
Where each line adds the current id. Then when you retrieve the form data, you can append the submitted id to the field you want to update.
Have you considered using an AJAX approach so you can submit just the line in question and not have to reload the page and return the whole data set each time?
Make <form> for each submit button. Adding <form> in the while():
if($result){
while($row = mysqli_fetch_array($result)){
echo "<form action='' method='POST'>";
//...
echo "</form>";
}
}
Your form tag is placed at the wrong place.
It should be within:
while($row = mysqli_fetch_array($result)){
$id = $row["ID"];
//....
//....
echo "<form action='' method='POST'>";
echo"<tr>";
echo "<td>" . $id . "</td>";
//....
//....
echo "<td><button type='submit' name='submit'>Save</button></td>";
echo"</tr>";
echo "</form>";
}

Placing id/option value from dropdown selection into variable PHP, MySQL HTML

I have a dropdown list that is filled with some values from a MySQL table.
The issue i'm facing is that I cannot place the ID from the selected value into a variable.
This is my code up until now:
<?php
mysql_connect('localhost', 'confidential', 'confidential');
mysql_select_db('mydb');
$sql = "SELECT zone_naam FROM zone";
$result = mysql_query($sql);
echo "<select name='zone_1' id='zone_1'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['idzone'] . "'>" . $row['zone_naam'] . " </option>";
}
echo "</select>";
?>
I would think that I could get the value from $row['idzone'] by using the code below.
if(isset($_POST['submit'])) {
$zone_1 = $_POST['zone_1'];
$richting = $_POST['richting'];
$zone_2 = $_POST['zone_2'];
}
I've tried several things, but I cannot come to a solution.
If I want to do the same thing in HTML with self set data like below it always works, but whenever I want to use PHP for this purpose I seem to fail.
<select name="zone">
<option value="1">Zone1</option>
<option value="2">Zone2</option>
<option value="3">Zone3</option>
</select>
I hope you all understand what I mean and can help me to find the cause of this problem.
Best regards,
Rudibwoyyy
#Rudi
Then David is correct. If you submit the HTML from your post
<select name="zone">
<option value="1">Zone1</option>
<option value="2">Zone2</option>
<option value="3">Zone3</option>
</select>
you can use, according the select name "zone", the variable $_POST['zone'], assuming you are using <form method="post">, (otherwise it's $_GET['zone']) will contain the value (1, 2 or 3 respectively) of the selected entry.
If this doesn't work for you, check if the generated HTML Code hat the correct name.
Below is my code that works!! Thanks for all the tips
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('confidential');
$sql = "SELECT * FROM zone";
$result = mysql_query($sql);
echo "<form type='submit' class='form-inline' action='' method='POST'>
<select name='zone_1' id='zone_1' class='form-control mb-2 mr-sm-2 mb-sm-
0'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='". $row['idzone'] . "'>" . $row['zone_naam'] . "
</option>";
}
echo "</select>";
echo "<form type=\"submit\" method=\"POST\"> ";
echo " <select name=\"richting\" class='form-control mb-2 mr-sm-2 mb-sm-0'> ";
echo " <option value=\"<-->\"> <--> </option> ";
echo " <option value=\"<--\"> <-- </option> ";
echo " <option value=\"-->\"> --> </option> ";
echo "</select> ";
$result2 = mysql_query($sql);
echo "<select name='zone_2' class='form-control mb-2 mr-sm-2 mb-sm-0'>";
while ($row = mysql_fetch_array($result2)) {
echo "<option value='" . $row['idzone'] . "'>" . $row['zone_naam'] . "
</option>";
}
echo "</select>";
echo "<br><br>";
echo "<input type='submit' value='Submit' name='submit' id='submit' class='btn btn-primary btn-sm'>";
echo "</form>";
if(isset($_POST['submit'])) {
$zone_1 = $_POST['zone_1'];
$richting = $_POST['richting'];
$zone_2 = $_POST['zone_2'];
}
?>

Retain select option inside php if statements

I am attempting to create a single PHP page that opens a salesman or customer window based off a hidden input field in a form being posted. The tricky part for me is I also need two select option dropdowns that appear based on the same hidden input field being posted but ALSO retain their value after submit.
I know how to retain the values of a form like so..
<form action="" method="post">
<select name="newyear">
<option <?php if ($sqlyear == '2015cust') { ?>selected="true" <?php }; ?>value="2015cust">2015</option>
<option <?php if ($sqlyear == '2016cust') { ?>selected="true" <?php }; ?>value="2016cust">2016</option>
</select>
</form>
But when I try and implement this method inside my PHP if statements it does not retain the selected option.
if ($open == 'salesman_window'){
echo "This is salesman dropdown menu";
echo "$sqlyear";
echo "<form action='' method='post'>";
echo "<input type='hidden' name='newyear_trigger' value=''>";
echo "<input type='hidden' name='window' value='salesman_window'>";
echo "<select name='newyear'>";
echo "<option if ($sqlyear == '2015users') { selected='true' }; value='2015users'> 2015</option>";
echo "<option if ($sqlyear == '2016users') { selected='true' }; value='2016users'>2016</option>";
echo "</select>";
echo "<button class='documentation_button'>Submit</button>";
echo "</form>";
}
if ($open == 'customer_window'){
echo "this is customer dropdown menu";
echo "$sqlyear";
echo "<form action='' method='post'>";
echo "<input type='hidden' name='newyear_trigger' value=''>";
echo "<input type='hidden' name='window' value='customer_window'>";
echo "<select name='newyear'>";
echo "<option if ($sqlyear == '2015cust') { selected='true' }; value='2015cust'>2015</option>";
echo "<option if ($sqlyear == '2016cust') { selected='true' }; value='2016cust'>2016</option>";
echo "</select>";
echo "<button class='documentation_button'>Submit</button>";
echo "</form>";
}
?>
I have tried breaking up the option with multiple echo ""; and tried a few ".." of these in there as well but either get syntax errors or it just doesn't retain the selection. Everything works in regards to showing the correct dropdown menu based on what is posted but I just cannot get the select option to retain. The $sqlyear is for sure getting the correct value each time the dropdown option is selected so I know it isn't that either. Can anyone help?
A couple of options:
Use a ternary operator in your echo:
echo "<option" . (($sqlyear == '2015users') ? " selected='true'" : '')
. " value='2015users'>2015</option>";
echo "<option" . (($sqlyear == '2016users') ? " selected='true'" : '')
. " value='2016users'>2016</option>";
Keep using the if, but without opening/closing PHP tags as much:
<option <?php if ($sqlyear == '2015users') echo " selected='true'"; ?> value='2015users'>
2015</option>
<option <?php if ($sqlyear == '2016users') echo " selected='true'"; ?> value='2016users'>
2016</option>

Update feature not updating, why is ID not being passed?

FIRST NOTE: I have looked at some of the suggested answers on SO, unfortunately I still haven't found an answer.
I have noticed that in my update statement, the ID value of the item im attempting to edit is not being passed to editUrl.php. However, if I hard code an id number (i.e. WHEREid= '13'";) the update works perfectly.
QUESTION: Why is the value of 'id' not being passed to editUrl.php?
CODE:
editUrlForm.php file:
<!-- Include AJAX Framework -->
<script src="js/ajax.js" language="javascript"></script>
<!--Include Database connections info-->
<?php include('config.php'); ?>
<?php
$id = $_GET['id'];
if(isset($_GET['id'])) {
$cdquery="SELECT * FROM links WHERE `id` = '$id'";
$cdresult=mysql_query($cdquery) or die ("Query to get data from first table failed: ".mysql_error());
while ($row = mysql_fetch_assoc($cdresult)) {
echo "Edit URL:";
echo "<form action='javascript:update()' method='get'>";
echo "<table>";
echo "<tr>";
echo "<th>URL ID:</th> <td><label for='urlId'>". $row['id'] . "</label></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Name:</th> <td><input name='name' type='text' id='name' value='". $row['name'] . "' size'30'></input></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Release Time:</th> <td><input name='releaseTime' type='time' id='releaseTime' value='". $row['releaseTime'] . "'></input></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Release Day:</th> <td><select name='releaseDay' id='releaseDay' value='". $row['releaseDay'] . "'> <option value='monday'>Monday</option> <option value='tuesday'>Tuesday</option> <option value='wednesday'>Wednesday</option> <option value='thursday'>Thursday</option> <option value='friday'>Friday</option> <option value='saturday'>Saturday</option> <option value='sunday'>Sunday</option> </select></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Category:</th> <td><select name='category' id='category' value='". $row['category'] . "'> <option value='television'>Television</option> <option value='movie'>Movie</option> <option value='music'>Music</option> </select></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Genre:</th> <td><select name='genre' id='genre' value='". $row['genre'] . "'> <option value='action'>Action</option> <option value='drama'>Drama</option> <option value='comedy'>Comedy</option> <option value='thriller'>Thriller</option> <option value='horror'>Horror</option> <option value='childrens'>Childrens</option> <option value='romantic'>Romantic</option> </select></td>";
echo "</tr>";
echo "<tr>";
echo "<th>URL:</th> <td><input name='url' type='text' id='url' value='". $row['url'] . "' size'250'></input></td>";
echo "</tr>";
echo "</table><br />";
echo "<input type='submit' name='Submit' value='Edit URL'/>";
echo "<input type='button' value='Cancel' onClick='actionCancel();return false;'/>";
echo "</FORM>";
}
}
?>
ajax function:
function update()
{
var name= encodeURIComponent(document.getElementById('name').value);
var releaseTime = encodeURIComponent(document.getElementById('releaseTime').value);
var releaseDay = encodeURIComponent(document.getElementById('releaseDay').value);
var category = encodeURIComponent(document.getElementById('category').value);
var genre = encodeURIComponent(document.getElementById('genre').value);
var url= encodeURIComponent(document.getElementById('url').value);
xmlhttp.open('get', 'editUrl.php?name='+name+'& releaseTime=' +releaseTime+'& releaseDay=' +releaseDay+'& category=' +category+'& genre=' +genre+'& url=' +url);
xmlhttp.onreadystatechange = urlRefresh;
document.getElementById("content02").innerHTML = "Processing Request. Please wait a moment...";
xmlhttp.send(null);
return;
}
editUrl.php file:
<!-- Include Database connections info. -->
<?php include('config.php'); ?>
<?php
$id = $_GET['id'];
$name = $_GET['name'];
$releaseTime = $_GET['releaseTime'];
$releaseDay = $_GET['releaseDay'];
$category = $_GET['category'];
$genre = $_GET['genre'];
$url = $_GET['url'];
$editUrl_sql = "UPDATE `links` SET `id` = '{$id}', `name` = '{$name}', `releaseTime` = '{$releaseTime}', `releaseDay` = '{$releaseDay}', `category` = '{$category}', `genre` = '{$genre}', `url` = '{$url}' WHERE `id` = '{$id}'";
$editUrl_sql= mysql_query($editUrl_sql) or die(mysql_error());
mysql_close($link);
?>
you do not passed id as parameter
your coude should look like:
var id = encodeURIComponent(document.getElementById('urlId').value);
xmlhttp.open('get', 'editUrl.php?name='+name+'&releaseTime=' +releaseTime+'&releaseDay=' +releaseDay+'&category=' +category+'&genre=' +genre+'&url=' +url+'&id=' +id);

Keep Selection visible after Submit

How can I make the filtered selection still appear after the request.
So if I have options 1,2,and 3. When I select 2 and data shows I still want 2 to display signifying that the data is filtered through option 2.
echo "<form name='country_list' method='POST' action='http://opben.com/colombia/familias-de-carteras' >";
echo "<select name='Country' tabindex='1' >";
while($row = mysql_fetch_array($result))
{
echo " <option value='". $row['Fund_Manager_Company_Code'] ."'>". $row['Fund_Manager_Company_Name'] ."</option>";
}
echo "</select>";
echo "<input type='submit' value='Filter' />";
echo "</form>";
You can do something like this :
$country = isset($_POST['Country']) ? $_POST['Country'] : '';
while($row = mysql_fetch_array($result))
{
echo " <option value='". $row['Fund_Manager_Company_Code'] ."' ".(($row['Fund_Manager_Company_Code'] == $country) ? 'selected="selected"' : '').">". $row['Fund_Manager_Company_Name'] ."</option>";
}
What you need is to add selected attribute to option:
$Country = $_POST['Country'];
$sected = 'selected = "selected" ';
while($row = mysql_fetch_array($result))
{
echo " <option ".($row['Fund_Manager_Company_Code'] == $Country? $selected : '')."value='". $row['Fund_Manager_Company_Code'] ."'>". $row['Fund_Manager_Company_Name'] ."</option>";
}
The one that the value selected and posted then will be selected...
something like
echo" <option value='" . $row['Fund_Manager_Company_Code'] . "' " . ((isset($_POST['Country']) && $_POST['Country'] == $row['Fund_Manager_Company_Code'])
? 'selected="selected"' : '') . ">" . $row['Fund_Manager_Company_Name'] . "</option>";
After your submit, you need to catch the selection in your PHP code:
$selection = $_POST['Country'];
echo "<form name='country_list' method='POST' action='http://opben.com/colombia/familias-de-carteras' >";
echo "<select name='Country' tabindex='1' >";
while($row = mysql_fetch_array($result))
{
$selected = "";
if ($row['Fund_Manager_Company_Code'] == $selection) {
$selected = "selected";
}
echo " <option value='". $row['Fund_Manager_Company_Code'] ."' ".$selected.">". $row['Fund_Manager_Company_Name'] ."</option>";
}
echo "</select>";
echo "<input type='submit' value='Filter' />";
echo "</form>";

Categories