Brandin Arsenault Brandin Arsenault - 2 months ago 6
Javascript Question

ID Not Submitting To Query

I appreciate your responses in advance.

I have several pages of orders that I am dealing with, however I am sure fixing one will provide me with enough knowledge to fix the others. In this case, I will show you the

daytasks.php
section. Basically, I can make function changes to the first line, but the second, third, etc. lines cannot be modified. Using the function buttons does nothing for those lines.
daytasks.php
calls upon
fetchdaytasks.php
to get the information based on date, which calls upon
./functions/additions.js
to perform the Javascript, which calls upon the functions corresponding script to perform the action requested.

e.g. I have two orders, each in their own separate
<tr>
, I can modify the first row, which is ID #
28
with any of the functions, but the second row, ID #
35
I cannot modify, it will simply not make any changes.

Portion of
daytasks.php


<div class="form-group">
<br />
<center>
<div class="input-group">
<input type="date" id="search_text" name="search_text" />
</div>
<div id="result"></div>
</center>
</div>


Portion of
fetchdaytasks.php


if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$id=$row['id'];
$orderdate=$row['date'];
$order=$row['order'];
$status=$row['status'];
echo "
<td><center>$id</center></td>
<td><center>$orderdate</center></td>
<td><center>$order</center></td>
<td><center>$status</center></td>
<form>
<input type='hidden' id='changeid' name='changeid' value='$id' />
<td><center><button type='button' name='btn_ship' id='btn_ship' class='btn btn-xs btn-success' data-id='$id'>SHIP</button> <button type='button' name='btn_unship' id='btn_unship' class='btn btn-xs btn-danger btn_delete'>UNSHIP</button> <button type='button' name='btn_bo' id='btn_bo' class='btn btn-xs btn-danger btn_delete'>BACKORDER</button> <button type='button' name='btn_fbo' id='btn_fbo' class='btn btn-xs btn-danger btn_delete'>FBO</button><br />
<button type='button' name='btn_cancel' id='btn_cancel' class='btn btn-xs btn-danger btn_delete'>CANCEL</button> <button type='button' name='btn_return' id='btn_return' class='btn btn-xs btn-danger btn_delete'>RETURN</button> <button type='button' name='btn_dam' id='btn_dam' class='btn btn-xs btn-danger btn_delete'>DAMAGED</button> <button type='button' name='btn_lost' id='btn_lost' class='btn btn-xs btn-danger btn_delete'>LOST</button></td>
</form>
</tr>
";
}
}
else
{
echo "No Orders For This Date";
}


Portion of
additions.js


$("#btn_ship").click(function(){
var changeid = $( this ).data( 'id );
var myData={"changeid":changeid};
$.ajax({
url : "./functions/shiporder.php",
type: "POST",
data : myData,
success: function(data,status,xhr)
{
$("#status_text").html(data);
$('#changeid').val();
}
});
});

$("#btn_fbo").click(function(){
var changeid = $('#changeid').val();
var myData={"changeid":changeid};
$.ajax({
url : "./functions/fboorder.php",
type: "POST",
data : myData,
success: function(data,status,xhr)
{
$("#status_text").html(data);
$('#changeid').val();
}
});
});


./functions/fboorder.php


<?php
include_once('/home/brandina/public_html/eldis/config/config.php');
$changeid = $_POST['changeid'];
if(mysqli_query($connect, "UPDATE `orders` SET `status`='FBO' WHERE `id`='$changeid'"))
?>


./functions/shiporder.php


<?php
include_once('/home/brandina/public_html/eldis/config/config.php');
$changeid = $_POST['changeid'];
if(mysqli_query($connect, "UPDATE `orders` SET `status`='SHIP' WHERE `id`='$changeid'"))
?>

Answer

Because you are looping in fetchdaytasks, this line will be repeated: <input type='hidden' id='changeid' name='changeid' value='$id' />

That means you will have multiple elements with an "id" of 'changeid'. In your JS you have var changeid = $('#changeid').val(); This does not know which #changeid you are talking about.

So I would get rid of the hidden input and work with "data-XXX" and put that on the buttons or on the form.

So <button type='button' name='btn_ship' id='btn_ship' class='btn btn-xs btn-success' data-id='$id'>SHIP</button> or <form data-id='$id'>

And then get the id in the JS like: var changeid = $( this ).data( 'id ); in case you do it on the button.

Comments