receive serialize data in php sent via ajax and loop through it - php

I am sending a form data via ajax call to a php script. I am serializing the data in ajax and on the php script I want to loop through that data to extract the values.
This is my ajax call
$("#submitAttendance").click(function(){
var data = $('form#attendanceForm').serialize();
$.ajax({
url: 'save-attendance.php',
method: 'post',
data: {formData: data},
success: function(data){
console.log(data);
alert(data);
}
});
});
and in the attendance.php I am doing
print_r(($_POST['formData']));//prints the entire serialize data
when I do this
parse_str($_POST['formData'], $searcharray);
print_r(($searcharray));//prints only last user and all radio buttons
I want to extract values so I can save it in db.
This is my form
<form action="" id="attendanceForm">
<?php
if(mysqli_num_rows($result)>0){
while($row = $result->fetch_assoc()){ ?>
<tr>
<input type="hidden" value="<?php echo($row['id']);?>">
<td><input type="text" name="name" value="<?php echo $row['fullname'];?>" readonly></td>
<td><input type="text" name="email" value="<?php echo $row['email'];?>" readonly</td>
<td><input type="text" name="class" value="<?php echo $row['class'];?>" readonly</td>
<td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
<td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
</tr>
<?php }
}
?>
<input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
</form>

You need to rename your items to be able to post arrays (that is call them "whatever" + "[]" and loop over them in PHP), e.g.:
HTML:
<form action="" id="attendanceForm">
<?php
if(mysqli_num_rows($result)>0){
while($row = $result->fetch_assoc()){ ?>
<tr>
<input type="hidden" value="<?php echo($row['id']);?>">
<td><input type="text" name="name[]" value="<?php echo $row['fullname'];?>" readonly></td>
<td><input type="text" name="email[]" value="<?php echo $row['email'];?>" readonly</td>
<td><input type="text" name="class[]" value="<?php echo $row['class'];?>" readonly</td>
<td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
<td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
</tr>
<?php }
}
?>
<input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
</form>
Later in PHP:
foreach ($_POST["formData"]["name"] as $name)
echo "Wow, $name is a really pretty name!";
Additionally, I am not sure what present and absent are meant to do and why they should have the same name (an id). You are already posting the id as an hidden field, why should it be done twice? One overrides the other one (as the names have to be unique).

In addition to #Jan answer I did following to get the complete data and loop through it
parse the incoming data
parse_str($_POST['formData'], $searcharray);
then loop through the array
for ($i = 0 ; $i <= sizeof($searcharray) ; $i++){
$name = $searcharray['name'][$i];
$email= $searcharray['email'][$i];
$class = $searcharray['class'][$i];
$present= ($searcharray['present'][$i]);
}
and my form code is
<form action="" id="attendanceForm">
<?php
if(mysqli_num_rows($result)>0){
$i=0;
while($row = $result->fetch_assoc()){
?>
<tr>
<input type="hidden" value="<?php echo($row['id']);?>">
<td><input type="text" name="name[]" value="<?php echo $row['fullname'];?>" readonly></td>
<td><input type="text" name="email[]" value="<?php echo $row['email'];?>" readonly</td>
<td><input type="text" name="class[]" value="<?php echo $row['class'];?>" readonly</td>
<td><input type="radio" value="present" name="present[<?php echo $i; ?>]" checked></td>
<td><input type="radio" value="absent" name="present[<?php echo $i; ?>]"></td>
</tr>
<?php $i++;
}
}
?>
<input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
</form>

Related

Print PHP page From Multi data From oci_fetch

I have this data in the oracle database photo here
I Get the data when i do while ($row = oci_fetch_array($result))
Here is my code
$query = "SELECT * FROM DEBITCREDIT WHERE DEBITCREDIT.ENTRYDATE LIKE $ENTRYDATE";
$result = oci_parse($conn, $query);
oci_execute($result);
<form method="POST" action="modal/printExpenes.php" id="SearchExpnsese" name="SearchExpnsese">
do {
<td><input type="text" name="ENTRYDATE" id="ENTRYDATE" value="<?php echo $row["ENTRYDATE"] ?>" disabled></td>
<td><input type="text" name="ACCOUNTID" id="ACCOUNTID" value="<?php echo $row["ACCOUNTID"] ?>" disabled></td>
<td><input type="text" name="REMM" id="REMM" value="<?php echo $row["REMM"] ?>" disabled></td>
<td><input type="text" name="COINPRICE" id="COINPRICE" value="<?php echo $row["COINPRICE"] ?>" disabled></td>
<td><input type="text" name="AMOUNT" id="AMOUNT" value="<?php echo $row["AMOUNT"] ?>" disabled></td>
<td><input type="text" name="FILENO" id="FILENO" value="<?php echo $FILENO = $row["FILENO"] ?>" disabled></td>
<td><input type="text" name="VOUCHER" id="VOUCHER" value="<?php echo $VOUCHER = $row["VOUCHER"] ?>" disabled></td>
<input type="submit" id="PrintExp" name="PrintExp">
} while ($row = oci_fetch_array($result));
<?php
if (isset($_POST['PrintExp'])) {
include './modal/printExpenes.php';
}
?> </form>
The output appears as in the following image photo here
The Code is working 100%
BUT
When I press print and I call my Print form
I call the data using $_Post[] as usual
Then I get this message
Warning: Undefined array key "REMM" in C:\xampp\htdocs\login\Finance\modal\printExpenes.php on line
Or Any other variables
I want to know how to print data anywhere in the while loop or any loop. Even if there Multi data From oci_fetch?

Form submission for different buttons

Good Day All,
I have a form with two buttons. One to "Edit" and the other one to "Delete" pilot records:
<form method="post" action="<?php echo adminurl('/pilotmanager/pilotsedit');?>">
<table class="PManager">
<tr><th colspan="2">pilot edit form</th></tr>
<tr>
<td><b>First Name:</b></td>
<td><input type="text" name="firstname" value="<?php echo $firstname;?>" /></td>
</tr>
<tr>
<td><b>Last Name:</b></td>
<td><input type="text" name="lastname" value="<?php echo $lastname;?>" /></td>
</tr>
<tr>
<td><b>Airline:</b></td>
<td>
<select name="code">
<?php
$allairlines = OperationsData::GetAllAirlines();
foreach($allairlines as $airline)
{
echo '<option value="'.$airline->code.'" '.$sel.'>'.$airline->name.'</option>';
}
?>
</select>
</td>
</tr>
<tr>
<td><b>Transfer Hours:</b></td>
<td><input type="text" name="transferhours" value="<?php echo $transferhours;?>" /></td>
</tr>
<tr>
<td><b>Hub:</b></td>
<td>
<select name="hub">
<?php
$allhubs = OperationsData::GetAllHubs();
foreach($allhubs as $hub)
{
echo '<option value="'.$hub->icao.'" '.$sel.'>'.$hub->icao.' - ' . $hub->name .'</option>';
}
?>
</select>
</td>
</tr>
<tr>
<td><b>Total Flights:</b></td>
<td><input type="text" name="totalflights" value="<?php echo $totalflights;?>" /></td>
</tr>
<tr>
<td><b>Total Pay:</b></td>
<td><input type="text" name="totalpay" value="<?php echo $totalpay;?>" /></td>
</tr>
<tr>
<td><b>Pilot active:</b></td>
<td>
<?php
if(intval($pilot->retired) == 1)
{
$retsel='selected';
$activesel = '';
}
else
{
$activesel = 'selected';
$retsel = '';
}
?>
<select name="retired">
<option value="0" <?php echo $activesel?>>Active</option>
<option value="1" <?php echo $retsel?>>Inactive</option>
</select>
</td>
</tr>
<tr>
<td><b>Email Address:</b></td>
<td><input type="text" name="email" value="<?php echo $email;?>" /></td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="pilotid" value="<?php echo $pilotid;?>">
<input type="hidden" name="action" value="saveprofile">
<input type="hidden" name="action" value="deletepilot">
<input type="submit" value="Save Changes" style="width:200px;">
<input type="submit" value="Delete" style="width:200px;">
</td>
</tr>
In my module I have the following function:
public function pilotsedit() {
$pilotid = $_POST['pilotid'];
$action = $_POST['action'];
switch($action) {
case 'saveprofile':
$this->savepro($pilotid);
echo '<script type="text/javascript">alert("Profile Updated!");</script>';
$url = $_SERVER['HTTP_REFERER']; // right back to the referrer page from where you came.
echo '<meta http-equiv="refresh" content="5;URL=' . $url . '">';
break;
case 'deletepilot':
$this->deletePilot($pilotid);
echo '<script type="text/javascript">alert("Pilot Deleted!");</script>';
$url = $_SERVER['HTTP_REFERER']; // right back to the referrer page from where you came.
echo '<meta http-equiv="refresh" content="5;URL=' . $url . '">';
break;
}
}
I'm trying to separate the button submission using "case" statement in my module but when I submit the form using "Edit" button the record gets deleted. Please help me find out what I'm doing wrong. Thanks
The answer is to use button instead of input tag. What I did was the folowing:
<td colspan="2">
<input type="hidden" name="pilotid" value="<?php echo $pilotid;?>">
<button type="submit" name="action" value="saveprofile" style="width:200px;">Save Changes</button>
<button type="submit" name="action" value="deletepilot" style="width:200px;">Delete Pilot</button>
</td>
And in my PHP module:
public function pilotsedit() {
$pilotid = $_POST['pilotid'];
$action = $_POST['action'];
switch($action) {
case 'saveprofile':
$this->savepro($pilotid);
break;
case 'deletepilot':
$this->deletePilot($pilotid);
break;
}
}
And it worked like I was expecting it to do.
You both send these actions on saveprofile and deletepilot
<input type="hidden" name="action" value="saveprofile">
<input type="hidden" name="action" value="deletepilot">
You should separate them.

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'];
}
?>

PHP Codeigniter - passing variables using action in form

I am trying to pass a variable to Controller, but can't. The form I am submitting works fine, it shows everything. But when I try to delete a row, then the problem is that I can't submit it, so that my action would look like /Products/delete/2.
See my code below:
<tbody>
<?php
for($i = 0; $i < count($all); $i++){?>
<tr>
<form action="/Products/delete/<?= $i ?>" method="post">
<td><input name="name" value="<?php echo $all[$i]['name'] ?>" readonly></td>
<td><input name="price" value="<?php echo $all[$i]['price'] ?>" readonly></td>
<td><input name="qty" value="<?php echo $all[$i]['qty'] ?>" readonly></td>
<td><input type="submit" value="Delete"></td>
</form>
</tr>
<?php
}
?>
</tbody>
Thank you!

I want to update data in mysql using the get method

Good morning I am seeking for a help regarding on my php code. I have a problem updating a value in database using get method.
Here is the function I have a form method which is GET and i have a forloop where in inside that loop counts the number of data with the same tracking code now. for example i have a 3 same tracking code so i have a 3 textbox and also i have a 3 submit button. Now i want to update the textbox 1 and click submit it will automatically updated.
For reference here is my code:
enter code here<form method="get" action="view-reservation.php">
<table class="table table-striped">
<tr>
<td>January</td>
<td>February</td>
<td>March</td>
<td>April</td>
<td>May</td>
<td>June</td>
<td>July</td>
<td>August</td>
<td>September</td>
<td>October</td>
<td>November</td>
<td>December</td>
<td></td>
</tr>
<?php foreach ($view as $key => $data):?>
<tr>
<td><input type="hidden" name="track" size="1" value="<?php echo $data->track?>">
<input type="hidden" name="pid" size="1" value="<?php echo $data->pid?>">
<input type="text" name="jan" size="1" value="<?php echo $data->jan?>"></td>
<td><input type="text" name="feb" size="1" value="<?php echo $data->feb?>"></td>
<td><input type="text" name="mar" size="1" value="<?php echo $data->mar?>"></td>
<td><input type="text" name="apr" size="1" value="<?php echo $data->apr?>"></td>
<td><input type="text" name="may" size="1" value="<?php echo $data->may?>"></td>
<td><input type="text" name="jun" size="1" value="<?php echo $data->jun?>"></td>
<td><input type="text" name="jul" size="1" value="<?php echo $data->jul?>"></td>
<td><input type="text" name="aug" size="1" value="<?php echo $data->aug?>"></td>
<td><input type="text" name="sep" size="1" value="<?php echo $data->sept?>"></td>
<td><input type="text" name="oct" size="1" value="<?php echo $data->oct?>"></td>
<td><input type="text" name="nov" size="1" value="<?php echo $data->nov?>"></td>
<td><input type="text" name="dec" size="1" value="<?php echo $data->dec?>"></td>
<td>
<a href="view-reservation.php?track=<?php echo $data->track?>&&pid=<?php echo $data->pid?>&&jan=<?php echo $data->jan?>"
class="label label-danger">Update Payment</a></td>
</tr>
<?php endforeach;?>
<?php endif?>
</form>
</table>
Here is my query when i execute:
if(isset($_GET['track']) && isset($_GET['pid']) && isset($_GET['jan']))
{
//
$jan = filter($_GET['jan']);
$track = filter($_GET['track']);
$pid = filter($_GET['pid']);
$query = $con->query("update payment set jan = '500' where track = '$track' and pid='$pid'") or die(mysqli_errno());
if($query)
{
echo'updated';
}
}

Categories