-3

Greeting developers, I search in all previous question from stackoverflow. There is no proper answer for this.I create two buttons inside form.One is addfriend and another one is unfriend which is disable. If the user click add , it direct to prompt box.After click ok for that,the unfriend button will able to click. Now my problem is it can submit my form but button is not work as expected until user logout. In all tutorial they show how to disable button only. Whenever i try, it work but not submit my form.I want my form submit and button disable until the user logout.Thanks in advance.

    <script>
function myFunction(form){
    var subject = prompt("Please enter Subject that want to study");
    var btn = document.getElementById("btn");
    var add = document.getElementById("add");
    btn.disabled=false;
    add.disabled=true;
    if (subject == null){
     form['subject'].value= subject;
    add.value="request sent";
    form.submit();
    return false;
    }
    else if(subject != null) {
        form['subject'].value= subject;
        add.value="request sent";
        btn.disabled=false;
        add.disabled=true;
        form.submit();
        return true;
            }

}
function unfriend(form){

    var btn = document.getElementById("btn");
    var add = document.getElementById("add");
    add.disabled=false;
    btn.disabled=true;
    add.value="request sent";   
    return true;
}
</script>
<form method="post" id="form" enctype="multipart/form-data" autocomplete="off" > 
    <input type="hidden" name="id" value="<?php echo $row['register_ID'];?>" />
    <input type="hidden" id="subject"  name="subject" data-uid=<?php echo $_SESSION['sid'] ;?>/>
    <td>
        <input type="submit" onclick="return myFunction(form)"name="addfriend" data-type='addfriend' id="add" class="btn" value="add" /> 
</form>
<form>                
    <input type="submit" value="unfriend" id="btn"  onclick="unfriend(form);" disabled="" /> 
    </td>   
</form>

1 Answers1

0

I am not an expert in javaScript but seem to understand the problem.

Two possible scenarios are:

1 - if you have input type="submit" then it will submit the form but your javascript method will not be invoked because after submitting the form you will be redirected to some web page either the same page or different page and so it is a new get request all together.

2 - If you have type="button" then it will not submit the form but your javascript method will be invoked as you will remain on the same web page.

check this for difference : Difference between <input type='button' /> and <input type='submit' />

Solution:

Keep input type=button and do an ajax call to the post method and in the success of the ajax call, enable/disable your add and unfriend buttons.

Don't have the code handy so unable to share code.

Hope it helps.

nazir_cinu
  • 66
  • 1
  • 6
  • yeah your true, but i want my data enter in prompt box send to database. here my problem in form submit make button not disable. is there have any way submit the form for button type ?? – sahana nazreen Aug 09 '18 at 07:30
  • @sahana : Yes, do this at last in your addfriend() method for input type='button' after disabling/enabling the button to submit the form "document.getElementById("myForm").submit()" – nazir_cinu Aug 09 '18 at 07:38