Solved

Onchange of select to refresh page

Posted on 2004-04-28
7
3,541 Views
Last Modified: 2013-12-12
Ok. I'm using PhP and MySQL.
I have a form with a select drop down menu for studentid. When I select the id I want it to automatically refresh the page passing php variables.
So i guess we need something on the OnChange within the select tag, and then some javascript, or can we just use php, we would like to just use php and not javascript code.
0
Comment
Question by:kephillips
  • 2
  • 2
7 Comments
 
LVL 14

Accepted Solution

by:
cracky earned 125 total points
ID: 10939985
You can't really do this without JavaScript. Thankfully, it's really easy.

Some of the code is copied from: http://www.umd.edu/HTMLClasses/JavaScript/events/change.html

<SCRIPT LANGUAGE="JavaScript">
<!--

function jumpTo(URL_List){
   var URL = URL_List.options[URL_List.selectedIndex].value;
   window.location.href = URL;
}

// -->
</SCRIPT>

In your case, you would loop through your db values and pass the id in your querystring to the new page:

<?php
// Assuming your student columns in the db are labeled "id" and "name"
$rs_students = mysql_query($your_query);
if (mysql_num_rows($rs_students)) {
?>
<FORM>
<SELECT NAME="site" onChange="jumpTo(this);">
<?php
  while ($r_student = mysql_fetch_object($rs_students)) {
?>
<OPTION VALUE="show_student.php?student_id=<?php echo $r_student->id; ?>"><?php echo $r_student->name; ?>
<?php
  }
?>
</SELECT>
</FORM>
<?php
}
?>

On the show_student.php page, you can then use the $_GET['student_id' super global to access the id value of the student and make another db query accordingly.

Is this enough information?
0
 
LVL 14

Expert Comment

by:cracky
ID: 10940028
To do this without JavaScript, you would need to submit the form to the next page and access the variable via $_POST['student_id'] like so:

<?php
// Assuming your student columns in the db are labeled "id" and "name"
$rs_students = mysql_query($your_query);
if (mysql_num_rows($rs_students)) {
?>
<FORM ACTION="show_student.php" METHOD="POST">
<SELECT NAME="student_id">
<?php
  while ($r_student = mysql_fetch_object($rs_students)) {
?>
<OPTION VALUE="show_student.php?student_id=<?php echo $r_student->id; ?>"><?php echo $r_student->name; ?>
<?php
  }
?>
</SELECT>
<INPUT TYPE="submit" NAME="Submit" VALUE="Submit">
</FORM>
<?php
}
?>
0
 

Author Comment

by:kephillips
ID: 10941944
Actually I don't NEED to go to a new page, but I guess I have to refresh it and fill in the values to the form according to the given id. How would you do it this way?
0
 
LVL 6

Expert Comment

by:Reapz
ID: 10953369
Like cracky said....

You need to submit the value of the Select and use that to pull new info from your DB.

At the top of your page fetch the submitted value of the select or set a default if it's the first time the page is viewed...

$sid = $_POST['studentid']
if($sid == ""){
     $sid = "DEFAULT VALUE HERE";
}

Enclose the select in a form (lets say it's called form1) then submit it when the selection is changed like this...

<select name="studentid" id="studentid" onChange="javascript:document.form1.submit()">
     <option value"INSERT VALUE HERE"<?php if (!(strcmp($r_student['student_id'], "$catid"))) {echo "SELECTED";} ?>><?php echo stripslashes($row_categories['cat_name']); ?></option>
</select>



0
 
LVL 6

Assisted Solution

by:Reapz
Reapz earned 125 total points
ID: 10953405
Sorry! Hit Submit by mistake... I'll try again. (Admin please delete the above comment)

Like cracky said....

You need to submit the value of the Select and use that to pull new info from your DB.

At the top of your page fetch the submitted value of the select or set a default if it's the first time the page is viewed...

$sid = $_POST['studentid']
if($sid == ""){
     $sid = "DEFAULT VALUE HERE";
}

Enclose the select in a form (lets say it's called form1) then submit it when the selection is changed like this...

<select name="studentid" id="studentid" onChange="javascript:document.form1.submit()">
     <option value"INSERT VALUE HERE"<?php if (!(strcmp($r_student['student_id'], "$sid"))) {echo "SELECTED";} INSERT TEXT TO DISPLAY HERE</option>
     etc...
</select>

Then display the relevant info...
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question