I've got two tables: Subjects and Careers :
"Subjects" includes (id, careers_id (is the foreign key to the column "id" of the table Careers) subject, description, hours)
"Careers" includes (id,name,description)
I put a button which allows me to add a new subject. So when I click on it another page open. I need to add a slider/select which shows me the careers available in the table career. Take a look,I need something like this :
Here is my code to add a new subject (it works,but i dont know how to make the slider/select which fetch data from the table careers :/)
<?php include('connect.php');
$error="";
if(isset($_POST['btnsave']))
{
$carreras_id=$_POST['txtcarreras_id'];
$subject=$_POST['txtsubject'];
$descripcion=$_POST['txtdescripcion'];
$carga_horaria=$_POST['txtcarga_horaria'];
if($_POST['txtid']=="0")
{
$a_sql=mysql_query("INSERT INTO subjects VALUES('','$carreras_id','$subject','$descripcion','$carga_horaria')");
if($a_sql)
{
header("location:index.php");
}
}else{
echo "Actualizar";
}
}
?>
<h2 align="center">ADD NEW SUBJECT</h2>
<form method="Post">
<table align="center">
<tr>
<td>Career:</td>
<td>
<input type='text' name='txtcarreras_id' />
<input type="hidden" name="txtid" value="0" />
</td>
</tr>
<tr>
<td>Subject:</td>
<td>
<input type='text' name='txtsubject' />
</td>
</tr>
<tr>
<td>Description:</td>
<td>
<input type='text' name='txtdescripcion' />
</td>
</tr>
<tr>
<td>Hours:</td>
<td>
<input type='text' name='txtcarga_horaria' />
</td>
</tr>
<tr>
<td></td>
<td>
<input type='submit' value=save name='btnsave' />
</td>
</tr>
</table>
</form>
I don't know what to do?
Hope you can help me!
Thanks!
To build your <option>s you would do something like this ->
$sql = mysql_query("SELECT * FROM Careers");
$options = "";
while($result = mysql_fetch_array($sql)){
$options .= "<option value='".$result['id']."'>".$result['name']."</option>";
}
(Note - you should update from mysql_ to MySQLi or PDO - MySQL: choosing an API )
Then you would change your <input> to a <select> with your $options ->
<tr>
<td>Career:</td>
<td><select name='txtcarreras_id'><?php echo $options; ?></select><input type="hidden" name="txtid" value="0" /></td>
</tr>
Related
How do I insert all employee attendance(Monthly once) in mysql database
Here is my db tables
att_employee_id | att_year | att_month | att_present_days | att_absent_days | att_workingdays
Date.prototype.daysInThisMonthOfThisYear=function() {
return new Date(this.getFullYear(),this.getMonth()+1,0).getDate();
}
var days_inmonth = new Date().daysInThisMonthOfThisYear();
$(document).ready(function() {
$( ".absent_days" ).keyup(function() {
var days = $(this).val();
$(this).closest("tr").find(".present_days" ).val( days_inmonth - days );
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<form name="emp_att" action="" method="post">
<table>
<thead>
<tr>
<th class="head0" style="width:5%">S.No</th>
<th class="head1" style="width:15%">Employee ID</th>
<th class="head0 no-sort" style="width:15%">No.of days present</th>
<th class="head1 no-sort" style="width:15%">No.of days absent</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>ETO0001</td>
<td><input type="text" class="present_days" name="present_days" value=""></td>
<td><input type="number" name="absent_days" class="absent_days" min="0" max="<?php echo date('t'); ?>" onChange="absentdays()" style="width:100%"></td>
</tr>
<tr>
<td>2</td>
<td>ETO0002</td>
<td><input type="text" class="present_days" name="present_days" value=""></td>
<td><input type="number" name="absent_days" class="absent_days" min="0" max="<?php echo date('t'); ?>" onChange="absentdays()" style="width:100%"></td>
</tr>
<tr>
<td>3</td>
<td>ETO0003</td>
<td><input type="text" class="present_days" name="present_days" value=""></td>
<td><input type="number" name="absent_days" class="absent_days" min="0" max="<?php echo date('t'); ?>" onChange="absentdays()" style="width:100%"></td>
</tr>
<tr>
<td>4</td>
<td>ETO0004</td>
<td><input type="text" class="present_days" name="present_days" value="" readonly></td>
<td><input type="number" name="absent_days" class="absent_days" min="0" max="<?php echo date('t'); ?>" onChange="absentdays()" style="width:100%"></td>
</tr>
</tbody>
</table>
<input type="submit" name="submit" value="Submit">
</form>
Here is my query to get all the employees
<form name="emp_att" action="" method="post">
<table>
<thead>
<tr>
<th class="head0" style="width:5%">S.No</th>
<th class="head1" style="width:15%">Employee ID</th>
<th class="head0 no-sort" style="width:15%">No.of days present</th>
<th class="head1 no-sort" style="width:15%">No.of days absent</th>
</tr>
</thead>
<tbody>
<?php
$allemp= mysqli_query($con,"select * from t_emp e");
$all_emp = mysqli_num_rows($allemp);
if ($all_emp > 0)
{
$i=1;
while($all_emp = mysqli_fetch_assoc($allemp))
{
echo "<tr>";
echo "<td>".$i."<input type='hidden' name='serialno' value='".$i++."'></td>
<td> ".$all_emp["emp_id"]."</td>
<td><input type='text' class='present_days' name='present_days' value=''></td>";
echo "<td><input type='number' name='absent_days' class='absent_days' min='0' max= date('t') onChange='absentdays()' style='width:100%'></a></td>";
echo "</tr>";
}
}
else
{
echo "0 Results";
}
?>
</tbody>
</table>
<input type="submit" name="submit" value="Submit">
</form>
Here I am getting all the n number of employees in the above form. when entered all employees "No.of days absent" and then hit on submit I am able to send only last record to the database.
Can any one help me to send all the employees attendance to database
Here is my form submission query
<?php
if(isset($_POST['submit']))
{
$serialno=$_post['serialno'];
$employee_id=$_post['emp_id'];
$att_year = date('Y');
$att_month = date('m');
for($i=1; $i<= $serialno;$i++)
{
$present_days=$_POST['present_days'];
$absent_days=$_POST['absent_days'];
}
$query=mysqli_query($con,"INSERT INTO t_emp_attendance(att_employee_id,att_year,att_month,att_present_days,att_absent_days)values('$employee_id','$att_year','$att_month','$present_days','$absent_days')");
}
?>
any suggestions will be appreciated, I need to store all the employees attendance to database
Check that name of input fields are created. It might be overwritten with the latest values. Send it as an array format like "absent_days[]" it will solve your problem
Check you spelling for INSERT. And also you have missed to pass the connection object for inserting
First parameter should be the connection object in mysqli_*
mysqli_query(connectionObject,your Query);
$query=mysqli_query("INSERT INTO t_emp_attendance(att_employee_id,att_year,att_month,att_present_days,att_absent_days)values('$employee_id','$att_year','$att_month','$present_days','$absent_days')");
for($i=1; $i<= $serialno;$i++)
{
$present_days=$_POST['present_days'][$i];
$absent_days=$_POST['absent_days'][$i];
$query=mysqli_query($con,"INSERT INTO t_emp_attendance(att_employee_id,att_year,att_month,att_present_days,att_absent_days)values('$employee_id','$att_year','$att_month','$present_days','$absent_days')");
}
Your query should be inside the loop. And as suggested by parag, make text field names as arrays []
Soomething like this,
<input type="text" class="present_days" name="present_days[]" value="">
^ ^
You shud do for all the text boxes.
I have a classrooms in schools and when I click on a certain classroom, I want to add students into it but my actual code is doing something stupid. It adds a student but i can see the student in all classrooms, not just in the one that i added him into. So when Im in classroom number 1, I see a form in there, I can add a student there, ... see how it works here:
here is the code: http://www.xxxx.xx/projekt/
here is my code in file trieda.php
<table align="center"><tr><td>
<form action="vlozit2.php" method="post">
Meno: <input type="text" name="meno" placeholder="Janko" maxlength="15" required>
Priezvisko: <input type="text" name="priezvisko" placeholder="Hruška" maxlength="20" required>
<input type="hidden" name="id_triedy" value="<?= $trieda['id_triedy'] ?>" />
<input type="submit" name="submit" value="Pridať študenta do triedy">
</form>
</td></tr></table>
<?php
$result = mysqli_query($prip,"SELECT * FROM student ORDER BY meno");
while($student = mysqli_fetch_array($result))
{
echo "<br /><table cellspacing='1' cellpadding='1' class='tabulka1' align='center'><tr>";
echo "<td width='200'><a href='student.php?id_triedy=".$trieda['id_triedy']."".id_student=".$student['id_student']."'>".$student['meno']." ".$student['priezvisko']."</a></td>";
?>
<td width='300px' align='right' bgcolor="#fbfbfb">Zmazať</td>
</tr></table>
<?php
}
?>
here is vlozit2.php (a code that works for the form to add a student)
if(isset($_POST['submit']))
{
//meno a priezvisko
$student = $_POST['meno'];
$student = $_POST['priezvisko'];
$trieda = $_POST['id_triedy'];
//connect to the database
include 'config.php';
//insert results from the form input
$sql = "INSERT INTO student (meno, priezvisko, id_triedy) VALUES('$_POST[meno]', '$_POST[priezvisko]', '$_POST[id_triedy]')";
$add = "<table align='center'>
<tr>
<td> Študent bol úspešne pridaný do triedy. </td>
</tr>
<tr>
<td><a href='./trieda.php'><strong>Späť</strong></a></td>
</tr>
</table>";
$not_add = "<table align='center'>
<tr>
<td> Študent s týmto menom a priezviskom už je v tejto triede. </td>
</tr>
<tr>
<td><a href='./trieda.php'><strong>Späť</strong></a></td>
</tr>
</table>";
if (mysqli_query($prip, $sql)) {
echo $add;
}else{
echo $not_add;
}
mysqli_close($prip);
}
?>
Try to replace your part of code with these snipets:
1) in trieda.php
<form action="vlozit2.php?id_triedy=<?php echo $_GET["id_triedy"];?>" method="post">
Meno: <input type="text" name="meno" placeholder="Janko" maxlength="15" required>
Priezvisko: <input type="text" name="priezvisko" placeholder="Hruška" maxlength="20" required>
<input type="submit" name="submit" value="Pridať študenta do triedy">
</form>
2) in vlozit2.php
$student = $_POST['meno'];
$priezvisko = $_POST['priezvisko'];
$id_trieda = $_GET['id_triedy'];
and
$sql = "INSERT INTO student (meno, priezvisko, id_triedy) VALUES( '{$student}', '{$priezvisko}', {$id_trieda} )";
Hopefully you store your id_trieda as INT type.
In your vlozit2.php file is nothing about inserting of class id. So put
<input type="hidden" name="classId" value="<?= $trieda['id'] ?>" />
to your form and in vlozit2.php get this value from $_POST['classId'] and insert it with other students data or anywhere you want to have it.
my problem goes like this:
my home page has tables with rows pulled from the database (while loops)
each row has a - cell in which he can add an event to that specific row
in order to do that i send the row id as a $_GET variable from the home page table
and in the "add event" page i store it as a variable
but when i submit my addevent form without filling it properly (as i coded) it simply refreshes the form only without the row id in the url therefor also the query i do in the beginning of the page for pulling the row data can no longer execute and that pops a PHP error
for the id variable which i sign it the $_GET and the query (mysql fetch array).
also of course all the data which i display in the form from that query is gone.
any suggestions on how to approach this ? thanks in advance, Regards.
EDIT:** kill the new guy! -Sorry i guess
home page where i send the id :
$sql = "SELECT * FROM alarms WHERE alarmstatus = 'OFF' and starttime='::' ORDER BY clientid ASC";
$query = mysql_query($sql);
echo "<table cellpadding='1px' border='1px' bordercolor='#0066FF' cellspacing='0'>
<form action='hpage.php' method='get'>";
while($fetch = mysql_fetch_array($query)) {
echo "<tr>
<td>
".$fetch['clientid']."</td>
<td>".$fetch['controller']."</td>
<td>".$fetch['typeid']."</td>
<td style='color: red'>".$fetch['alarmstatus']."</td>
<td>".$fetch['starttime']."</td>
<td>".$fetch['endtime']."</td>
<td><a href='includes/editalarm.php?id=".$fetch['id']."'>Edit</a></td>
<td><a href='includes/addevent.php?id=".$fetch['id']."'>Add event</a></td>
<td><a href='includes/deletealarm.php?id=".$fetch['id']."'>Delete</a></td>
</tr>";
}
the add event where i get the variable and make the query:
$alarmid = $_GET['id'];
$sql = "SELECT * FROM alarms WHERE id=".$alarmid;
$query = mysql_query($sql);
$fetch = mysql_fetch_array($query);
?>
the form:
<table cellpadding="2px" cellspacing="0" >
<form action="addevent.php" method="post">
<tr>
<td>סניף:</td>
<td><input style="width:200px; background-color: #d6d6d6;" readonly name="client" value="<?php echo $fetch['clientid']; ?>" /></td>
</tr>
<tr>
<td>בקר:</td>
<td><input style="width:200px; background-color: #d6d6d6;" readonly name="controller" value="<?php echo $fetch['controller']; ?>" /></td>
</tr>
<tr>
<td>אזעקה:</td>
<td><input style="width:200px; background-color: #d6d6d6;" readonly name="controller" value="<?php echo $fetch['typeid']; ?>" /></td>
</tr>
<tr>
<td>מוקדן:</td>
<td>
<?php
$sql = "SELECT * FROM users WHERE privilege = '2'";
$query = mysql_query($sql);
echo "<select name='user' style='width:207px;'>";
echo "<option>..</option>";
while ($fetch2 = mysql_fetch_array($query)){
echo "<option>".$fetch2['username']."</option>";
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td>איש קשר:</td>
<td><input type="text" name="contact" /></td>
</tr>
<tr>
<td>הודעה:</td>
<td><input type="text" style="width:200px; height:100px" name="message" /></td>
</tr>
<tr>
<td>תשובה:</td>
<td><input type="text" style="width:200px; height:100px" name="answer" /></td>
</tr>
<tr>
<td>שעה:</td>
<td>
<select name="eventhour">
<option value ="default"></option>
<?php
for($i = 0; $i<60; $i++){
$value = $i;
if($i<=9){
$value= "0".$i;
}
echo "<option>".$value."</option>";
}
?>
</select>
<select name="eventminute">
<option value ="default"></option>
<?php
for($i = 0; $i<24; $i++){
$value = $i;
if($i<=9){
$value= "0".$i;
}
echo "<option>".$value."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" name="save" value="שמור" />
<input type="submit" name="cancell" value="בטל" />
</td>
<td></td>
</tr>
</form>
Your form action is POST. If you change that to GET then you will have the form as $_GET.
I want to let the user select one "row" to use to submit the with to request a report type. How can I put radio buttons in the first column of a table and whichever is selected is the active row that gets sent to the next page via the submit button?
I think Andreas is on the right track, but it's not as useful as it could be. This should be a bit better:
<?php
blah ...
echo <<<HTML
<form action="handler.php" action="post">
<table>
HTML;
foreach ($rows as $row)
{
$id = $row['id'];
$text = $row['text']; // escape this unless you know it's safe
echo <<<HTML
<tr>
<td><input type="radio" value="$id" name="theRadioButton" /></td>
<td><input type="text" name="textfield_$id" value="$text" /></td>
</tr>
HTML;
}
echo <<<HTML
</table>
</form>
HTML;
form handler:
<?php
$id = isset($_POST['theRadioButton']) ? $_POST['theRadioButton'] : null;
if ($id)
{
$textfield = $_POST["textfield_$id"];
}
?>
If you want to do it in pure PHP, i guess you could do this:
<form action="ascript.php" action="post">
<table>
<tr>
<td><input type="radio" value="row1" name="theRadioButton" /></td>
<td><input type="text" name="row1textfield" /></td>
</tr>
<tr>
<td><input type="radio" value="row2" name="theRadioButton" /></td>
<td><input type="text" name="row2textfield" /></td>
</tr>
<tr>
<td><input type="radio" value="row3" name="theRadioButton" /></td>
<td><input type="text" name="row3textfield" /></td>
</tr>
</table>
</form>
ascript.php
<?php
if ($_POST['theRadioButton'] == "row1") {
echo $_POST['row1textfield'];
// Handle row 1 ..
}
else if ($_POST['theRadioButton'] == "row2") {
echo $_POST['row2textfield'];
// Handle row 2 ..
}
else if ($_POST['theRadioButton'] == "row3") {
echo $_POST['row3textfield'];
// Handle row 3 ..
}
?>
However, if you're willing to use some jQuery, you could just name the textfields the same thing and disable the fields you're not going to use. Here's a fiddle: http://jsfiddle.net/rrvQu/1/
I'm trying to get the emails corresponding to the checkbox using the following codes. But, I'm not getting the correct checked emails in the new variable. Can anyone please check ??
<?php
include("connection.php");
$username=$_SESSION['username'];
$query=mysql_query("SELECT * FROM contacts WHERE username='$username'");
$num=mysql_num_rows($query);
$info=mysql_fetch_array($query);
$i=0;
$msg='';
?>
<table width="672" border="0">
<?php
$i=0;
while($info)
{
?>
<form action="compose.php" method="post">
<tr style="font-size:14px;">
<td width="21" bgcolor="#f2f2f2"> <input type="checkbox" name="add" onSelect="<?php $msg=$msg.$info['email'].", ";?>"/> </td>
<td width="229" bgcolor="#f2f2f2"> <?php echo $info['email']; ?> </td>
<td width="408" bgcolor="#f2f2f2"> <?php echo $info['name']; ?> </td>
</tr>
<?php
$info=mysql_fetch_array($query);
$i++;
}
$_SESSION['contacts']=$msg;
?>
<tr><td></td><td></td><td><br />
<input class="new-button" type="submit" value="Insert & Compose" name="submit" /></td>
</tr>
</form>
</table>
To get any value back for checkboxes they must have a value=. In your case you probably would want the value to be the according email address.
One problem with your code is using onSelect= instead of value=, and second you didn't print the actual value into the page. Rewrite it to:
<td width="21" bgcolor="#f2f2f2">
<input type="checkbox" name="add"
value="<?php print $info['email']; ?>"/> </td>
If you need the $msg variable to do something, assemble it after the output.
<input type="checkbox" name="add" value="<?php echo $msg.$info['email'];?>"/>
checkbox does not have onSelect event probobly you got value in mind and in PHP code you should echo and what .", " is for?