Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Onchange of select to refresh page

Posted on 2004-04-28
7
Medium Priority
?
3,887 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 14

Accepted Solution

by:
cracky earned 500 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:Andy
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:Andy
Andy earned 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

610 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