Sending data to another page without a form - php

I was informed that I can send data without a POST function. What I am trying to figure out is how I can get rid of the form around my table and send the data to my page called "edit-product".
In addition to the data, I want the user to be sent there as well, just like the form's action attribute would do.
As of now, I have it structured like this:
<?php
$stmt = $dbc->query("SELECT `id`,`first`,`last`,`product` FROM users");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while($row = $stmt->fetch()) {
?>
<form method="POST" action="edit-product">
<tr>
<td><?php echo $row['id'];?>"</td>
<td><?php echo $row['first'];?></td>
<td><?php echo $row['last'];?></td>
<td><?php echo $row['product'];?></td>
<input name="id" type="hidden" value="<?php echo $row['id'];?>" readonly>
<input name="first" type="hidden" value="<?php echo $row['first'];?>">
<input name="last" type="hidden" value="<?php echo $row['last'];?>">
<input name="product" type="hidden" value="<?php echo $row['product'];?>">
<td><input name="save" type="submit" value="Save"></td>
<td><div class="delete-class" name="delete" id="<?php echo $row['id']; ?>">Delete</div></td>
<td><input name="edit" type="submit" value="Edit"></td>
</tr>
</form>
<?php } ?>
</tbody>
</table>
In my edit-product page, I receive the data like this:
$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];
How could I do this?

You can use an edit button with anchor.
<table>
<tbody>
<?php
$stmt = $dbc->query("SELECT `id`,`first`,`last`,`product` FROM users");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while($row = $stmt->fetch()) {
?>
<tr>
<td><?php echo $row['id'];?>"</td>
<td><?php echo $row['first'];?></td>
<td><?php echo $row['last'];?></td>
<td><?php echo $row['product'];?></td>
<td><input name="save" type="submit" value="Save"></td>
<td><div class="delete-class" name="delete" id="<?php echo $row['id']; ?>">Delete</div></td>
<td>Edit</td>
</tr>
<?php } ?>
</tbody>
</table>
And u can catch the url value from edit-prpduct page.
like
$id = $_GET['id'];
$first = $_GET['first'];
$last = $_GET['last'];
$product = $_GET['product'];
Just try this without any form post.

Related

Post form not submitting inside while loop table php

I am trying to submit this form but it only submits the hidden input field. I can't understand why. It was working before.
while ($row = mysqli_fetch_array($gettimesheets)) {
$date = $row['Date'];
$date = date('d-m-Y', strtotime($date));
$teachername = $row['StaffName'];
$teacherid = $row['StaffID'];
$starttime = $row['StartTime'];
$endtime = $row['EndTime'];
$totalhours = $row['TotalHours'];
$timesheetid = $row['id'];
?>
<tr>
<form action="edittimesheet.php" method="post">
<td class="text-center"><?php echo $date; ?></td>
<td><?php echo $teachername; ?></td>
<td><?php echo $teacherid; ?></td>
<td><input type="time" name="starttime" class="form-control" value="<?php echo $starttime; ?>"></td>
<td><input type="time" name="endtime" class="form-control" value="<?php echo $endtime; ?>">
<input type="text" name="timesheetid" value="<?php echo $timesheetid; ?>" hidden></td>
<td class="text-right">
<input type="submit" class="btn btn-success" value="Edit"/>
<a href="deletetimesheet.php?id=<?php echo $timesheetid; ?>"
class="btn btn-simple btn-danger">Delete</a>
</td>
</form>
</tr>
<?php
}
It's odd but putting parts of a table inside a form is not a good idea. Actually browsers in such situation are confused! You can put your whole form inside a cell (td).
For more information Check this
Make your form outside your table and on your edit-submit button the id you edit.
<form action="edittimesheet.php"
<table>
<?php
while ($row = mysqli_fetch_array($gettimesheets)) {
$date = $row['Date'];
$date = date('d-m-Y', strtotime($date));
$teachername = $row['StaffName'];
$teacherid = $row['StaffID'];
$starttime = $row['StartTime'];
$endtime = $row['EndTime'];
$totalhours = $row['TotalHours'];
$timesheetid = $row['id'];
?>
<tr>
method="post">
<td class="text-center"><?php echo $date ;?></td>
<td><?php echo $teachername ;?></td>
<td><?php echo $teacherid ;?></td>
<td><input type="time" name="starttime[$timesheetid]" class="form-control" value="<?php echo $starttime; ?>" ></td>
<td><input type="time" name="endtime[$timesheetid]" class="form-control" value="<?php echo $endtime; ?>" >
<input type="text" name="timesheetid[$timesheetid]" value="<?php echo $timesheetid; ?>" hidden></td>
<td class="text-right">
<input type="submit" name="edit" value ="<?php echo $timesheetid; ?>;" class="btn btn-success" value="Edit"/>
Delete
</td>
</tr>
<?php
}
?>
</table>
</form>

Form Action PHP Sql

I have coding like this, it should when clicked Dekripsi button, it will appear download.php file, but this coding does not work
<?php
$nik = $_SESSION['nik'];
$no = 1;
$sql = "SELECT enkrip.Id,
(SELECT user.nama
FROM user
WHERE user.nik=enkrip.filefrom) as nama,
enkrip.filefrom,
enkrip.filename,
enkrip.kunci,
enkrip.dateupload,
enkrip.folder
FROM enkrip
WHERE enkrip.fileto = '$nik'
ORDER BY enkrip.Id ASC";
$data = mysql_query($sql);
while ($master = mysql_fetch_array($data)) {
?>
<form action="download.php?Id=<?php echo $master['Id']; ?>&file=<?php echo $master['folder']; ?>" method="post" target="_blank" >
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $master['nama'] ?></td>
<td><?php echo $master['filename'] ?></td>
<td><?php echo $master['kunci'] ?></td>
<td><?php echo $master['dateupload'] ?></td>
<td><input type="text" class="form-control" name="kunci" placeholder="Masukan Kunci Dekrip"></td>
<td><button type="submit" class="btn btn-success">Dekripsi</button></td>
<tr>
</form>
<?php } ?>
You are using method post for your form so:
<form action="download.php" method="post" target="_blank" >
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $master['nama'] ?></td>
<td><?php echo $master['filename'] ?></td>
<td><?php echo $master['kunci'] ?></td>
<td><?php echo $master['dateupload'] ?></td>
<td><input type="text" class="form-control" name="kunci" placeholder="Masukan Kunci Dekrip"></td>
<td><button type="submit" class="btn btn-success">Dekripsi</button></td>
<tr>
<input type="hidden" name="Id" value="<?php echo $master['Id']; ?>">
<input type="hidden" name="file" value="<?php echo $master['folder']; ?>">
</form>
All the parameters must be passed in post mode (using hidden input fields)

How to add multiple rows of data from one sql table to another with selection based on checkboxes?

<form method="post">
<?php
$sql_u="SELECT * from cubaan";
$query_u = mysqli_query($conn,$sql_u);
while($row=mysqli_fetch_assoc($query_u)){
?>
<table border="2">
<tr>
<td><input type="checkbox" name="bio[]" value="<?php $row['name'];?>" data-valuetwo="<?php $row['age'];?>" data-valuethree="<?php $row['job'];?>"></td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['age'];?></td>
<td><?php echo $row['job'];?></td>
</tr>
</table>
<?php
}
?>
<input type="submit" name="post">
</form>
this is my code where I want to select data from the database and the selected data will insert to the new database
Use input type hidden as following
<form method="post">
<?php
$sql_u="SELECT * from cubaan";
$query_u = mysqli_query($conn,$sql_u);
while($row=mysqli_fetch_assoc($query_u)){
?>
<table border="2">
<tr>
<td>
<input type="checkbox" name="bio[<?php echo $row['id'];?>]" value="<?php echo $row['name'];?>">
<input type="hidden" name="age[<?php echo $row['id'];?>]" value="<?php echo $row['age'];?>">
<input type="hidden" name="job[<?php echo $row['id'];?>]" value="<?php echo $row['job'];?>">
</td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['age'];?></td>
<td><?php echo $row['job'];?></td>
</tr>
</table>
<?php
}
?>
<input type="submit" name="post">
</form>
And while your form submit action use
<?php foreach($_POST['bio'] as $id=>$name) {
echo $name;
echo $_POST['age'][$id];
echo $_POST['job'][$id];
}
?>

Passing for each values to php script

I am passing a table data value within a for each loop to the php script. The problem here is that when I try to echo $exchange;. I don't get any values of the specific product name. The product name being displayed in the form is item1.
<form action="exchangeItem" method="post">
<tbody>
<?php
$query = "SELECT * FROM product";
$data = $MySQLi_CON->query($query);
foreach ($data as $key ) {
?>
<tr>
<td class="id"><h3><input type="hidden" name="productName" ><?php echo $key['product_Name'];?></h3>
</td>
<input name = "id" type = "hidden" value="<?php echo $id; ?>">
<td id="exchange_button">
<input value="Exchange" name="exchange_submit" type="submit" class="btn btn-primary btn-md raised exchange_submit">
</td>
</tr>
<?php
}
?>
</tbody>
</form>
exchangeItem.php
<?php
$exchange = $_POST['productName'];
echo $exchange;
exit;
?>
This
<td class="id"><h3><input type="hidden" name="productName" ><?php echo $key['product_Name'];?></h3>
Should be:
<td class="id"><h3><input type="hidden" name="productName" value="<?php echo $key['product_Name'];?>" ></h3>
first, edit your td
<td class="id"><h3><input type="hidden" name="productName[]" value="<?php echo $key['product_Name'];?>" ></h3>
your name => productName should be in array thats why you need bracket [ ], because you use foreach. then your post will be in array too.
exchangeItem.php
<?php
print_r($_POST['productName']);
exit;
?>
I have added & changed your 2 input name productName[] , id[] and your form action exchangeItem.php if you get product name must be used input name with array like [] because you are used foreach loop.
<form action="exchangeItem.php" method="post">
<tbody>
<?php
$query = "SELECT * FROM product";
$data = $MySQLi_CON->query($query);
foreach ($data as $key ) {
?>
<tr>
<td class="id">
<h3><input type="hidden" value="<?php echo $key['product_Name'];?>" name="productName[]" ><?php echo $key['product_Name'];?></h3>
<input name="id[]" type="hidden" value="<?php echo $id; ?>">
</td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
<tr>
<td id="exchange_button">
<input value="Exchange" name="exchange_submit" type="submit" class="btn btn-primary btn-md raised exchange_submit">
</td>
</tr>
</tfoot>
</form>
exchangeItem.php
<?php
if(isset($_POST['exchange_submit'])) {
$exchange = $_POST['productName'];
foreach($exchange as $product_name) {
echo $product_name;
}
}
?>
The problem you facing here is with this code
<td class="id">
<h3><input type="hidden" name="productName" ><?php echo $key['product_Name'];?></h3>
</td>
here in the input field you didn't have added the value, that's why when you trying to print the posted value in your action file it's showing none. So just add add value to the input field
<td class="id">
<h3><input type="hidden" name="productName" value="<?php echo $key['product_Name'];?>"></h3>
</td>
Set value for you hidden element... No need to place the hidden element in the header tag. Use productName[] for posting array of values.
<td class="id">
<h3><?php echo $key['product_Name'];?></h3>
<input type="hidden" name="productName[]" value="<?php echo $key['product_Name'];?>">
</td>
foreach all posted values to print.
<?php
$exchange = $_POST['productName'];
foreach($exchange as $key) {
echo $key;
}
exit;
?>
Update for select
Change yout html for select values..
<td class="id">
<h3><input type="checkbox" name="productName[]" value="<?php echo $key['product_Name'];?>"><?php echo $key['product_Name'];?></h3>
</td>

pass relevant hidden field values from the form to the controller

Im having a page that shows monthly subscriptions of a user which is created using codeigniter. what i want to do is when a the user clicks on make payment pass the values in the hidden files to the controller.
<?php echo form_open('options/done');?>
<table class="tables">
<thead>
<tr>
<th>Ref Code</th>
<th>Month</th>
<th>Year</th>
<th>action/th>
</tr>
</thead>
<tbody>
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];?>
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<tr>
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php echo form_close();?>
so when someone hits the submit button how can i pass only the hidden values which are relevant to that table row?
add form just inside the <tr> elements inside your loop (see below with your code)
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];
?>
<tr>
<form action="target.php" method="post" name="formName_<?php echo $s;?>" >
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</form>
</tr>
<?php endforeach; ?>

Categories