?
Solved

Onchange of select to refresh page

Posted on 2004-04-28
7
Medium Priority
?
3,798 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

752 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