I would like to add new ROWS and COLUMNS into another existing TABLE within the same page. How could I do that easily?
Here is the code:
$sql = mysql_query("SELECT * FROM tblsubjecschedule ORDER BY Subject");
$i=0;
$b =1;
while($row=mysql_fetch_assoc($sql) ){
echo "<tr><td>".$row['Subject']."<input type='hidden' name='subj[$i]' value=".$row['Subject']."></td>
<td>".$row['Lec']."<br/>".$row['Lab']."<input type='hidden' name='leclab[$i]' value=".$row['Lec']."><input type='hidden' name='lab[$i]' value=".$row['Lab']."></td>
<td>".$row['Descriptive']."<input type='hidden' name='desc[$i]' value=".$row['Descriptive']."></td>
<td>".$row['Day']."<br/>".$row['Labday']."<input type='hidden' name='daylabday[$i]' value=".$row['Day']."><input type='hidden' name='labday[$i]' value=".$row['Labday']."></td>
<td>".$row['LecTime']."<br/>".$row['LabTime']."<input type='hidden' name='lectlabt[$i]' value=".$row['LecTime']."><input type='hidden' name='labtime[$i]' value=".$row['LabTime']."></td>
<td>".$row['Room']."<br/>".$row['Labroom']."<input type='hidden' name='roomlabroom[$i]' value=".$row['Room']."><input type='hidden' name='labroom[$i]' value=".$row['Labroom']."></td>
<td><input id='send' name='reserv[$i]' type='submit' value='Add' [$b] /></td></tr>";
$i++;
}
?>
</tbody>
</table>
<table border="1" style='width: 900px;'>
<thead>
<tr>
<th>SubjectCode <th>Units <th>Time <th>Day <th>Room</th>
</tr>
</thead>
and this is where I display the data but it displays one row only
<?php
if (isset($_POST['reserv'])){
if(!empty($_POST['reserv'])){
$i = current(array_keys($_POST['reserv']));
$subj=$_POST['subj'][$i];
$leclab=$_POST['leclab'][$i];
$lab=$_POST['lab'][$i];
$labday=$_POST['labday'][$i];
$desc=$_POST['desc'][$i];
$daylab=$_POST['daylabday'][$i];
$lectlabt=$_POST['lectlabt'][$i];
$labtime=$_POST['labtime'][$i];
$roomLabroom=$_POST['roomlabroom'][$i];
$labroom=$_POST['labroom'][$i];
}
}
if (isset($_POST['reserv'])){
$count=0;
$a=0;
$c=current(array_keys($_POST['reserv']));
if (!empty($c)){
$a=1;
for ($count;$count<=$a;$count++){
echo "<tr>
<td>".$subj."</td>
<td>".$leclab."<br/>".$lab."</td>
<td>".$lectlabt."<br/>".$labtime."</td>
<td>".$daylab."<br/>".$labday."</td>
<td>".$roomLabroom."<br/>".$labroom."</td>
</tr>";
}
}
}
?>
dude as it enter the loop
for ($count;$count<=$a;$count++)
value 0f a=1 and count=0
now first it executes when count=0 and den again wen count=1 bt all dis time value of a remains same that is a=1
now wen count=2 count<=1 (false)
dats why onie 2 rows are displayed
for every row u making different forms so wen submitted a form,data is submitted of one form and wen u run the iteration after post twice in the loop dat value comes two times
Related
I just need to send a line of a table in my PHP page after click on button.
This is my code :
$login = $_POST['login'];
$requete = Connexion::query("SELECT id
FROM session
WHERE login='$login'");
$requete = $requete[0][0];
$application = Connexion::query("SELECT titre,cleAk,cleAs,cleCk
FROM cles
WHERE sessionId='$requete'");
echo "<form class='login-form' method='POST'><table class='table'><th>Titre</th><th>Cle ak</th><th>Cle as</th><th>Cle ck</th><tr>";
for($i=0;$i<sizeof($application);$i++)
{
for($j=0;$j<4;$j++)
{
echo "<td><input type='hidden' name='titre' value='".$application[$i][$j]."'>".$application[$i][$j]."</td>";
}
echo "<td><button type='submit' name='api'>Submit</button></td></tr></form>";
}
And this var_dump($_POST['titre']); return the last cell of the last line.
Can you help me ?
As was briefly mentioned in the comments of a previous answer, if you only want to submit the contents of one line, then you will need a separate <form> on every line (table row). You were trying this with a closing </form> inside the loop, but you opened it outside the loop resulting in invalid HTML with lots of closing </form> tags but only one opening <form> tag.
Another problem with your form inputs is that they were all named the same after the first column name='titre' but you had specified each column separately in the table header. To fix this, others offered an imprecise option by creating an array name='titre[]', but I think it's better to dynamically name the inputs to match the column key.
I don't know what database API you're using, but it appears to only produce a numeric multidimensional array without associative keys. There's probably a class method to get the column names, or you can manually assign it based on your SQL query as I have done with the array $cols
<?php
// debugging to show you everything posted
print_r($_POST);
// generate some random data for example
$application[0][0] = "first titre";
$application[0][1] = "first cleAk";
$application[0][2] = "first cleAs";
$application[0][3] = "first cleCk";
$application[1][0] = "second titre";
$application[1][1] = "second cleAk";
$application[1][2] = "second cleAs";
$application[1][3] = "second cleCk";
$application[2][0] = "third titre";
$application[2][1] = "third cleAk";
$application[2][2] = "third cleAs";
$application[2][3] = "third cleCk";
// get the columns for input names
$cols = array('titre','cleAk','cleAs','cleCk');
// semantically complete HTML table
echo "
<table class='table'>
<thead>
<tr><th>Titre</th><th>Cle ak</th><th>Cle as</th><th>Cle ck</th><th>Submit</th></tr>
</thead>
<tbody>
";
for($i=0;$i<sizeof($application);$i++)
{
echo "
<tr>
<form class='login-form' method='POST'>";
for($j=0;$j<sizeof($cols);$j++)
{
echo "
<td><input type='hidden' name='".$cols[$j]."' value='".$application[$i][$j]."'>".$application[$i][$j]."</td>";
}
echo "
<td><button type='submit' name='api'>Submit</button></td>
</form>
</tr>\n";
}
echo "
</tbody>
</table>
";
Results in this rendered output when I click on the first row's submit button. Note that you can't post data in the snippet below, this is purely for static HTML demonstration purposes.
Array
(
[titre] => first titre
[cleAk] => first cleAk
[cleAs] => first cleAs
[cleCk] => first cleCk
[api] =>
)
<table class='table'>
<thead>
<tr><th>Titre</th><th>Cle ak</th><th>Cle as</th><th>Cle ck</th><th>Submit</th></tr>
</thead>
<tbody>
<tr>
<form class='login-form' method='POST'>
<td><input type='hidden' name='titre' value='first titre'>first titre</td>
<td><input type='hidden' name='cleAk' value='first cleAk'>first cleAk</td>
<td><input type='hidden' name='cleAs' value='first cleAs'>first cleAs</td>
<td><input type='hidden' name='cleCk' value='first cleCk'>first cleCk</td>
<td><button type='submit' name='api'>Submit</button></td>
</form>
</tr>
<tr>
<form class='login-form' method='POST'>
<td><input type='hidden' name='titre' value='second titre'>second titre</td>
<td><input type='hidden' name='cleAk' value='second cleAk'>second cleAk</td>
<td><input type='hidden' name='cleAs' value='second cleAs'>second cleAs</td>
<td><input type='hidden' name='cleCk' value='second cleCk'>second cleCk</td>
<td><button type='submit' name='api'>Submit</button></td>
</form>
</tr>
<tr>
<form class='login-form' method='POST'>
<td><input type='hidden' name='titre' value='third titre'>third titre</td>
<td><input type='hidden' name='cleAk' value='third cleAk'>third cleAk</td>
<td><input type='hidden' name='cleAs' value='third cleAs'>third cleAs</td>
<td><input type='hidden' name='cleCk' value='third cleCk'>third cleCk</td>
<td><button type='submit' name='api'>Submit</button></td>
</form>
</tr>
</tbody>
</table>
You should pass an array to handle the multiple elements with the inputs. In Your code it should be look like this:
for($j=0;$j<4;$j++)
{
echo "<td><input type='hidden' name='titre[$j]' value='".$application[$i][$j]."'>".$application[$i][$j]."</td>";
echo "<td><button type='submit' name='api'>Submit</button></td></tr></form>";
}
The modification is name='titre[]'. Hope this will help you
I want to insert multiple records from a looped table. Each row have hidden inputs.
I will POST the total number of rows for the table which will be used in the loop condition.
Here is my view:
<input type="hidden" name="rowcount">//total row of the table
<table class="table" id="ctable" style="border:0;">
<?php
foreach ($row12 as $row12) {
echo"<tr>
<td style='vertical-align:midle; width:175px; border:0; padding-right:20px;' align='right' ><b>".$row12->mapel_un."</b></td>
<td style='border:0;'>
<input type='hidden' name='id_peserta' value='$row13->id_peserta'>
<input type='hidden' name='id_un' value='$row12->id_un'>
<input class='form-control' type='text' name='nilai' value='$nilai' />
</td>
</tr>";
}
?>
</table>
Here is my model:
function simpan_nilai(){
$jumlah = $this->input->post('rowcount');
$x=1;
while($x<=$jumlah){
$this->db->query("INSERT INTO nilai SET
id_peserta = '".$this->input->post('id_peserta')."',
id_un = '".$this->input->post('id_un')."',
nilai = '".$this->input->post('nilai')."' ");
$x++;
}
return "info-Data berhasil disimpan ...";
}
I've only been able to insert the last row of data. For example, if the data is like (001, 1, 100), (001, 2, 90), (001, 3, 95), only the last row of record is inserted into the database i.e. (001, 3, 95).
Please help me. Thanks!
closed.
i already fix my problem.
thanks to all who helped me
View:
<input type='text' name='rowcount'>
<table class="table" id="ctable" style="border:0;">
<?php
foreach ($row12 as $row12) {
echo"<tr>
<td style='vertical-align:midle; width:175px; border:0; padding-right:20px;' align='right' ><b>".$row12->mapel_un."</b></td>
<td style='border:0;'>
<input type='hidden' name='id_peserta' value='$row13->id_peserta'>
<input type='hidden' name='id_un$row12->id_un' value='$row12->id_un'>
<input class='form-control' type='text' name='nilai$row12->id_un' value='$nilai' />
</td>
</tr>";
}
?>
</table>
Model:
function simpan_nilai(){
$jumlah = $this->input->post('rowcount');
$x=1;
while($x<=$jumlah){
$nilai = 'nilai' . $x;
$id_un = 'id_un' . $x;
$this->db->query("INSERT INTO nilai SET
id_peserta = '".$this->input->post('id_peserta')."',
id_un = '".$this->input->post($id_un)."',
nilai = '".$this->input->post($nilai)."' ");
$x++;
}
return "info-Data berhasil disimpan ...";
}
The reason is because you have multiple hidden inputs of the same name id_peserta. In your PHP codes, the only input that is received is the last input of the same name.
You can try to append a count after each of your input name to make it unique.
<input type="hidden" name="rowcount"> <!-- total row of the table -->
<table class="table" id="ctable" style="border:0;">
<?php
$count = 1; // You can start from zero. Either works.
foreach ($row12 as $row12) {
echo "<tr>
<td style='vertical-align:midle; width:175px; border:0; padding-right:20px;' align='right' ><b>".$row12->mapel_un."</b></td>
<td style='border:0;'>
<input type='hidden' name='id_peserta{$count}' value='$row13->id_peserta'>
<input type='hidden' name='id_un{$count}' value='$row12->id_un'>
<input class='form-control' type='text' name='nilai{$count}' value='$nilai' />
</td>
</tr>";
$count++;
}
?>
</table>
And for your PHP codes.
function simpan nilai(){
$jumlah = $this->input->post('rowcount');
$x=1;
while($x<=$jumlah){
$this->db->query("INSERT INTO nilai SET
id_peserta = '".$this->input->post('id_peserta' + $x)."',
id_un = '".$this->input->post('id_un' + $x)."',
nilai = '".$this->input->post('nilai' + $x)."' ");
$x++;
}
return "info-Data berhasil disimpan ...";
}
So I have this program where the user sets up a database table. First, I ask them how many fields they want.
first.php
<html>
<form name="formCreateFields" method="post" align="center" action="second.php">
<p>Number of fields: <input type ="text" name="fieldsNum"/>
<input type="submit" name="submitFieldsNum" value=" Submit "/></p><br>
</form>
</html>
Then I loop the fields, depending on their input above.
second.php
<?php
echo "<form name='formSetupFields' method='post' align='center' action='third.php'>";
for ($z=1; $z<=$_POST['fieldsNum']; $z++) {
echo "<table align='center'>
<tr>
<th rowspan=2> <big> $z </big> </th> <th>Name</th> <th>Type</th> <th>Length</th>
</tr>
<tr>
<td><input type='text' name='fieldName$z'></td>
<td><input type='text' name='fieldType$z'></td>
<td><input type='text' name='fieldLength$z'></td>
</tr>
</table><br><br>";
}
<input type='submit' name='submitFieldSetup'>
</form>";
?>
I'm having problems after this. I've been trying to test fetching them by putting them in an array and using foreach to view them but can't seem to get anywhere. I thought it was okay to use something like $_POST['fieldName$z'] but I guess I was wrong.
I just need to find out how I could fetch all the inputs in the second file. Any ideas? Thanks in advance! :)
If fieldsNum is a number, the for should be fine in this case, just properly concatenate the values:
echo "<form name='formSetupFields' method='post' align='center' action='third.php'>";
echo "<table align='center'>";
echo '
<tr>
<th rowspan=2></th> <th>Name</th> <th>Type</th> <th>Length</th>
</tr>';
for ($z = 1; $z <= $_POST['fieldsNum']; $z++) {
echo "
<tr>
<td><input type='text' name='inputs[$z][name]'></td>
<td><input type='text' name='inputs[$z][type]'></td>
<td><input type='text' name='inputs[$z][length]'></td>
</tr>
";
}
echo '</table><br><br>';
echo "<input type='submit' name='submitFieldSetup'>";
echo '</form>';
Then in third.php;
if(isset($_POST['inputs'])) {
$inputs = $_POST['inputs'];
foreach($inputs as $input) {
echo $input['name'];
echo $input['type'];
echo $input['length'];
}
}
I am trying to update multiple rows on submit of a form (in particular this one is the "hours" field.
I have it working but only one of the value updates vs all of them.
There is the possibility of having different values for each update.
The form code:
$query2 = "select * FROM work_hours WHERE formid = $formid ";
$result = $mysqli->query( $query2 );
$num_results = $result->num_rows;
if( $num_results > 0){
echo " <table border='0' align='center'>
<tr>
<td colspan='2' align='center'>
<strong> Time Away Break Down</strong>
</td>
</tr>
<tr>
<td align='center'>Date</td>
<td align='left'>Hours</td>
</tr>";
while( $row = $result->fetch_assoc() ){
extract($row);
echo " <tr>
<td class='hidden_sm' align='center'>
<input type='text' name='id' size='10' value='$id' class='dept' readonly style='width:30px;'>
<input type='text' name='date' size='40' value='$date' class='dept' readonly> <input type='text' name='end_date' size='40' value='$end_date' class='dept' readonly>
</td>
<td class='hidden_sm' align='left' >
<input type='text' name='hours' size='10' style='width:30px;' value='$hours' class='dept' >
</td>
</tr>
";
}
echo "<tr>
<td colspan='2' align='center'>
<input type='submit' name='Submit' value='Submit Request'>
</td>
</tr>
</form>
</table>";//end table
Submit Code:
$id = $_POST['id'];
$formid = $_POST['formid'];
$hours = $_POST['hours'];
include 'connect-db.php';
$stmt = $mysqli->prepare("UPDATE work_hours SET hours = ? WHERE formid = ?");
$stmt->bind_param('si',
$_POST['hours'],
$_POST['formid']);
$stmt->execute();
if ( $stmt ) {
echo "<p align='center'>Thank you, this request has been approved.<BR>You will be redirected in 5 seconds</p>";
} else {
echo "Error, you status cannot be updated. <BR> Please contact your system administrator.";
}
$stmt->close();
?>
Could anyone point me in the right direction to have all values update on submit, as I have had zero luck.
As well I do understand the need to prevent SQL Injections, and that I am working, so no need to remind me.
Thanks in advance!
Looks like you'll want to use a CASE statement as explained here:
How does MySQL CASE work?
Use a loop to build the statement and you're better off using the id as the identifier instead of formid, since the id is the unique value and you could have different results in the form.
i have a upload result form for a certain semester. where i have collected all the subjects and student list for a certain semester.but my problem is i can't insert the result to the database.only a single row is inserted,
controller
$data=array();
for($i=0;$i<count($_POST['Number']);$i++){
if($_POST['Number'][$i] != ''){
$data=array(
'Id'=>$_POST['id'][$i],
'Dept'=>$_POST['Dept'][$i],
'SubjectCode'=>$_POST['sCode'][$i],
'SubjectName'=>$_POST['sName'][$i],
'Semister'=>$_POST['Semister'][$i],
'MidNumber'=>$_POST['Number'][$i]
);
$this->load->model('Upload_Result_model');
$this->Upload_Result_model->upload_midResult($data);
}
}
form:
<table>
<tr>
<th>Student ID</th>
<?php foreach($subjects as $s):?>
<th><?php echo $s->subjectName; echo "<br>"; echo "(".$s->subjectCode.")"; ?></th>
<?php foreach($student as $st):?>
<input type='hidden' id='id[]' name='id[]' value='<?php echo $st->Id;?>'>
<?php endforeach;?>
<input type='hidden' name='Dept[]' id='Dept[]' value=<?php echo $Dept;?>>
<input type='hidden' name='Semister[]' id='Semister[]' value=<?php echo $Semister;?>>
<input type='hidden' id='sCode[]' name='sCode[]' value='<?php echo $s->subjectCode?>'>
<input type='hidden' id='sName[]' name='sName[]' value='<?php echo $s->subjectName?>'>
<?php endforeach;?>
</tr>
<?php foreach($student as $st):?>
<tr>
<td><?php echo $st->Id?></td>
<?php for($i=0;$i<count($subjects);$i++):?>
<td><input type='text' size='7' id='Number[]' name='Number[]'/></td>
<?php endfor;?>
</tr>
it will be very much helpful for me if anyone fix this problem
i dont think the html form elements will come in as arrays in php. try doing print_r($_POST); to see the passed variables and their values. they might come in just comma separates.
i think you might need to do something like $_POST['id']=explode(',',$_POST['id']); and so on to fix your problem
the Numbers variable also has other problems you should look in to
i think
foreach($student as $st):
should be after the . Now you open multiple rows, but not closing them. Also Ids are inside 2 for loops, so they are more than the other elements, I am not sure if this is what you try to do.
So, if this is ok you can try:
$Numbers = #$_POST['Number'];
$ids = #$_POST['id'];
foreach($Numbers as $a => $b)
{
if($Numbers[$a]!='')
{
$data=array(
'Id'=>$ids[$a];, ... }
}
}