0

I'm running into a problem with changing the name attribute on an input element when a button is clicked.

I have 3 buttons:
* Add Renter
* Delete Customer
* Update Customer

This is the input element:

<input type=\"checkbox\" name=\"todelete[]\" value=\"$busRow[0]\" class=\"check\" />

$busRow is the user's id..

The form is submitted to a php script that checks to see what button was set and does different actions for that button. When the user clicks the delete it loops through and deletes each customer that was in the todelete[] array. When the user clicks the Update Customer button i want to change the checkbox input's name to "id". Then my php script will grab the user's id instead of grabbing the array value and then redirect the user to the Update Customer page with the id stored on the URL.

This is the javascript i'm using at the bottom of the page:

<script type="text/javascript">
    $("button").click(function () {
        if ($(this).text() == "Update Customer") {
            $('input.check').attr("name", "id");
        }
    });
</script>

When i get to the Update Customer page the id is not there in the URL and i can't pull any values based upon the ID :(

1
  • Can you provide the redirect code? Commented Apr 25, 2011 at 16:36

2 Answers 2

1

You're setting the name value to "id", instead of the value stored in value. Try this:

$("button").click(function () {

    if ($(this).text() == "Update Customer") {
        var element = $('input.check');
        element.attr("name", element.attr("value"));
    }
 });

Although, I think you could just simply pass the value of the input tag instead of the name when the btn clicked is 'Update'. That would save you the hassle of changing these different attributes and could save you trouble.

Edit: I'm reading the OP again and see you want to set the name att to "id" and not the user's id??? You're question is vague and hard to understand what you're trying to do. You're leaving out crucial code that grabs this "id" and redirects the user.

Edit2: What's up with the semi-colon between your string concat? I hope that's not in your source code. Now seeing your redirect code. I think your problem lies in trying to change the tag's attributes/values with an 'input.check' selector. I would also say you should redesign the way you are implementing this. Your checkboxes shouldn't hold your user information. But regardless, I don't think you need to even change the name value. If update isset() then just make $id = (its current name value);
Download firebug to debug your javascript code. Firebug will also show you errors in your code that wouldn't see otherwise. It should help you out a lot

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this and it still didn't grab the id in my php script.
1

Try putting an alert inside the if statement to check if that is actually becoming true, to narrow down what might be the cause.

Also I've never seen input.check used as a selector. Perhaps give the button an id making it:

<input id=\"customercheck\" type=\"checkbox\" name=\"todelete[]\" value=\"$busRow[0]\" class=\"check\" />

Then change the attribute via the ID like so:

$('#customercheck').attr("name", "id");

that also saves you if you add another input to the field later down the road.

3 Comments

I agree, the input tag needs an ID to make life easier
Why you use the scape '\' character?
I only used the escape because the OP did (I assume he copied it out of a PHP echo statement). If you are just adding that code to a HTML page it is unnecessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.