$.post is only submitting first row of results in while loo jquery - php

I have searched in many threads but can't seem to find the answer. I have a form that is being submitted based on a while loop. I have a jquery system which works but only submits the first result regardless of which row I have chosen to delete. I tried changing all the ID's to classes but that still doesn't work. I don't want to have to reload the page.
This is my code:
<script>
function DeleteHomework() {
var studentid = $(".studentid").val();
var <?php echo $classsubjects; ?> = $(".<?php echo $classsubjects; ?>").val();
var datesent = $(".datesent").val();
$.post("deletehomework.php", { studentid: studentid, datesent: datesent, <?php echo "$classsubjects: $classsubjects,"; ?> },
function(data) {
$('#deletehomeworkresult').html(data);
});
}
</script>
<form>
<?php
while ($row = mysqli_fetch_array($gethomeworkdata)) {
$datehomeworksent = $row['timestamp'];
$datehomeworksent123 = $row['timestamp'];
$datehomeworksent = date('d-m-Y', strtotime($datehomeworksent));
$homeworksubject = $row[$classsubjects];
$javadate = str_replace('-', '', $datehomeworksent);
?>
<tr><input type="text" hidden value="<?php echo $studentid; ?>" name="studentid" class="studentid"><input type="text" hidden value="<?php echo $datehomeworksent123; ?>" name="datesent" class="datesent">
<td><?php echo $datehomeworksent; ?></td>
<td><input type="text" class="form-control <?php echo $classsubjects; ?>" value="<?php echo $homeworksubject; ?>" name="<?php echo $classsubjects; ?>" id="<?php echo $classsubjects; ?>"></td>
<td>
<button type="button" rel="tooltip" class="btn btn-success" onclick="EditHomework();">
<i class="material-icons">edit</i>
</button>
<button type="button" rel="tooltip" class="btn btn-danger" onclick="DeleteHomework();">
<i class="material-icons">close</i>
</button>
</td>
</tr>
<?php
}?> </form>
Would appreciate any help knowing where i've gone wrong.

Related

Alternative $_POST method in PHP in Getting Values of a Button in a loop condition

I'm new to PHP so I am in need of your help.
My table in web is in a loop so it gets values in my mySQL phpMyAdmin database until it reaches the end of the database table.
I have a problem getting the desired value I want to show up in my table when certain button is pressed. Once I pressed a single button, it actually shows up the value of it, BUT, IT SHOWS THE OTHER BUTTON'S VALUE AS WELL.
In my conclusion, I think my $_POST code is missing something or is a complete mistake.
The code is here:
<tr>
<?php
while($row = mysqli_fetch_assoc($query)){
$exerciseid = $row['exerciseid'];
$question = $row['question'];
$a = $row['answera'];
$b = $row['answerb'];
$c = $row['answerc'];
$correctanswer = $row['answer'];
?>
</tr>
<tr>
<td><?php echo $question?></td>
<td><button class="btn btn-primary" value="<?php echo $a?>" name="a-submit">A. <?php echo $a?></button></td>
<td><button class="btn btn-primary" value="<?php echo $b?>" name="b-submit">B. <?php echo $b?></button></td>
<td><button class="btn btn-primary" value="<?php echo $c?>" name="c-submit">C. <?php echo $c?></button></td>
<td><?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
if(isset($_POST['a-submit'])){
echo $a;
}
else if(isset($_POST['b-submit'])){
echo $b;
}
else if(isset($_POST['c-submit'])){
echo $c;
}
else{
}
}
?>
</td>
</tr>
<?php
}
?>
What do you think is the possible cause of my mistake? Please help
If I am not wrong, all your buttons are in single form that's why you are getting all the buttons value when you click any button.
I am not sure why you need post here because, if you want to get the button value you can simply get it using jQuery.
Check my sample code.
<script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>
<?php
for ($i = 1; $i < 5; $i++) {
?>
<table>
<tr>
<td>Question <?php echo $i; ?></td>
<td><button class="btn btn-primary btn-answer" value="Answer 1" rel="question<?php echo $i; ?>" name="a-submit">A. Answer 1</button></td>
<td><button class="btn btn-primary btn-answer" value="Answer 2" rel="question<?php echo $i; ?>" name="b-submit">B. Answer 2</button></td>
<td><button class="btn btn-primary btn-answer" value="Answer 3" rel="question<?php echo $i; ?>" name="c-submit">C. Answer 3</button></td>
<td class="question<?php echo $i; ?>-answ"></td>
</tr>
</table>
<?php
}
?>
<script type="text/javascript">
$(document).ready(function () {
$('.btn-answer').on('click', function () {
$('.' + $(this).attr('rel') + '-answ').html($(this).val());
});
});
</script>
class "btn-answer" -> global class for all answer buttons to call jquery click action
rel -> to identify the question
question<?php echo $i; ?>-answ -> to identify the answer block the show the user selection
If you still want to use the same variable name within a loop, and display the value that was submitted by user, you could do it like below :
<tr>
<td><?php echo $question?></td>
<td><button class="btn btn-primary" value="<?php echo $a?>" name="a-submit">A. <?php echo $a?></button></td>
<td><button class="btn btn-primary" value="<?php echo $b?>" name="b-submit">B. <?php echo $b?></button></td>
<td><button class="btn btn-primary" value="<?php echo $c?>" name="c-submit">C. <?php echo $c?></button></td>
<td><?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
if(isset($_POST["a-submit"]) && $_POST["a-submit"] == $a){
echo $a;
}
else if(isset($_POST["b-submit"]) && $_POST["b-submit"] == $b){
echo $b;
}
else if(isset($_POST["c-submit"]) && $_POST["c-submit"] == $c){
echo $c;
}
else{
}
}
?>
</td>
</tr>
But this will not prevent multiple answer from being shown because no unique id been given on each variable (for example if there are question which having the same answer).

PHP value wont post

Hello for some reason job_id will not parse from myjobs.php to payment.php and I really cannot see how.Does anyone know why this may be?Similar files seem to work but for some reason this wont I am thinking maybe because I also have html code on payment.php which i haven't done before?
<?php while($row = mysqli_fetch_array($result)):?>
<tr>
<td><?php echo $row['job_id']; ?></td>
<td><?php echo $row['title'];?></td>
<td><?php echo $row['description'];?></td>
<td><?php if($row['accepted']==1 AND $row['start_escrow']==0):?><form action = "payment.php">
<input type="hidden" value="<?php echo $row['job_id']?>" name="job_id" />
<input type="submit" class="btn btn-xlarge btn-block btn-primary" value ="Start Escrow"></input></input><?php endif; ?></td>
<td><?php if($row['start_escrow']==1):?><form action = "review.php">
<input type="hidden" value="<?php echo $row['job_id']?>" name="job_id" />
<input type="submit" class="btn btn-xlarge btn-block btn-primary" value ="Start Escrow"></input></input><?php endif; ?></td>
</tr>
<?php endwhile;?>
</table>
payment.php
<?php
require 'config.php';
$jobid = $_POST['job_id'];
$query = "UPDATE job SET start_escrow = '1' WHERE job_id = '$jobid''";
$success = $conn->query($query);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
echo "Thank You For Contacting Us <br>";
$conn->close();
?>
Set your method as POST in form.
<form action = "payment.php" method="POST">
<form action = "review.php" method="POST">

How to get the values from fields in the current row of a while loop when a button if clicked in php

I need help Please: How do I get the values of the fields in the row i click when the button with id of $id is clicked. I have tried the code below but when i run the script, I get only the value of the first row even if I clicked the button on the last row
<tbody>
<?php
$sn = 1;
$marksheet = $_GET['id'];
$query = " SELECT * FROM $marksheet ";
$result = mysqli_query($dbConn,$query) or die("Error: ".mysqli_error($dbConn));
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$student_id = $row['student_id'];
$regno = $row['regno'];
$full_name = $row['full_name'];
$t1a1 = $row['t1a1'];
$t1a2 = $row['t1a2'];
$t1a3 = $row['t1a3'];
$t1ex = $row['t1ex'];
$t2a1 = $row['t2a1'];
$t2a2 = $row['t2a2'];
$t2a3 = $row['t2a3'];
$t2ex = $row['t2ex'];
$t3a1 = $row['t3a1'];
$t3a2 = $row['t3a2'];
$t3a3 = $row['t3a3'];
$t3ex = $row['t3ex'];
$marksheet = $marksheet;
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $student_id; ?></td>
<td><?php echo $regno; ?></td>
<td><?php echo $full_name; ?></td>
<td>
<input type="text" class="form-control" id="ca1" name="ca1" value="<?php echo $t1a1; ?>" />
</td>
<td>
<input type="text" class="form-control" id="ca2" name="ca2" value="<?php echo $t1a2 ?>" />
</td>
<td>
<input type="text" class="form-control" id="ca3" name="ca3" value="<?php echo $t1a3; ?>" />
</td>
<td>
<input type="text" class="form-control" id="ex" name="ex" value="<?php echo $t1ex; ?>" />
</td>
<td>
<div class="btn-group">
<button class="btn btn-primary btn-xs" id="<?php echo $id; ?>" href="#" > <i class="fa fa-save"></i> </button>
</div>
</td>
<input type="hidden" class="form-control" id="marksheet1" value="<?php echo $marksheet; ?>" />
</tr>
<?php
echo "<script>
$(\"#$id\").click(function(){
var id = $id;
var marksheet = $('#marksheet1').val();
var ca1 = $('#ca1').val();
var ca2 = $('#ca2').val();
var ca3 = $('#ca3').val();
var ex = $('#ex').val();
$.ajax({
url: 'update_marksheet.php',
method: 'post',
data:{
id:id,
marksheet:marksheet,
ca1:ca1,
ca2:ca2,
ca3:ca3,
ex:ex
},
success: function(response){
alert(response);
$('#$id').addClass('fa fa-check btn-success');
}
});
});
</script>";
$sn++;
}
?>
</tbody>

php function call messes up table

I've got a html table I build from the database:
<tr onclick="DoNav('<?php echo $result_videos[$i]["video_url"]; ?>');">
<td>
<?php echo $result_videos[$i]["camera_name"]; ?>
</td>
<td>
Timezone: <?php echo queryTimezone(); ?> <br>
Video Size: <?php echo $result_videos[$i]["video_size"]; ?> bytes <br>
Video Length: <?php echo strTime($result_videos[$i]["video_length"]); ?>
</td>
<td>
<form name="myform" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">
<input type="submit" name="delete_video" value="Delete" title="Delete this video" onClick="return confirm('Are you sure you want to delete?')"/>
<input type="hidden" name="video_url" value="<?php echo $result_videos[$i]["video_url"]; ?>" />
</form>
</td>
</tr>
The problem is this function:
function queryTimezone()
{
$query = "SELECT timezone FROM #__camcloud_users WHERE user_id=".$user->id;
$db->setQuery($query);
$timezone = $db->loadResult();
return $timezone;
}
It messes the table up. I don't seem to get any php errors and it cuts off the table at the "Timezone:" part and throws it to the top right of the page. Basically where I call the function queryTimezone. If I change to put this inside the html table it works fine:
<tr onclick="DoNav('<?php echo $result_videos[$i]["video_url"]; ?>');">
<td>
<?php echo $result_videos[$i]["camera_name"]; ?>
</td>
<td>
Timezone: <?php
$query = "SELECT timezone FROM #__camcloud_users WHERE user_id=".$user->id;
$db->setQuery($query);
$timezone = $db->loadResult();
echo $timezone;
?> <br>
Video Size: <?php echo $result_videos[$i]["video_size"]; ?> bytes <br>
Video Length: <?php echo strTime($result_videos[$i]["video_length"]); ?>
</td>
<td>
<form name="myform" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">
<input type="submit" name="delete_video" value="Delete" title="Delete this video" onClick="return confirm('Are you sure you want to delete?')"/>
<input type="hidden" name="video_url" value="<?php echo $result_videos[$i]["video_url"]; ?>" />
</form>
</td>
</tr>
This works fine. Did I do something wrong with how I call the function?
The function cannot determine what $user->id, therefore doesnt return a valid result... Theres nothing in the function that defines what $user is
Use his function declaration instead:
function queryTimezone($user,$db)
{
$query = "SELECT timezone FROM #__camcloud_users WHERE user_id=".$user->id;
$db->setQuery($query);
$timezone = $db->loadResult();
return $timezone;
}
call this way:
Timezone: <?php echo queryTimezone($user,$db); ?> <br>

Jquery auto-compute

My goal is to be able to compute the subtotal from given 2 values. First value is given, and the other one is inputted. The subtotal changes when the user inputs a different value into the quantity field.
Here's the jquery code that I'm using:
<script type="text/javascript">
$(function(){
$('input[id^=qty]').keyup(function(){
var parentDiv = $(this).closest('#axd');
var curprice=parentDiv.find('input[id^=price]').val();
var curqty= this.value;
var curtotal=curprice * curqty;
parentDiv.find('input[id^=comp]').val(curtotal);
});
});
And here's the body:
<tr>
<form name="cartform" method="get" onsubmit="return validate_form(this);" action="checkout.php">
<div id="axd">
<td><?php echo $name; ?></td>
<input type="hidden" id="pids" name="ids[]" value="<?php echo $id; ?>"/>
<input type="hidden" name="qoh[]" value="<?php echo $qtyhand; ?>"/>
<input type="hidden" name="dprice[]" value="<?php echo $dsprice; ?>"/>
<td><?php echo $qtyhand; ?></td>
<td><input type="text" id="<?php echo 'qty' . $id; ?>" name="qbuys[]" value=""/></td>
<input type="hidden" id="<?php echo 'price' . $id; ?>" name="sprice[]" value="<?php echo $ssprice; ?>"/>
<td><?php echo $ssprice; ?></td>
<td><input type="text" name="subtot[]" id="<?php echo 'comp' . $id; ?>" value=""/></td>
<td><img src="../img/system/delete-icon.png"></img></td>
</div>
</form>
</tr>
The error is that it doesn't output any value at all. What's wrong with my code?
Here's the link to the full version of the code: http://cu.pastebin.com/sWxFV6Hg
why not just assign each field an ID and use the classic javascript instead of jquery for one multiply and a sum.
go like this: - assign ids then write this function:
function operation()
{
var op1 = document.getElementById('id1').value;
var op2 = document.getElementById('id2').value;
document.getElementById('wherever_you_want_it').value = op1 (whatever operation) op2;
}
Call the function on whatever event you want. And... done it.

Categories