0

I have a chatbot of sorts set in MSDOS terminal style. The user types their response then hits enter. I have found that when I tag the button with "display: none;" it causes the page to reload on several browsers (my windows chrome browser doesn't reload. not sure why). How can I have the button hidden yet the form and code functioning properly? I would rather not use "position: absolute;" to send it off screen.

HTML:

<div id="bot"><form>
<input id="user-response" type="text" placeholder="" autocomplete="off" autofocus />
<button id="user-submit" type="submit">Send</button>
</form></div>

JAVASCRIPT:

$('#bot').on('click', '#user-submit', function(e) {
e.preventDefault();
message = $('#user-response').val();
message = message.toLowerCase();
sendUserResponse();
getBotResponse(message);
});

I have tried various types of return: false;, event.preventDefault();, and such but everything works fine as long as I don't apply display: none; styling to the button. I've also changed it to input and type="button"/type="submit" but again display: none; causes the page to reload when hitting 'enter'. I have read about 20 different questions on page reload onclick query ajax etc... none of them seem to address this issue.

EDIT:

Used two of the suggestions below to check for key press "Enter" to submit the form but they continue the same pattern of failure. Form reloads the page if your submit button is styled display: none; The methods below work when the button is displayed visibly. Will check code for return values that may be causing submit and reload. Currently: Page with Visible button - works fine with any method. Page with Display: none button - causes reload under all given methods.

Would also like to mention that the page reload is not present on Chrome 49.0.2623.87 for Windows 7 but present on all OS X browsers and some Windows browsers.

EDIT 2

Bug: display: none; buttons cause page reload. Apparent on OS X browsers and some Windows. Solution: switch to full Ajax non-forms and should solve any problems. Will post working Ajax solution when put together. Correct answer below is for suggesting Ajax. Bug submitted to Chrome Dev and may be to other browsers debs later.

Chattervast
  • 156
  • 1
  • 11
  • you want to use enter? – mmativ Mar 16 '16 at 01:37
  • do you want it to submit when hidden or not? – Ammar Hasan Mar 16 '16 at 01:46
  • yes I want it to submit while it is hidden just by using enter. Think of using terminal or command prompt. no submit button just hit enter. I'm styling something similar to that but it's just a javascript page. I have it working with position: absolute; top: -9999px; but I don't want to keep that because of possibly styling issues later on down the road. – Chattervast Mar 16 '16 at 01:57

2 Answers2

1

I think you're looking for AJAX. AJAX lets you send the form data to a back end PHP file (that can then insert data into a DB, and/or get data from the DB) without reloading the page.

The page is reloading because you are using a <form>. When the submit button is pressed, it submits the form, which always refreshes the page. However, if you use AJAX you can send the data, (optionally receive a response) and carry on as normal.

In fact, when you are using AJAX you don't even need to use a <form> structure -- simple DIVs work just fine. Then you don't need to use event.preventDefault() to suppress the built-in form refresh.

Check out this post and especially its examples. Copy them onto your own system and see how they work. It's pretty simple.


Also, change the Submit button's type from type="submit" to type="button"


To detect when a user presses ENTER in the input field, and then click the submit button, try this:

$(function(){
    $('#user-response').keyup(function(){
        var keycode = (e.keyCode ? e.keyCode : e.which);
        if(keycode == '27'){ $('form').fadeOut();}
        if(keycode == '13'){ 
            $('#user_submit').click();
        }
    });
});
Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • I know that AJAX is where I will end up soon as my project will be expanding and I will be interacting with PHP and databases but my main concern with this question is to address the irregularity of button functions depending on the display value. I was wondering if anyone had overcome this problem and how to solve it as I am still pre-AJAX with my project. Your comment is helpful and definitely where I will be headed but I would like to have something useful here if someone comes across this problem in their own work. – Chattervast Mar 16 '16 at 02:03
  • I will be doing it. I just want to see the aforementioned problem solved. If it is an issue that hasn't been addressed then it might be of some consideration to browser developers or the W3C. But yet I will be doing AJAX soon. Thanks! – Chattervast Mar 16 '16 at 02:14
  • Thanks! I will try you suggestion when I begin work on the project again tomorrow and will respond then. – Chattervast Mar 16 '16 at 02:21
  • Using this causes the form to reload the page still. Not sure why as I tried returning false and preventing default as well but still with no success. Seems to be something else returning when the button is not displayed. This works until I style the submit button with display: none; – Chattervast Mar 16 '16 at 13:16
  • My solution is to absolute position the button to -9999px so it is off screen and works fine. I'm wondering why such odd behavior occurs because the button is display: none; I will switch to ajax today but I was hoping to find the reason to non-displayed buttons causing javascript functions to fail and reload the page. – Chattervast Mar 16 '16 at 13:28
  • yes, tried type="button" as well. Submitted a chrome bug to see if anything comes of it. I figure everyone is buried under piles of bugs anyways from people like me trying odd things. And I doubt Microsoft would care since IE trashes any standard for internet code ;) Thanks for your help! will be marking your suggestion as correct answer and editing to cite that Ajax is only way to overcome. – Chattervast Mar 16 '16 at 13:37
0

In such case when you don't need to have the submit button, just use form's submit method, like first you provide your form tag an id (e.g. myForm)

<div id="bot">
  <form id="myForm">
    <input id="user-response" type="text" placeholder="" autocomplete="off" autofocus />
    <button id="user-submit" type="submit" style="display: none !important;">Send</button>
  </form>
</div>

and then to submit the form use

document.getElementById("myForm").submit();

and if you want this to happen when you press enter in textbox then use it like

$('#user-response').on('keypress', function(event){
   if (event.which === 13 || event.keyCode === 13) {
      document.getElementById("myForm").submit();
   }
});
Ammar Hasan
  • 2,436
  • 16
  • 22
  • Still reloads the page when button is styled with display: none; but this otherwise works when you have the button styled visibly. – Chattervast Mar 16 '16 at 13:17