Jump to content

need help


mona

Recommended Posts

Iam working with my project and i had no timeIam working with a php, mysql program, In this program I have combobox for student_id I need onchange of this combo to execute another select statement (for example select name from student where studentid=...) and put the name in a text boxmy problem is as follows:onchange I need to call a javascript function and inside it run a php that selects from table where studentid=(the value of the combo). how can I do that, that is passing javascript variables or values to php I tried to make submit to the same form but it is not working this is my php file:add1.php

<?php$db = mysql_connect('localhost','root', 'password');mysql_select_db ("collab1",$db);$sql=mysql_query("select Id from student",$db);?><html><title>New Violation</title><body><form name='form1' method='post' action='add1.php'>Student Id:<select name='sid' onchange='getname()'><?while ($row = mysql_fetch_array($sql, MYSQL_NUM)) {?><option value='<?= $row[0]?>'><?= $row[0]?><?  }?></select><br>Name:<input name='sname' type='text'/><input type='submit' value='Add New Violation'/></form><script language='javascript'>function getname(){{ form1.submit(); <?if(isset($_POST["sid"])){$sql=mysql_query("select * fom student where student_id=".$_POST["sid"],$db);$row=mysql_fetch_row($sql);}?>form1.sname.value=<?=$row["sname"]?>;}</script></html>

[edit] wrapped code in

 tags - skemcin[/i]
Edited by Skemcin
Link to comment
Share on other sites

You already used PHP to populate the combobox. The rest is straight JavaScript.

function getname(){    var sel = document.form1.sid;    var selectedName = sel.options[sel.selectedIndex].value;    document.form1.sname.value = selectedName;}

That should work

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...