Jump to content

Need help with a Javascript function


Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi Everyone

 

I'm having some troubles (again) with jquery,

 

I have a table which is created by a while loop which loops out this info here:

//Contains a hidden input so we can submit a deletion of the receipt
echo "<tr><td>".$receiptName."</td><td>".$receiptNote."</td><td>£".$receiptValue."</td><td>".$receiptDate."</td><td>
<a href=".$url . $receiptPath . $receiptFile."><img class='zoom-image' style='width:auto;' src=".$url . $receiptPath . $justfileName.".jpg></img></a></td>
<td class='hidden'><input id='ReceiptId' class='hidden' name='FileID' value='".$fileId."'></td>
<td><a type='submit' href='#' OnClick='removeReceipt()'><h1 class='glyphicon glyphicon-remove-circle' style='opacity: 0.25;'></td></tr>";
} else {
//Contains a hidden input so we can submit a deletion of the receipt
echo "<tr><td>".$receiptName."</td><td>".$receiptNote."</td><td>£".$receiptValue."</td><td>".$receiptDate."</td><td>
<a href=".$url . $receiptPath . $receiptFile."><img class='zoom-image' style='width:auto;' src=".$url . $receiptPath . $receiptFile."></img></a></td>
<input id='ReceiptId' class='hidden' name='FileID' value='".$fileId."'>
<td><a type='submit' href='#' class='btn' OnClick='removeReceipt()'><h1 class='glyphicon glyphicon-remove-circle' style='opacity: 0.25;'></td></tr>";

and my Jquery is 

<script type="text/javascript">	
	function removeReceipt() {
      $.ajax({
			url: '<?php echo Thisurl();?>',
			data:{ReceiptId: $('#ReceiptId').val()
			},
			type: 'post',
			success:function() {
			window.location.reload(true);
			}
			
      });
 }
</script>

But I just realized this will only delete the top item on the list because ReceiptId Is exactly the same on every row so it just chooses the first one and deletes it.

 

Is there a way I can do something like?

 

OnClick='removeReceipt(".<?php echo $fileId.")

 

and have the javascript function read that variable?

 

e.g It would be something like OnClick='removeReceipt(32)'

 

the Javascript function can then take that 32 a supply it to the rest of the php from below

<?php 

//This will call the remove receipt function if called by the ajax form above.
if (isset($_POST['ReceiptId'])){
$receiptId = $_POST['ReceiptId'];
	
removeTheReceipt($receiptId);
	
}
?>
Link to comment
https://forums.phpfreaks.com/topic/299038-need-help-with-a-javascript-function/
Share on other sites

  • Solution

Yes you can echo the value of $fileId for passing its as an argument to the javascript function. Your javascript function definition then becomes

function removeReceipt(fileId) {
      $.ajax({
			url: '<?php echo Thisurl();?>',
			data:{ReceiptId: fileId },
			type: 'post',
			success:function() {
			window.location.reload(true);
			}
			
      });
}

You literally just responded after i head banged a bit and figured it out, I'm having massive trouble with getting my head around javascript....

 

Maybe I A training course is required :( its about £2500 though :|

 

Thanks for the response 

Ch0cu3r

I really appreciate all the help I get on this site.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.