Reputation: 201
I'm looking for a simple solution to this. The code is using JQuery and PHP. What I have is 2 forms - 1 selects a student, and the value is a 2 digit code. Another form uploads a document for that student, and I need the student_id from the first form to get submitted with the 2nd form. I've looked, and wasn't able to see a solution that quite worked. Just looking for something simple, and if there's another post that works I'd gladly take a link to that :) The first form: (I apologize for the formatting, its not coming out right and I'm late for school)
<form>
<select id="student_id" name="student_id" onChange="showInvoicesForStudent();">
<option value=''>Select Student</option>
<?php
$temp = mysql_query("select * from students order by lname");
while ($row = mysql_fetch_array($temp)) {
echo "<option value='" . $row['student_id'] . "'>" . $row['lname'] . ", " . $row['fname'] . "</option>";
}
echo "</select>";
$DB->close();
?>
</form>
The 2nd (where I'm trying to get the student_id from above into:
<form name="upload_invoice" method="post" action="upload_invoice.php" enctype="multipart/form-data" >
<input type="hidden" value="document.getElementById('student_id');" id="student_id"/>
Upvotes: 2
Views: 2839
Reputation: 1770
**this code will give you the exact result**
//upload_invoice.php
print_r($_POST);
//index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" >
function showInvoicesForStudent(student_id)
{
document.getElementById('studentForm_id').value=student_id;
document.upload_invoice.submit();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<select id="student_id" name="student_id" onChange="showInvoicesForStudent(this.value);" >
<option value=''>Select Student</option>
<?php
$temp = mysql_query("select * from students order by lname");
while ($row = mysql_fetch_array($temp)) {
echo "<option value='" . $row['student_id'] . "'>" . $row['fname'] . ", " . $row['lname'] . "</option>";
}
$DB->close();
?>
</select>
<form name="upload_invoice" method="post" action="upload_invoice.php" enctype="multipart/form-data" >
<input type="hidden" name="studentForm_id" id="studentForm_id"/>
</form>
</body>
</html>
Upvotes: 0
Reputation: 1526
If both forms are on the same page you could either combine them or if this is not possible you can use jQuery to add the value from the first form to the second form. In that case you need to change the ID of the second form, because it is not allowed to have two HTML elements with the same ID on the same page. (the following code is untested)
<script>
$(document).ready(function(){
$('#student_id').change(function() {
$('#student_id2').val($('#student_id).val());
});
});
</script>
If the forms are on different pages you do not need to use jQuery, but rather use $_GET
or $_POST
(depending on the method of the form). If the second form is not on the page that immediately followed the first page, you need to save the student_id in a cookie (using set cookie
) and access it with ($_COOKIE
)
Upvotes: 1
Reputation: 7055
function getCodeIdForStudent(){
var selectedBox=document.getElementById('student_id');
var selectedItem=selectedBox.value;
var hiddenText=document.getElementById('hidden_text_id');
hiddenText.value=selectedItem;
}
and <select id="student_id" name="student_id" onChange="getCodeIdForStudent();">
Upvotes: 0
Reputation: 11
Two forms means: Two separate php files?
solution (one php file): The little javascript code you placed at "value" is not that bad. But a) it is not recognized as javaScript and b) it is the wrong place. You should place it at the button submit (onClick). However if both forms are placed on the same page: Why do you have two separate forms? If you embed the student_id field into the second form it will be submitted.
solution (two php files): However if the 2nd form is placed on another php file you won't ever getting access to the first form by javascript. Therefor you should use php tags and "$_REQUEST['student_id']" to print the value. (Keep in mind to do good escaping so that no one can break into this form).
Upvotes: 1