We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    I have been two days trying to find out how to handle a form submission so that when the user clicks on the "submit" button the prototype Updater is activated but the form is not submitted.

    This code is a sample that I found that will display your name as you type it.

    Event.observe(window, 'load', init, false);
    	
    	function init(){
    		$('greeting-submit').style.display = 'none';
    		Event.observe('greeting-name', 'keyup', greet, false);
    	}
    
    	function greet(){
    	  	var url = 'greeting.php';
    		var pars = 'greeting-name='+escape($F('greeting-name'));
    		var target = 'greeting';
    		var myAjax = new Ajax.Updater(target, url, {	method: 'get',	parameters: pars});
    	}
    


    It works great. But I want to show the submit button (easy-just remove the display:none line), and display the message when the submit button is clicked without actually submitting the form. Keyup and onblur may be "cute", but the user expects to see a "Submit" button.

    Anybody have any ideas how to do that using Prototype? I can do it in raw Javascript, but I really want to try to learn how to use the Prototype library.
      Studying MODX in the desert - http://sottwell.com
      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
      Join the Slack Community - http://modx.org
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Set up a <span id="greeting-submit">Click here</span> to replace the submit input, changed the observer to

      Event.observe('greeting-submit', 'click', greet, false);


      and it works perfectly. But that still does not help with non-javascript users; they still need a submit button. I am sure there must be a way to use the button’s click event and block the form’s submitting.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 4018
        • 1,131 Posts
        Honestly? According to some sources, the number of users who have disabled javascript are anywhere from less than 1% to 10%. So the question to ask yourself is this: For the site you’re building, who is your target audience? Based on that and current statistics, what’s the likelihood that users will have javascript turned off? If the site uses any sort of AJAX methods then it doesn’t matter because anyone with javascript turned off won’t be able to use the site anyways. I personally don’t care. I’ve built sites with alot javascript functionality and if I had to develop them with non-javascript users in mind I wouldn’t be able to develop them.

        Now...about your Submit button problem. You should be able to have a submit button and alter the functionality of the submit button via javascript. I’m sure it’s just a matter of using an onSubmit event or something.
          Jeff Whitfield

          "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Hm...isn’t that pretty close to the percentage of non-IE users?
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 1764
            • 680 Posts
            Returning false from the onsubmit event will prevent the submit from going through. For example:

            <form action="http://www.modxcms.com" onsubmit=" /* call AJAX stuff here */ return false;">
             <button type="submit">Submit</button>
            </form>
            
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              That will work, I was just sure that there was some parameter or something to have the Prototype kill the form submission. Plus I’m trying to get all the javascript out of the HTML, using listener objects instead. Separate behaviour from presentation and all that.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 1764
                • 680 Posts
                Quote from: sottwell at Feb 08, 2006, 08:18 AM

                That will work, I was just sure that there was some parameter or something to have the Prototype kill the form submission.

                Not that I know of, but I don’t know prototype that well either.

                Quote from: sottwell at Feb 08, 2006, 08:18 AM

                Plus I’m trying to get all the javascript out of the HTML, using listener objects instead. Separate behaviour from presentation and all that.

                You may still be able to. Try returning false from your onSubmit listener. It may work.