tags:

views:

103

answers:

5

hi,

i wanna disable a submit button when onclick. im able to disable the button but i cant submit the post value to php.

+1  A: 
<input type="submit" onclick="this.disabled = true" value="Save"/>

or ref this

Salil
hi, this works but if i wanan submit to php, the POST value is missing. however, i have found an alternative solution which i will post below. feel free to comment on it.
The value won't be persisted through post if this is done.
Polaris878
+1  A: 

i found a alternative online. wat i did is to create a fake disable and hidden button. when the actual button is clicked, i will hide it and show the fake disable button.

actual button:

$onclick = "
var boolconfirm = confirm('$strconfirmattempt');
if(boolconfirm==true){
finishattempt.style.display='none';
finishattempt2.style.display='inline';
}
return boolconfirm;";

fake button:

echo "<input type=\"submit\" name=\"finishattempt\" value=\"submit\" onclick=\"$onclick\" />.
<input type=\"submit\" id=\"finishattempt2\" name=\"finishattempt2\" value=\"submit\" style=\"display:none;\" DISABLED/>\n";
Format your code better. It shouldn't be in an incomprehensible chunk like that. And once you've formatted it, highlight the code and hit `Ctrl+K`, which will indent everything by 4 spaces, causing it to be displayed as pre-formatted source-code.
Lèse majesté
+1 for finding a workable solution and formatting your code. Though in the future you might want to break the code into more lines so that the horizontal scrollbars don't appear. But as it is it's already 100x better than it looked before.
Lèse majesté
haha ok tks. i will try to format it better next time.
+2  A: 

If you disable an input, then its value naturally won't be included in the form data. You'll need to disable the button after you submit. If you bind a callback to onclick, then it runs before the form submits.

What you need is something like this:

jQuery:

$(document).ready(function() {
    $(document).unload(function() {
        $('#submit-btn').attr('disabled', 'disabled');
    });
});

Regular JS:

document.onunload = disableSubmit;
function disableSubmit() {
    /* disable the submit button here */
}

Basically, instead of binding to the submit button's onclick event, this binds the disabling code to the document's unload event (document.onunload), which gets fired once the form is submitted and you begin to leave the page.

Lèse majesté
Add something about how your code is jQuery and not plain old JS
Polaris878
Ah, yea, I had mentioned that the first snippet was jQuery in one of my edits, but I think I accidentally deleted it.
Lèse majesté
+1  A: 

You could use a hidden field which would hold the value of the button and pull that value out of your POST data:

<input type="hidden" id="hiddenField" value="default" />

<input type="button" id="myButton" onclick="buttonClick();">

function buttonClick()
{
    document.myForm.myButton.disabled = true;
    document.myForm.hiddenField.value = "myButtonClicked";
}

My PHP is a little rusty, but then you can access the hidden field like so:

if ($POST['hiddenField'] == "myButtonClicked")
{
    // Click handling code here
}
Polaris878
+1  A: 

Why not create a disabled submit button that is hidden, and an active submit button, and onClick show the disabled and hide the active? I could do this in jQuery, but I'm kinda useless without it. Sad, eh?

melee