Php dynamic forms while loop - php

This is my dynamic form
<input type="checkbox" class="thebox" value="selected<?php echo"$x"; ?>"/>
</td>
<td>
<input type="text" class="thebox" name="id<?php echo"$x"; ?>" value="<?php echo $row['member_id']; ?>" />
</td>
<td>
<input type="text" class="thebox" name="fn<?php echo"$x"; ?>" value=" <?php echo $row['first_name']; ?>"/>
</td>
<td>
<input type="text" class="thebox" name="ln<?php echo"$x"; ?>" value="<?php echo $row['last_name']; ?>"/>
</td>
<td>
<input type="text" class="thebox" name="sd<?php echo"$x"; ?>" value="<?php echo $row['start_date']; ?>"/>
</td>
</tr>
<?php
$x++;
endwhile;
?>
<tr align="center">
<td>
</td>
<td>
<input type="submit" name="delmem" class="myButton" value="Delete member "/>
</td>
</tr>
</table>
</form>
</div>
I am trying to get the selected check box variables only. I have tried everything. I know I am a php noob someone help me please. How can I retrieve the selected check box attributes?
<?php
if(isset($_POST['delmem']))
{
$i = 1;
while($i <= $_SESSION['stuff'])
{
$a = $_POST["fn"."$i"]."<br />";
echo $a;
$i++;
if(isset($_POST["selected"."$i"]))
{
echo $_POST["$fn"."$i"]."<br />";
echo $_POST["$ln"."$i"];
}
}
}
}

I think you made mistake in post Variable
$_POST["$fn"."$i"] to $_POST["fn"."$i"]
if(isset($_POST["selected"."$i"]))
{
echo $_POST["$fn"."$i"]."<br />";
echo $_POST["$ln"."$i"];
}
To
if(isset($_POST["selected"."$i"]))
{
echo $_POST["fn"."$i"]."<br />";
echo $_POST["ln"."$i"];
}

Related

Why submitting this form it submits wrong ids?

I am submitting this from and in the same page getting the values of the $_POST['id] and displaying them but its getting the wrong id not the ones I am selecting from the row in the table:
<tbody>
<?php if (!empty($found_devices)) {
foreach ($found_devices as $devs) : ?>
<tr>
<td>
<input type="checkbox" id="myCheck" onclick="display_hide_selectClient(this)" name="devices[<?php echo $devs["id"] ?>]" value="<?php echo $devs["id"] ?>">
</td>
<td>
<?php echo $devs["serial_imei"] ?>
</td>
<td>
<?php echo $devs["serial_no"] ?>
</td>
<td>
<?php echo $devs["created_date"] ?>
</td>
<td>
<input hidden name="edit_device_imei" value="<?php echo $devs["serial_imei"] ?>">
<input hidden name="edit_device_serial" value="<?php echo $devs["serial_no"] ?>">
<input hidden name="edit_device_id" value="<?php echo $devs["id"] ?>">
<button type="submit" name="edit_device">Edit</button>
</td>
</tr>
<?php endforeach;
} ?>
</tbody>
So, when submitting this form with the button edit_device I am receiving the wrong values in PHP.
PHP Ccode:
if (isset($_POST['edit_device']) && !empty($_POST['edit_device_id'])) {
$get_selected_Devices = e($_POST['edit_device_id']);
$get_selected_Devices_s = e($_POST['edit_device_serial']);
$get_selected_Devices_i = e($_POST['edit_device_imei']);
}

How to check if array result is clicked in php

How do I check if an array result is clicked.
Below is my code to iterate through the database.
The problem now is that is not checking to confirm if the result of the array is clicked.
<tr>
<td><input value=<?php echo $row_Recordset1['subject_id']; ?> type="submit" id=<?php echo $row_Recordset1['subject_id']; ?>
class="form-control btn btn-success" id="subject_id">
<input name="subject_id[]" type="hidden" value="<?php echo $row_Recordset1['subject_id']; ?>" /> <?php if(isset($row_Recordset1['subject_id'])) {
echo "u are welcome";
//code...
}
?></td>
<td><div><?php echo $row_Recordset1['name']; ?></div></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

Table and form submit

I have to submit all values in different cell in database with one submit
When I click submit it only inserts last student and last subject marks
example (flinch and physics):
<tbody>
<?php
$query="SELECT *FROM `students` WHERE `standard`='".$standard."'";
$ex_query=mysql_query($query);
while($row=mysql_fetch_array($ex_query)) {
$id=$row['id'];
$fname=$row['fname'];
$lname=$row['lname'];
$rollno = $row['rollno'];
$standard = $row['standard'];
$section = $row['section'];
?>
<tr>
<th scope="row">1</th>
<td><?php echo $fname; ?> <?php echo $lname; ?>
<input type="hidden" name="name" value="<?php echo $fname; ?> <?php echo $lname; ?>">
</td>
<td><?php echo $standard; ?>
<input type="hidden" name="standard" value="<?php echo $standard; ?>">
</td>
<td><?php echo $section; ?>
<input type="hidden" name="section" value="<?php echo $section; ?>">
</td>
<?php $querysub="SELECT *FROM `subject`";
$ex_querysub=mysql_query($querysub);
while($row=mysql_fetch_array($ex_querysub))
{
$id=$row['id'];
$subject=$row['subject'];
?>
<td><input type="hidden" name="subject" value="<?php echo $subject; ?>"><input type="text" class="form-control col-md-7 col-xs-12" name="marks"></td>
<?php } ?>
<!-- <td><i class="fa fa-edit"></i></td>
<td><i class="fa fa-trash-o"></i></td> -->
</tr>
<?php }
?>
</tbody>
</table>
<input type="submit" name="submit">
<?php
if(isset($_POST['submit'])) {
$id
$name = $_POST['name'];
$standard = $_POST['standard'];
$section = $_POST['section'];
$subject = $_POST['subject'];
$marks = $_POST['marks'];
$query="INSERT INTO `marks` (`id` ,`name` ,`standard` ,`section` ,`subject` ,`marks`) VALUES (NULL, '$name', '$standard', '$section', '$subject', '$marks')";
$ex_query=mysql_query($query); ?>
<!-- <script>
window.location.href="view-standard.php";
</script> --> <?php
}
}
?>
</form>
You can add a row index to each elements name like this
<tr>
<th scope="row">1
</th>
<td>
<?php echo $fname; ?>
<?php echo $lname; ?>
<input type="hidden" name="name[]" value="<?php echo $fname; ?> <?php echo $lname; ?>">
</td>
<td>
<?php echo $standard; ?>
<input type="hidden" name="standard[]" value="<?php echo $standard; ?>">
</td>
<td>
<?php echo $section; ?>
<input type="hidden" name="section[]" value="<?php echo $section; ?>">
</td>
<?php $querysub="SELECT *FROM `subject`";
$ex_querysub=mysql_query($querysub);
while($row=mysql_fetch_array($ex_querysub))
{
$id=$row['id'];
$subject=$row['subject'];
?>
<td>
<input type="hidden" name="subject[]" value="<?php echo $subject; ?>">
<input type="text" class="form-control col-md-7 col-xs-12" name="marks">
</td>
<?php } ?>
<!-- <td><i class="fa fa-edit"></i></td>
<td><i class="fa fa-trash-o"></i></td> -->
</tr>
Then when you access $_POST['name'] for example you will get an array with all values and you can use a loop and insert them in database.
$i=0;
while ($row = mysql_fetch_assoc($ex_querysub)) {
?>
<td>
<input type="text" name="form[<?php echo $i; ?>]['name']" value="<?php echo $row['name']; ?>" />
</td>
<td>
<input type="text" name="form[<?php echo $i; ?>]['section']" value="<?php echo $row['section']; ?>" />
</td>
.... you got the idea
<?php
$i++;
}
Always use mysql_fetch_assoc instead of mysql_fetch_array.
And the action itself
<?php
if (isset($_POST['form'])) {
foreach ($_POST['form'] as $row) {
$sql = sprintf("INSERT INTO marks SET
name = '%s',
section = '%s'
... add all fields here
",
strip_tags($row['name']), // you should always sanitize your input from user. Or you'll have SQL Injection problems
strip_tags($row['section']),
);
mysql_query($sql);
}
}
I alwasy prefer to use the second form of INSERT statement http://dev.mysql.com/doc/refman/5.7/en/insert.html . It's similar to UPDATE and it's easier to understand. specially if you have big insert queries. Usually primary key which is auto_increment should be skipped on inserts

Passing variables get undefined after 'Refreshing the page' in php

I'm trying to passing values from one page to another page...In a page seller-profile.php, when i refresh the page all passing data from seller-ride.php gets undefined, so i have to get back to the seller-ride then click on button then all data get display, How can i avoid this kind of situation? please suggest me better solution!
seller-ride.php
<form method="post" action="seller-profile.php">
<td><input type="text" value="<?php echo $row['company_name']; ?>" name="c_name" readonly> </td>
<td><input type="text" value="<?php echo $row['mobile']; ?>" name="mob" readonly></td>
<td><input type="text" value="<?php echo $row['count_free']; ?>" name="cf" readonly></td>
<td> <input type="text" value="<?php echo $row['count_travel']; ?>" name="ct" readonly></td>
<td><input type="text" value="<?php echo $row['price']; ?>" name="p" readonly>
<td><input type="submit" class="btn btn-flat btn-primary btn-sm" value="View-Profile" name="submit"></button></td>
</form>
seller-profile.php
<?php
include 'seller-profile-config.php';
?>
<?php echo $company_name; ?>
<?php echo $mobile; ?>
<?php echo $count_free; ?>
<?php echo $count_travel; ?>
<?php echo $price; ?>
seller-profile-config.php
<?php
if(isset($_POST['submit'])){
$company_name=$_POST['c_name'];
$mobile=$_POST['mob'];
$total_income=$_POST['p'];
$count_free=$_POST['cf'];
$count_travel=$_POST['ct'];
}
?>

Form in while validation doesn't work

I am trying to validate a "modify" form. The data is received from database so of course i have to put it in a while.
<?php
while ($row = mysql_fetch_array($categories))
{
?>
<form name="adminEditCategory" action="../db/adminModifyCategory.php" method="POST" enctype="multipart/form-data" >
<tr>
<td>
<input type="text" name="id" value="<?php print $row['id']; ?>" readonly="readonly" style="width: 20px;" />
</td>
<td> <input type="text" name="name<?php print $row['id']; ?>" value="<?php print $row['name']; ?>" /><span></span>
</td>
<td> <img src="../utils/getLogoCategory.php?id=<?php print $row['id']; ?>" style="max-width:40px; max-height:40px; float: left; margin-bottom: 10px;" align="left" /> </td>
<td>
<input type="file" name="attachment<?php print $row['id']; ?>" value="" style="float: right; "><span></span>
</td>
<td> <?php print date('d-m-Y', strtotime($row['added_date'])); ?>
</td>
<td>
<select width="294" name="is_deleted<?php print $row['id']; ?>" style="width: 50px;">
<?php
foreach($is_deleted as $id=>$name)
{
?>
<option value="<?php print $id; ?>" <?php if($id == $row['is_deleted']) print "selected= 'selected'"; ?> ><?php print $name; ?></option>
<?php
}
?>
</select><span></span>
</td>
<td> <input type="submit" value="Modifica" name="Edit" class="button" onClick="return validateEditCategory(<?php print $row['id']; ?>)" />
<input type="submit" value="Sterge" class="button" name="Delete" onClick="return validateDeleteCategory()">
</td>
</tr></form>
<?php
}
?>
This is the validation code:
function validateEditCategory(Id) {
var valid;
valid = true;
// alert (document.forms["adminEditCategory"]["name"+Id].name);
valid *= checkNume(document.forms["adminEditCategory"]["name"+Id]);
valid *= checkIsFileGol(document.forms["adminEditCategory"]["attachment"+Id]);
return valid > 0;
}
The validation works, but only for the first row. I can't understand why it doesn't validate the rest of the rows.
Try changing this <form name="adminEditCategory" action=
to this <form name="adminEditCategory<?php print $row['id']; ?>" action=
and
this document.forms["adminEditCategory"]
to this document.forms["adminEditCategory" + Id].
In replace, you can reduce all the ID appending to names of inputs. The point is, give all the forms different names, but inside each form you can call all the fields the same, because you already pass the ID in the <input name='id'>. But that is not necessary, just shorter and more logical.

Categories