Solved

Onchange of select to refresh page

Posted on 2004-04-28
7
3,516 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

910 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

22 Experts available now in Live!

Get 1:1 Help Now