I'm having trouble with my dynamic select forms. When the "subject" form is submitted it works perfectly, but when the "course" form is then submitted, the form seems to submit properly (localhost/page.php?subject=1&course=2), but it returns the option back to null so it isn't working with later php that is dependent on the course_id.
Header PHP:
<?php
$subject = $course = null;
$conn = mysql_connect('', '', '');
$db = mysql_select_db('',$conn);
if(isset($_GET["subject"]) && is_numeric($_GET["subject"]))
{
$subject = $_GET["subject"];
}
if(isset($_GET["course"]) && is_numeric($_GET["course"]))
{
$country = $_GET["course"];
}
?>
Javascript:
<script language="JavaScript">
function autoSubmit()
{
var formObject = document.forms['theForm'];
formObject.submit();
}
</script>
HTML Form:
<form name="theForm" method="get">
<select name="subject" onChange="autoSubmit();">
<option value="null">Select a Subject...</option>
<?php
$sql = "SELECT DISTINCT subj_name, subj_id FROM table1 ORDER BY subj_name";
$result = mysql_query($sql) or die ("couldn't execute query");
while($row = mysql_fetch_array($result))
{
echo ("<option value=\"$row[subj_id]\" " .
($subject == $row["subj_id"] ? " selected" : "") . ">$row[subj_name]</option>");
}
?>
</select>
<?php
if($subject != null && is_numeric($subject))
{
?>
<select name="course" onChange="autoSubmit();">
<option value="null">Select a Course...</option>
<?php
$sql = "SELECT DISTINCT course_id, course_name, subj_id FROM table1 WHERE subj_id = $subject";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo ("<option value=\"$row[course_id]\" " .
($course == $row["course_id"] ? " selected" : "") . ">$row[course_name]</option>");
}
?>
</select>
<?php
}
?>
</form>
In these lines "selected" should only be set if $subject and $course are not passed in callback request:
<option value="null" selected="selected">Select a Subject...</option>
...
<option value="null" selected="selected">Select a Course...</option>
Otherwise, you'll get two options marked as selected in each dropdowns. This cause to bug.
<pre>
if(isset($_GET["course"]) && is_numeric($_GET["course"]))
{
$country = $_GET["course"];
}
</pre>
Here, you are setting a $country variable instead of $course.
**Use this one**
$course= $_GET["course"];
I found the error: $country in line 14 needs to be $course:
if(isset($_GET["course"]) && is_numeric($_GET["course"]))
{
$course = $_GET["course"];
}
?>
Related
<?php
$sql=mysqli_query($mysqli,"select * from calls where eid='$aid'")or die(mysqli_error($mysqli));
while($row2=mysqli_fetch_assoc($sql))
{
?><select name='city'>
<option value="">Select City</option>
<?php
$c=explode(',', $row2['city']);
foreach ($c as $c1)
{
?>
<option value='$c1' <?php if(isset($_POST['search']) || isset($_POST['next'])|| isset($_POST['submit']))
{
if(($c==$c1))
{ echo 'Selected';}
}?>><?php echo $c1;?></option>"
<?php }
echo"</select>";
}
?>
calls table city is stored as comma separated array. I have successfully get it into drop-down box. Now I want that whenever I submit or page refresh it holds se selected drop-down value.
You should check for isset($_POST["city"]) && $_POST["city"] == $city
foreach ($cities as $city)
{
$selection = (isset($_POST["city"]) && $_POST["city"] == $city ? 'selected':'' );
echo ' <option value="'.$city.'" '.$selection.' >'.$city.'</option>';
}
You only need to compare the value of $c1 against its previous value returned by the form submit, after checking that this execution is the result of the form being posted.
<?php
$sql = "select * from calls where eid=?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s', $aid)
$stmt->execute();
$result = $stmt->get_result();
while($row2 = $result->fetch_assoc()) {
?>
<select name='city'>
<option value="">Select City</option>
<?php
$c=explode(',', $row2['city']);
foreach ($c as $c1) {
$sel = ''; // init value
if ( $_SERVER['REQUEST_METHOD'] == 'POST' && $c1 == $_POST['city'] ) {
$sel = "selected='selected'";
}
echo "<option $sel value='$c1' >$c1</option>";
}
echo "</select>";
}
?>
I have this onchange event on php, if the user choose an option, it fetched the data that is connected on it. However, the page refreshes and the choice of the user disappear. Also, I cannot insert the data to database. I've tried add a form method="post" to the form attribute, but unfortunately it cannot fetch the data.
Thankyou in advance.
<?php
$dsn = 'mysql:host=localhost;dbname=admin';
$username = 'root';
$password = '';
try{
// Connect To MySQL Database
$con = new PDO($dsn,$username,$password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $ex) {
echo 'Not Connected '.$ex->getMessage();
}
$gradeassign = '';
$sectionassign = '';
function getPosts()
{
$posts = array();
$posts[3] = $_POST['sectionassign'];
$posts[4] = $_POST['gradeassign'];
return $posts;
}
if(isset($_POST['addfac']))
{
$data = getPosts();
$insertStmt = $con->prepare('INSERT INTO
facultyagain(sectionnumber,gradelevelassign)
VALUES(:sectionassign,:gradeassign)');
$insertStmt->execute(array(
':sectionassign'=> $data[3],
':gradeassign'=> $data[4],
));
if($insertStmt)
{
echo 'Data Inserted';
}
}
?>
<html>
<head>
<title>Country</title>
</head>
<body>
<form action="trial.php">
Select Your grade
<select name="gradeassign" onchange="this.form.submit()">
<option value="" disabled selected>--select--</option>
<option value="1">Grade 1</option>
<option value="2">Grade 2</option>
<option value="europe">Europe</option>
</select>
<?php
require 'connection.php';
if(isset($_GET["gradeassign"])){
$gradeassign=$_GET["gradeassign"];
$sql = "SELECT sectionassign FROM sections WHERE gradeassign='$gradeassign'";
$result = $con->query($sql);
echo "<select>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='$row[sectionassign]'>" . $row["sectionassign"]. " </option>";
}
} else { echo "<B>0 Results</B>"; }
echo "</select>";
}
?>
<BR>
<button id="addfac" name="addfac">Add Faculty</button>
</form>
</body>
</html>
You can also store the value in a variable and check to see if the option matches the variable. For example:
<option value="" disabled>--select--</option>
<option value="1" <?php if(isset($_GET['gradeassign'])) && $_GET['gradeassign'] == "1") echo "checked"; ?>>Grade 1</option>
<option value="2" <?php if(isset($_GET['gradeassign'])) && $_GET['gradeassign'] == "2") echo "checked"; ?>>Grade 2</option>
<option value="europe" <?php if(isset($_GET['gradeassign'])) && $_GET['gradeassign'] == "europe") echo "checked"; ?>>Europe</option>
The better way to do it would be to create the whole list with a loop. Bu this should work.
I am taking the values of the course and the semester,and searching in database for the instructors who teach the course at that semester,It worked. But I want the values to be shown in a list after submitting it, I want the list to appear only after submitting.Then I want to choose from the list an instructor and add him in database with the course and the semester selected first,without re selecting them.Please help..
<?php
session_start();
$_SESSION['courses'] = $_SESSION['semester'] = $coursechosen = $semesterchosen = "";
if ($_SERVER["REQUEST_METHOD"] == "GET") {
error_reporting(E_ALL ^ E_NOTICE);
require_once 'connect.php';
$semester = "";
$courses = $_GET['courses'];
$sem = $_GET['sem'];
$semyr = $_GET['semyr'];
$semester = $sem . $semyr;
if (isset($_GET['courses']) && isset($_GET['sem']) && isset($_GET['semyr'])) {
$_SESSION['courses'] = $courses;
$_SESSION['semester'] = $semester;
$instructors = mysql_query("select Instructor_Id from teach where
Semester='$semester' AND course_code='$courses' ");
if (!$instructors) {
die("Database access failed: " . mysql_error());
}
$row = mysql_num_rows($instructors);
}
}
?>
<?php
$instructor = $_GET['instructor'];
if (isset($_SESSION['courses']) && isset($_SESSION['semester'])) {
$coursechosen = $_SESSION['courses'];
$semesterchosen = $_SESSION['semester'];
$query = "Insert INTO coordinators(instructor_id,course_code,semester)
VALUES ('$instructor','$coursechosen','$semesterchosen')";
// $query="Insert INTO instructors(Fname)VALUES('$instructor')";
mysql_query($query);
}
?>
<form method="get" action="Chairman.php" onsubmit="return
validate()">
<select name="courses" id="courses" >
<option value="courses"><--Courses--></option>
<option value="PHYS220">Physics for Engineers</option>
<option value="MATH210">Calculus II</option>
<option value="MATH225">Linear Algebra with Applications</option>
</select>
<select name="sem" id="sem" >
<option selected="selected">Season</option>
<option value="Fall">Fall</option>
<option value="Spring">Spring</option>
<option value="Summer">Summer</option>
</select>
<select name="semyr" id="semyr" >
<option>Year</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<div> <input type="submit" value="Submit" ></input></div>
<div>
<select name="instructor">
<?php
if ($row !== 0) {
for ($i = 0; $i < $row; ++$i) {
$instructor_id = mysql_fetch_array($instructors);
$instructorsname = mysql_query("select * from instructors where
Id='$instructor_id[0]'");
$rowname = mysql_num_rows($instructorsname);
$instructorsnames = mysql_fetch_array($instructorsname);
echo "<option
value='$instructorsnames[0]'>" . $instructorsnames['Fname'] . "
" . $instructorsnames['Lname'] . "</option>";
}
}
?>
</select>
</div>
</form>
</body>
</html>
mysql_fetch_array returns an array with your data. So, to show instructors, you have to iterate through your list and write them out. Try:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<option value='$row['id']'>".$row['Fname']."
".$row['Lname']."</option>";
}
That should give you a list of option. But I'm not quite sure if I understood what your question is. If that doesn't answer it, could you be more specific?
I am using dropdown to select values, and after I click the submit button, my values change. Please help me retain the selected values. Using POST method I have got the solution, but I want to use with GET method. Is it possible?
1.) 1st select stmt:
<form action="" method="GET">
<select name="sort" >
<option value="inc_patientName">Patient Name</option>
<option value="inc_date">Date</option>
<option value="inc_status">Status</option>
<option value="inc_patientAge">Age</option>
</select>
<input type="submit" name="GETREPORT" value="Get Report"/>
if ($_GET)
{echo"hi";}
</form>
2.) 2nd select with while
<?php
//Selecting ward from table ward master
$sql = "SELECT ward_name,ward_id FROM ward_master";
$result = mysql_query($sql);
echo "<select name='ward'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['ward_id'] . "'>" . $row['ward_name'] . "</option>";
}
echo "</select>";
?>
3.) 3rd select with javascript:
<select name="daydropdown" id="daydropdown" ></select>
<select name="monthdropdown" id="monthdropdown"></select>
<select name="yeardropdown" id="yeardropdown"></select>
<script type="text/javascript">
populatedropdown("daydropdown", "monthdropdown", "yeardropdown")
Javascript code:
<script type="text/javascript">
var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
function populatedropdown(dayfield, monthfield, yearfield)
{
var today=new Date()
var dayfield=document.getElementById(dayfield)
var monthfield=document.getElementById(monthfield)
var yearfield=document.getElementById(yearfield)
for (var i=1; i<=31; i++)
dayfield.options[i]=new Option(i, i)
dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day
for (var m=0; m<12; m++)
monthfield.options[m]=new Option(monthtext[m], monthtext[m])
monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
var thisyear=1999
for (var y=0; y<45; y++){
yearfield.options[y]=new Option(thisyear, thisyear)
thisyear+=1
}
yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
}
</script>
try
if(isset($_GET['sort'])) {
echo $_GET['sort'];
}
and get selected index
<option value="inc_patientName" <?php if (isset($_GET['sort']) && $_GET['sort'] == "inc_patientName") echo 'selected="seleceted"'; ?>>Patient Name</option>
and so on for all options values match
For 2nd dropdown:-
while ($row = mysql_fetch_array($result)) {?>
<option value="<?php echo $row['ward_id'];?>" <?php if (isset($_GET['sort']) && $_GET['sort'] == $row['ward_id']) echo 'selected="seleceted"'; ?>><?php echo $row['ward_name'];?></option>
<?php }
use it like this... it myt work.
<select name="ward">
<?php
$sql = "SELECT ward_name,ward_id FROM ward_master";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
if ($_GET['ward']==$row["ward_id"]) {
echo '<option selected="selected" value='.$row["ward_id"].'>'.$row["ward_name"].'</option>';
} else {
echo '<option value='.$row["ward_id"].'>'.$row["ward_name"].'</option>';
}
}
?>
</select>
You can do something like this:
<select name="sort" >
<option value="inc_patientName"<?php if ($_GET['sort'] == 'inc_patientName') echo ' selected="seleceted"'; ?>>Patient Name</option>
<option value="inc_date"<?php if ($_GET['sort'] == 'inc_date') echo ' selected="seleceted"'; ?>>Date</option>
<option value="inc_status" <?php if ($_GET['sort'] == 'inc_status') echo ' selected="seleceted"'; ?>>Status</option>
<option value="inc_patientAge"<?php if ($_GET['sort'] == 'inc_patientAge') echo ' selected="seleceted"'; ?>>Age</option>
</select>
Try this:
Use this function in ur script:
function setday(id,elementname)
{
document.getElementById(elementname).value=id;
}
Use this in ur php form, Repeat the same code for year and month dropdowns:
if (isset($_GET['daydropdown']))
{
echo "<script type='text/javascript'>setday('".$_GET['daydropdown']."','daydropdown')</script>";
}
I have issue with select menu on PHP. I tried to get mysql database to select menu. However, it displays none.
Here is my code:
default:
mysql_select_db($database_conn, $conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$Rsenroll = mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$row_Rsenroll = mysql_fetch_assoc($Rsenroll);
$totalRows_Rsenroll = mysql_num_rows($Rsenroll);
$courseid = $row_Rsenroll['courseid'];
$er_staffid = "";
break;
}
?>
<select name="courseid">
<option value="" SELECTED>Selected Course ID</option>
<?php
foreach( $Course as $course_id) {
if ( $course_id == $courseid) {
$selected = " SELECTED";
} else {
$selected = "";
}
?>
<option value="<?php echo $course_id; ?>"<?php echo $selected; ?>><?php echo $row_Rsenroll['courseid']; ?></option>
<?php
}
?>
</select>
Thank you for any help and advice.
Assuming courseid is being passed as a variable in the sending URL (file.php?courseid=COURSEID), I think this should do what you want:
This might clean up your script a little (though I switched it to mysql_fetch_array as I'm more familiar with that than mysql_fetch_assoc. Feel free to use assoc):
<?php
$cid = '6116';
?>
<select name="courseidMenu">
<option value="" SELECTED>Selected Course ID</option>
<?php
$query = mysql_query("SELECT * FROM tbl_enroll WHERE courseid = '$cid'", $conn)or die(mysql_error());
$total_rows = mysql_num_rows($query);
while($row = mysql_fetch_array($query)){
$courseId = $row['courseid'];
?>
<option value="<?=$courseId?>" ><?=$courseId?></option>
<?
}
?>
</select>
updated use this it is working on my portal <select>
<option value=''>Select Provider</option>
<?php
$server="server name";
$user="user name";
$password="password";
$database="database";
$conn=mysql_connect($server,$user,$password) or die("connection failed");
mysql_select_db($database,$conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$result= mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$n=mysql_num_rows($result);
if($n>0)
while($row=mysql_fetch_array($rs))
echo"<option value='$row['courseid']'>$row['courseid']</option>";
mysql_close($conn);
?>