Jump to content

Dynamic Form


superdan_35

Recommended Posts

Hi all.

 

I have a basic form with a drop down list then some text boxes whose purpose is to edit data stored in a database. The user would select what they want to edit from the drop down list and the current values would dynamically appear in the text boxes ready to be changed as required.

 

Aside from the database, the form and populating the drop down I have nothing! I am aware that I could use a separate php script through the form action but I don't want to add an extra click as the user has already made two to get to this page.

 

I therefore am thinking down the route of a java onChange event, which in know isn't php but both languages are going to need to be incorporated, aren't they?

 

Any suggestions?

 

Thanks,

Dan

Link to comment
https://forums.phpfreaks.com/topic/150756-dynamic-form/
Share on other sites

Thanks for the quick reply.

 

If the drop down has say 10 items, each with the id from the database as the key, could I use the onChange to set a variable called, for example, selected. Then use selected in a query to find all the associated details and add them to the text boxes.

 

If this is correct I have the code set up below, but I don't know what to do for the onChange?

 

// Fill the drop down
<select name="staffname" >';
$i = 0;
while ($i < $numstaff)
{
echo '<OPTION value='.$id[$i].'> '.$name[$i].'';
$i++;
}	
echo '</select>';

// Get the chosen ID and run query
$selected = ;
$query = mysql_query("SELECT * FROM staff WHERE staff_id LIKE $selected");

// Display result in text text boxes

Link to comment
https://forums.phpfreaks.com/topic/150756-dynamic-form/#findComment-792056
Share on other sites

The javascript would have to go inside the HTML, not PHP.

<select name="staffname" onchange="document.form.selectname.value='something here'">

 

Like that although you'd have to change the selectname to the name of the second selection box and modify the Javascript slightly - it isn't really "my thing"

Link to comment
https://forums.phpfreaks.com/topic/150756-dynamic-form/#findComment-792083
Share on other sites

Ok, this is where I am!

 

I currently have the following for my form:

 

// Show drop down of staff members
<select name="staffname" onchange="showStaff(this)">
$i = 0;
while ($i < $num)
{
     echo '<OPTION value='.$assos_id[$i].'> '.$staff[$i].'';
     $i++;
}    
</select>

// Run query based on selection
$query = @mysql_query("SELECT * FROM staff WHERE staff_id LIKE $staff_id");
$row = mysql_fetch_object($query);

// Show results
First Name: <input type="text" name="fname" value="'.$row->fname.'"/>
Surname: <input type="text" name="lname" value="'.$row->lname.'"/>
// And do on

The JavaScript function has been created to show it works but has no relevant functionality:

<script language="javascript">
function showStaff(choice)
{
    var choice = choice.options[choice.selectedIndex].value;
    alert (choice);
}
</script>

 

So how do I manipulate the php in the function and send it back? Or in otherwords, how do I get choices from the JavaScript to be staff_id in the php?

 

Thanks again,

Dan

Link to comment
https://forums.phpfreaks.com/topic/150756-dynamic-form/#findComment-792765
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.