$_POST variable is always the same - php

I'm trying to get a variable from a link (invisible button, basically), however, when I get to the redirected page, it's always the same $_POST variable it shows. I've checked if it's set before or after the for loop, and it's not. I've also printed out the values in the for loop, and they're different, as they should be. I've also tried unsetting the variable at the bottom of the redirect page after use. I'm totally out of ideas now... what to do?
<form method="post" action="http://localhost:8888/wordpress/job-openings/view-job/">
<table>
<tr>
<th>Job ID</th>
<th>Position</th>
<th>Client</th>
<th>Recruiter</th>
</tr>
<? for($i = 0; $i < count($rowArray); $i++) { ?>
<? $positionStatus = $data->response->result->JobOpenings->row[$i]->FL[8]->content;
$recruitmentResponsible = $data->response->result->JobOpenings->row[$i]->FL[7]->content;
$recruitmentResponsibleTweak = current(explode("(", $recruitmentResponsible));
$job_opening_id = $data->response->result->JobOpenings->row[$i]->FL[0]->content;
$request_url = 'http://recruit.zoho.com/ats/private/json/JobOpenings/getRecordById?authtoken=xxx&scope=recruitapi&id=' . $job_opening_id;
if ($positionStatus == 'In-progress') {
?>
<tr>
<td>
<input type="hidden" name="job_id" value="<?php echo $request_url ?>" />
<button id="jobopening-link">
<? echo $data->response->result->JobOpenings->row[$i]->FL[4]->content; ?>
</button>
</td>
<td><? echo $data->response->result->JobOpenings->row[$i]->FL[5]->content; ?></td>
<td><? echo $data->response->result->JobOpenings->row[$i]->FL[6]->content; ?></td>
<td><? echo $recruitmentResponsibleTweak ?></td>
</tr>
<? }
} ?>
</table>
</form>

First, there is no reference to $_POST in your code. Second, the reason why you get always the same value is because you are always setting the same exactly name to the hidden input, and when the form is sent you will get only the first input with that name. My suggestion, is to move the form tag inside the for loop, that way you will create multiple forms and each one will have the submit button and the proper value in the hidden input:
<table>
<tr>
<th>Job ID</th>
<th>Position</th>
<th>Client</th>
<th>Recruiter</th>
</tr>
<? for($i = 0; $i < count($rowArray); $i++) { ?>
<form method="post" action="http://localhost:8888/wordpress/job-openings/view-job/">
<? $positionStatus = $data->response->result->JobOpenings->row[$i]->FL[8]->content;
$recruitmentResponsible = $data->response->result->JobOpenings->row[$i]->FL[7]->content;
$recruitmentResponsibleTweak = current(explode("(", $recruitmentResponsible));
$job_opening_id = $data->response->result->JobOpenings->row[$i]->FL[0]->content;
$request_url = 'http://recruit.zoho.com/ats/private/json/JobOpenings/getRecordById?authtoken=xxx&scope=recruitapi&id=' . $job_opening_id;
if ($positionStatus == 'In-progress') {
?>
<tr>
<td>
<input type="hidden" name="job_id" value="<?php echo $request_url ?>" />
<button id="jobopening-link">
<? echo $data->response->result->JobOpenings->row[$i]->FL[4]->content; ?>
</button>
</td>
<td><? echo $data->response->result->JobOpenings->row[$i]->FL[5]->content; ?></td>
<td><? echo $data->response->result->JobOpenings->row[$i]->FL[6]->content; ?></td>
<td><? echo $recruitmentResponsibleTweak ?></td>
</tr>
<? } ?>
</form>
<? } ?>
</table>

and the input type="submit"???
to capture the $_POST content:
<?php
if (isset($_POST['job_id'])) {
echo 'Do something for this job Id.';
}
?>

Related

Error when empty MySQL field even with conditions

When I click on a project in my web app, that it contains values, I do not get any error, but I created another project with empty fields in MySQL, and I clicked on it, in my web app (PHP app), so I got this error:
Undefined variable: total in
C:\wamp\www\architect\projDetails.php on line 80
I have this PHP code:
$id = $_REQUEST['id'];
$sql = "SELECT (SELECT SUM(total_pay) FROM workers) total,workers. * FROM workers WHERE projects_id = ".$id." ORDER BY date_of_pay DESC";
$stmt = mysqli_query($con, $sql) or die($sql."<br/><br/>".mysqli_error($con));
And here html and php code near line 80:
<tr>
<?php while($rows = mysqli_fetch_array($stmt)){ $total = 0; ?>
<tr>
<?php if($rows['total']!=0){
$total = $rows['total'];
}
else {
$total = "غير متوفر";
}
?>
<td align="center"><?php echo $rows['total_pay']?></td>
<td align="center"><?php echo $rows['date_of_pay']?></td>
<td align="center"><?php echo $name['project_name'] ?></td>
<td align="center"><!--<input class="imgClass_insert" type="submit" name="submit1" value="" />-->
<input class="imgClass_dell" type="submit" onClick="return confirm('Are you sure you want to delete?')" name="delete_workers" value=""/>
</td>
</tr>
</tr>
<?php } ?>
<tr>
<td colspan="3">مجموع تكاليف العمال في مشروع <?php echo $name['project_name']?></td>
<td align="center"><?php echo $total ?></td>
</tr>
So when total is empty, I get the error,and when it is not empty, I don't get any errors, so what is the problem here?
Your variable $total is currently only alive within the while block. You are exiting this block at the statement </tr><?php } ?><tr>. That means your variable is no longer allocated in code row <td align="center"><?php echo $total ?></td> below. To use this total variable below the while block, add the declaration and initialization of the variable above the while block, e.g:
<tr>
<?php $total = 0;
while($rows = mysqli_fetch_array($stmt)){ ?>
<tr>
<?php if($rows['total']!=0){ <-- Check this comparison (described below)
$total = $rows['total'];
}
else {
$total = "غير متوفر";
}
?>
<td align="center"><?php echo $rows['total_pay']?></td>
<td align="center"><?php echo $rows['date_of_pay']?></td>
<td align="center"><?php echo $name['project_name'] ?></td>
<td align="center"><!--<input class="imgClass_insert" type="submit" name="submit1" value="" />-->
<input class="imgClass_dell" type="submit" onClick="return confirm('Are you sure you want to delete?')" name="delete_workers" value=""/>
</td>
</tr>
</tr>
<?php } ?>
<tr>
<td colspan="3">مجموع تكاليف العمال في مشروع <?php echo $name['project_name']?></td>
<td align="center"><?php echo $total ?></td>
</tr>
Hope this will work for you. The annotated line in code above will compare a string with an integer value. Please be aware that $rows['total'] will contain a value of type String. Your comparison is against an integer value of 0. PHP should not throw an error or warning but it could come to unwanted results in this if statement. (Further information: http://php.net/manual/de/language.types.type-juggling.php )
Try something like below.
echo isset($total)? $total: 0;
Here if we are getting any data from DB, then only it will enter into the while loop. Now inside while loop only $total is defining.
If we define it outside the while loop, the default value, will solve the issue
<tr> <?php
$total = "0";
while($rows = mysqli_fetch_array($stmt)){ $total = 0;
if($rows['total']!=0){
$total = $rows['total'];
}
//.....
//.....
} ?>
</tr>
<tr>
<td colspan="3">some text <?php echo $name['project_name']?></td>
<td align="center"><?php echo $total ?></td>
</tr>
Thank you

delete using checkbox code not working properly [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Code not working properly always shows the message fail.
function delete(){
$con = mysqli_connect("localhost","root","","rishita_db");
$sql="select * from 14_patientdetails";
$result=mysqli_query($con,$sql);
?>
<form method="post" action="">
<center>
<h1><u>Patient Details</u></h1>
<table border="1" style="font-family:Georgia;color:#800000;font-style:bold;">
<tr style="font-family:Georgia;color:green;font-style:bold;">
<th>#</th>
<th>Patient ID</th>
<th>Patient Name</th>
<th>DOB</th>
<th>Gender</th>
<th>Address</th>
<th>Phone No.</th>
<th>Medicare</th>
<th>Doctor Associated</th>
</tr>
<form method="post" action="">
<?php
while($row=mysqli_fetch_array($result))
{
$r=$row['patientId'];
?>
<tr>
<td><input type='checkbox' name='checkbox[]' id="checkbox" value=<?php echo $r; ?>></td>
<td><?php echo $row['patientId']; ?></td>
<td><?php echo $row['patientName']; ?></td>
<td><?php echo $row['DOB']; ?></td>
<td><?php echo $row['Gender']; ?></td>
<td><?php echo $row['Address']; ?></td>
<td><?php echo $row['Phone']; ?></td>
<td><?php echo $row['Medicare']; ?></td>
<td><?php echo $row['Doctor']; ?></td>
</tr>
<?php
}
?>
</table>
<table>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="del" type="submit" id="del" value="Delete"></td>
</tr>
</table>
</form>
<?php
if(isset($_POST["del"]))
{
if(isset($_POST["checkbox"]))
echo 'Enter';
$chk = isset($_POST['checkbox']);
$chkcount = count($chk);
for($i=0;$i<$chkcount;$i++){
$del=$chk[$i];
$sql1 = "DELETE FROM 14_patientdetails WHERE id='$del'";
$q = mysqli_query($con,$sql1);
}
if($q){
echo "Success";
}else{
echo 'Fail';
}
}
}
This is wrong:
$chk = isset($_POST['checkbox']);
$chkcount = count($chk);
for($i=0;$i<$chkcount;$i++){
$del=$chk[$i];
Now there are one too many mistakes, multiple <form> tags and a single closing </form>
I couldn't not edit your code on my phone so I will suggest the way to get along. I'm pretty sure you'll love it when you do it yourself.
Make a <td><input type = "checkbox" name = "checkbox" value = "<?php echo $r; ?>"/>Proceed</td> inside a while-loop something similar to your code while($row = mysqli_fetch_array($result))
In the end of the <form> make a <button> and redirect user to another page probably delete.php. Now, check its set;
if(isset($_POST['checkbox']
{
//foreach loop for your query
foreach($_POST['checkbox'] as $val)
{
// check what you're getting..
echo $val;
}}
You could also use a simple for-loop
If you want:
for($i = 0; $i <count($_POST['checkbox']; $i++)
{
// do your stuff..
}
Bottom line: try to differentiate your forms, queries and make a single loop to read from database and assign the values at the same time to your checkboxes.
This is what I have understood and tried to write from my phone,
please study mysqli/PDO to prevent SQL Injection/XSS.

PHP table data filtering

I want to filter data in the table use dropdown menu (e.g. filter by name so i can see all data with name 'Jane'). I don't want to move to another page (use ajax or anything else if can). Any idea what must i do ?
This is the dropdown menu and table code :
<!-- Dropdown menu -->
<div class="col-md-2">
<select class="form-control selectpicker">
<option value="">Name</option>
<?php
// print all name value from $administratorProvider
foreach($administratorProvider as $administrator){
?>
<option value="<?php $administrator->first_name ?>"><?php echo $administrator->first_name; ?></option>
<?php
}
?>
</select>
</div>
<table>
<!-- Table heading -->
<thead>
<tr>
<th class="center">No.</th>
<th>Name</th>
<th>Email</th>
<th>Join</th>
<th>Last Login</th>
</tr>
</thead>
<!-- Table body -->
<tbody>
<?php
$i=1;
foreach ($dataProvider as $data){
?>
<tr>
<div>
<td class="center"><?php echo $i; ?></td>
<td><?php echo $data->name; ?></td>
<td><?php echo $data->email; ?></td>
<td><?php echo $data->join; ?></td>
<td><?php echo $data->last_login; ?></td>
</div>
</tr>
<?php $i++; } ?>
</tbody>
<!-- // Table body END -->
</table>
Thanks for any advice.
Regards
You can use jQuery to achieve this effect quite easily. Make two files:
1) One that includes the table.
2) One that has the select tag that will reload the first file upon change of the <select> tag.
Let's call the first file select.php
<script>
// Load the div with the contents of the table.php file with no GET parameter
$('div').load('table.php');
$('select').change(function() {
var name = $(this).val();
var data = 'name='+ name;
// Make sure that the table's contents don't change if the first option tag
// is selected.
if(name != '') {
$('div').load('table.php', data);
}
});
</script>
<div class="col-md-2">
<select class="form-control selectpicker">
<option value="">Name</option>
<?php
// print all name value from $administratorProvider
foreach($administratorProvider as $administrator){
?>
<option value="<?php $administrator->first_name ?>"><?php echo $administrator->first_name; ?></option>
<?php
}
?>
</select>
</div>
File two can be called table.php
$sql = "SELECT * FROM table WHERE name = '".$name."'";
$query = mysql_query($sql)or die(mysql_error());
$num = mysql_num_rows($query);
$i = 0;
while($row = mysql_fetch_array($query)) {
// Save your info as variables
$name[$i] = $row['name'];
$email[$i] = $row['email'];
$join[$i] = $row['join'];
$login[$i] = $row['last_login'];
}
?>
<div>
<table>
<thead>
<tr>
<th class="center">No.</th>
<th>Name</th>
<th>Email</th>
<th>Join</th>
<th>Last Login</th>
</tr>
</thead>
<tbody>
<?php
for($i=0;$i<$num;$i++) {
?>
<tr>
<div>
<td class="center"><?php echo $i; ?></td>
<td><?php echo $name[$i]; ?></td>
<td><?php echo $email[$i]; ?></td>
<td><?php echo $join[$i]; ?></td>
<td><?php echo $login[$i]; ?></td>
</div>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
Upon change of the select tag, the second file will be loaded with the value of the selected option tag sent to that file as a GET variable. You might wanna take the SQL part with a grain of salt as I'm not sure how you're fetching your relevant data.

How: HTML table row click results in POST form linking to subsequent page

Evening all, I have the form, which is populated from a sql database.
<table class="sortable" border="2" cellspacing="2" cellpadding="2">
<tr>
<th> Serial Number </th>
<th> Date </th>
<th> Time </th>
<th> Color </th>
<th> Design </th>
<th> Result </th>
</tr>
<?php
$i = 0;
while ($i < $num)
{
$serial = mysql_result($results, $i, 'serial_number');
$date = mysql_result($results, $i, 'date');
$time = mysql_result($results, $i, 'time');
$airport = mysql_result($results, $i, 'color');
$terminal = mysql_result($results, $i, 'design');
$result = mysql_result($results, $i, 'result');
?>
<tr>
<td> <?php echo $serial; ?> </a></td>
<td> <?php echo $date; ?> </td>
<td> <?php echo $time; ?> </td>
<td> <?php echo $color; ?> </td>
<td> <?php echo $design; ?> </td>
<td> <?php echo $result; ?> </td>
</tr>
<?php
$i++;
}
?>
What I would like to do is have each row of the table clickable. When each row is clicked, one cell of data from that row (the first cell) is sent via (POST) to the next page. Can a form be integrated into each tr??
You specify jQuery in your tags, so I assume you can use that.
// when any row is clicked
$('table').on('click', 'tr', function () {
// get the value
var value = $(this).find('td:first').text();
// redirect the user with the value as a GET variable
window.location = nextPage + '?data=' + value;
});
Where nextPage is the URL of the page you want to redirect to.
The short answer: yes, a form can be integrated into each tr.
The long answer - use just as you did before:
<tr>
<td>
<form method="post" target="details.php">
<input type="submit" name="more" value="<?php echo $serial; ?>"
</form>
<?php echo $serial; ?>
</td>
<td> <?php echo $date; ?> </td>
<td> <?php echo $time; ?> </td>
<td> <?php echo $color; ?> </td>
<td> <?php echo $design; ?> </td>
<td> <?php echo $result; ?> </td>
</tr>
But GET is easier, make a series of links that go details.php?number=123, or whichever:
<td>
<a href="details.php?number=<?php echo $serial; ?>"
<?php echo $serial; ?>
</a>
</td>
Although get can use a form, the data send is not user-customised, so the form data can be generated to use like a link.
Try with that code in echo when create your table:
<td><?php echo(''.$serial.'');?></td>
For each data that you have!
OTHER SOLUTION WITHOUT ACTION
<td><?php echo(''.$serial.'');?></td>
my_page.php - where to send the data
?[variable_name] - where is stored the data

loop through form

I have a couple of problems. I'm creating a form inside a table, from what I understand this is not a good idea. But because of the loop I want to make sure the table header is outside so it doesn't repeat. Is there a smarter way to do this?
Also more importantly I can't seem to get the delete button to remove the correct video. It seems to delete the last one in the list. Something wrong with how I'm looping over this?
<p>
<h3>Recorded Videos</h3>
<table id="webcam-table">
<thead>
<tr>
<td>Camera Name</td>
<td>Video Size</td>
<td>Date Created</td>
<td>Video Length</td>
<td>Video Options</td>
</tr>
</thead>
for($i=0;$i<$num_videos;$i++)
{
<form action="<?php htmlentities($_SERVER['PHP_SELF']);?>" method="POST">
<input type="hidden" name="video_id" value="<?php echo $result_videos[$i]["video_id"]; ?>" />
<tbody>
<tr>
<td>
<?php echo $result_videos[$i]["camera_name"]; ?>
</td>
<td>
<?php echo $result_videos[$i]["video_size"]; ?>
</td>
<td>
<?php echo $result_videos[$i]["video_datetime"]; ?>
</td>
<td>
<?php echo $result_videos[$i]["video_length"]; ?>
</td>
<td>
<input type="submit" name="delete_video" value="Delete" onClick="javascript:return confirm('Delete this video?');"/>
</td>
</tr>
</tbody>
}
echo "</table>";
echo "</form>";
echo "</p>";
}
}
if (isset($_POST['delete_video'])) {
$video_id = $_POST['video_id'];
$query_delete_video = 'DELETE FROM `#__videos` WHERE `video_id`='.$video_id;
$db->setQuery($query_delete_video);
$db->query();
header("location: " . $_SERVER['REQUEST_URI']);
In your loop you are creating the 'form' tag. However you are not closing it each time. This is causing your deleting problem.
Move
echo "</form>";
Inside the loop.
looks good to me, the only issue I see is that the tag should be outside the loop (opening before, closing after).
Revised code
<?
if (isset($_POST['delete_video']))
{
$video_id = $_POST['video_id'];
$query_delete_video = 'DELETE FROM `#__videos` WHERE `video_id`='.$video_id;
$db->setQuery($query_delete_video);
$db->query();
header("location: " . $_SERVER['REQUEST_URI']); //you should not allow things to be echoed before a header()
}
?>
<script type="text/javascript">
function onMySubmit(video_id)
{
document.myform.video_id.value = video_id;
return confirm('Delete this video?');
}
</script>
<p>
<h3>Recorded Videos</h3>
<!-- you had <?php htmlentities(...) ?>, you should have had
<?php echo htmlentities(...) ?> or <?= ... ?> -->
<form name="myform" action="<?= htmlentities($_SERVER['PHP_SELF']);?>" method="POST">
<input type="hidden" name="video_id" value="" />
<table id="webcam-table">
<thead>
<tr>
<td>Camera Name</td>
<td>Video Size</td>
<td>Date Created</td>
<td>Video Length</td>
<td>Video Options</td>
</tr>
</thead>
<tbody>
<? for($i=0;$i < $num_videos;$i++) { ?>
<tr>
<td><?= $result_videos[$i]["camera_name"]; ?></td>
<td><?= $result_videos[$i]["video_size"]; ?></td>
<td><?= $result_videos[$i]["video_datetime"]; ?></td>
<td><?= $result_videos[$i]["video_length"]; ?></td>
<td><input type="submit" name="delete_video" value="Delete" onClick="javascript:return onMySubmit(<?=$result_videos[$i]["video_id"];?>);"/></td>
</tr>
<? } ?>
</tbody>
</table>
</form>
</p>

Categories