Funk247
Funk247

Reputation: 330

Jquery - never mind dropping the leading zero, how do I keep it?

I have a jquery driven delete confirmation script that inserts values into hidden form fields and fires off a php delete script.

echo "<a class='delete' href='#' onclick='document.delAccForm.delAcc.value = ". $row['AccountNumber']."; document.delAccForm.delMyID.value = ". $row['MyID']."; document.delAccForm.delAccID.value = ". $row['AccountID'].";'>Delete</a>";

Form looks like this:

<form method="post" name="myform">
    <input type="hidden" name="AccountID" value="<?php echo $out['AccountID']; ?>" />
    <input type="hidden" name="MyID" value="<?php echo $out['PropertyID']; ?>" />
    <input type="hidden" name="formVar" value="">
</form>  

That all works fine, however certain entries cannot be deleted and on investigation I've found that any account number beginning with 0 is truncated to the first number above 0. This is either a browser action or Jquery treating the number as a value, how can I force it to treat it as a string.

I've tried this format, without success, the html this produced was pretty messed up.

<a class='delete' href='#' onclick='document.delAccForm.delAcc.value = '". $row['AccountNumber']."'; document.delAccForm.delMyID.value = '". $row['MyID']."'; document.delAccForm.delAccID.value = '". $row['AccountID']."';'>Delete</a>

Upvotes: 1

Views: 1586

Answers (1)

colestrode
colestrode

Reputation: 10658

EDIT

I think I understand your problem better now. You have HTML links created by PHP and you want to be able to update the values of a hidden HTML form based on values specific to that link. Then you want to submit the form when that link is clicked in order to do server-side processing. The leading 0's are being trimmed when you are setting the values of the form, so the question is how to prevent that.

HTML + PHP

Use custom data attributes to build up your anchors like so:

echo '<a class="delete" href="#" data-account-number="'.$row['AccountNumber'].'" data-account-id="'.$row['AccountId'].'" data-my-id="'.$row['MyId'].'">Delete</a>';

jQuery/javascript

Then use this jQuery to attach a click listener to all delete anchors. Note that I'm using the attr() function, not data(). data() will try to interpret the datatype and cast it for you, in this case casting your ID's as numbers and trimming the leading 0's. Using attr will ensure you are working with strings:

$(function() {
    $('a.delete').on('click', function(e) {
        var that = $(this);

        $('#delAcc').val(that.attr('data-account-number')); 
        $('#delMyID').val(that.attr('data-my-id')); 
        $('#delAccID').val(that.attr('data-account-id')); 

        $('form[name="myform"]').submit();
    });
});

http://api.jquery.com/jQuery.data/

http://api.jquery.com/attr/

Upvotes: 3

Related Questions