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'];
}
?>
Related
I have this code:
echo " <form action='https://test.com/forum/index.php?/search/&q=".
$row['record_meta_keywords'] ."
&type=cms_records7&search_and_or=and&search_in=titles'> ";
echo " <select name='/search/&q=". $row['record_meta_keywords'] ."
&type=cms_records7&search_and_or=and&search_in=titles'> ";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['record_meta_keywords'] ."'>" .
$row['record_meta_keywords'] ."</option>";
}
echo " </select> ";
echo " <br><br> ";
echo " <input type='submit'> ";
echo " </form> ";
?>
But, when I choose an option in the dropdown, the URL is being displayed like this:
https://test.com/forum/index.php?search=%26q%3DHowell+Lanes+%26type%3Dcms_records7%26search_and_or%3Dand%26search_in%3Dtitles
The problem is I need the URL to be displayed like this in order for it to work properly:
https://test.com/forum/index.php?/search/&q=Howell%20Lanes%20&type=cms_records7&search_and_or=and&search_in=titles
I tried urlencode but that didn't seem to work either.
How do I solve this problem?
I am trying to retain the value selected in a drop down menu. Everything is working, but I don't know how to show and retain the selected value. How can I do this?
I've got this working using another way:
<?php if($_POST['selClass'] == $row1['class']) echo 'selected="selected"' ?>
but this leads to other problems, i.e. a blank option in my drop down menu.
<form action="" method="POST" name="form1" id="form1">
<select name="selClass" size="1" id="selClass" onchange="form1.submit()">
<option value="">Select a class</option>
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
?>
</select>
</form>
You can approach this as
<?php
$selectedOption = '';
if($_POST){
$selectedOption = $_POST['selClass'];
}
?>
<form action="" method="POST" name="form1" id="form1">
<select name="selClass" size="1" id="selClass" onchange="form1.submit()">
<option value="">Select a class</option>
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
if($row1["class"] == $selectedOption)
echo "<option value='".$row1["class"] ."' selected='selected'>" . $row1["class"]. "</option>";
else
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
?>
</select>
</form>
There is two option you can choose whichever you feel ease.
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
if($_POST['selClass'] == $row1['class']){
echo "<option value='".$row1["class"] ."' selected='selected'>" . $row1["class"]. "</option>";
}else{
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
}
?>
OR
<?php
$selectedClass = $_POST['selClass'];
while ($row1 = mysqli_fetch_array($rs5)) { ?>
<option value="<?php echo $row1['çlass']?>" <?php if(selectedClass == $row1['class']) { echo "selected='selected'"; }?> ><?php echo $row1['class']?></option>
<?php } ?>
This is the code I've got so far:
<label for="course">Course</label>
<select name="course" class="form-control" style="margin-bottom:2%;">
<?php
$sql="SELECT course_name FROM course";
$result = mysqli_query($conn, $sql) or die(mysql_error());
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['course_name'] ."'>" . $row['course_name'] ."</option>";
}
?>
</select>
I'm trying to make it so the dropdown appears blank at first instead of showing an option pulled from the database. (all connection etc is above)
Alex answered this question for you. Just add <option>Select...</option> before the loop (Change Select... to white space if you really want it to show a "blank" option).
Here is the code:
<label for="course">Course</label>
<select name="course" class="form-control" style="margin-bottom:2%;">
<option>Select...</option>
<?php
$sql="SELECT course_name FROM course";
$result = mysqli_query($conn, $sql) or die(mysql_error());
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['course_name'] ."'>" . $row['course_name'] ."</option>";
}
?>
</select>
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);
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'" : "";