Solved

Onchange of select to refresh page

Posted on 2004-04-28
7
3,493 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now