SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,106
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post How to have success in .submit

    Hi, can i ask if this is my form and i will use the .submit how do i returned message successfully or failed after submitting the form using .submit


    HTML Code:
     <form id="myform" action="tomyprocess.php">
        <input type="text" name="Fname">
       <input type="text" name="Mname">
      <input type="text" name="Lname">
    </form>
    is this correct ?

    Code:
     $(function(){
       $('myform').submit(function(data){
            if(data=='successfull'){
             alert("success");
         } 
         
       });
    });
    thank you in advance.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,065
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    .submit doesn't return any data, it just submits the form, and thus refreshes the page. It seems like you are looking to post your form with AJAX.

    Code:
    $('#myform').submit(function() {
        $.post($(this).attr('action'), $(this).serialize(), function(data) {
            if (data === 'successfull') {
                alert('success!');
            } else {
                alert('A problem occurred while submitting your data. Please try again later.');
            }
        });
        return false; // don't actually submit, let AJAX handle the posting
    });
    This assumes the page you post to only contains the word "successfull" and nothing else.

    Your code should also be able to work without javascript, so for example, make sure you also validate at the server side, not just in javascript.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,106
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    .submit doesn't return any data, it just submits the form, and thus refreshes the page. It seems like you are looking to post your form with AJAX.

    Code:
    $('#myform').submit(function() {
        $.post($(this).attr('action'), $(this).serialize(), function(data) {
            if (data === 'successfull') {
                alert('success!');
            } else {
                alert('A problem occurred while submitting your data. Please try again later.');
            }
        });
        return false; // don't actually submit, let AJAX handle the posting
    });
    This assumes the page you post to only contains the word "successfull" and nothing else.

    Your code should also be able to work without javascript, so for example, make sure you also validate at the server side, not just in javascript.

    Hi ScallioXTX, Thank you for the reply, is that code will work if i have input type="file" ?can i still get the value of uploaded file?

    Thank you in advance.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,065
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    No I don't think it will function with file uploads, at least not in all browsers.
    Is it just one file and nothing else? If so, you may want to look at an AJAX file uploader, like fine uploader
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,106
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you.,,can i ask what if i will get the id of the file example

    HTML Code:
    <input type="file" id="upload" name="upload">
    Then the request

    Code:
      $('#myform').submit(function(){
       // Then send via ajax
         var xx = $('#upload').val();
    
        $.ajax({
           type: 'post',
           data: {up:xx},
           url: 'to_process.php',
           success:function(data){
             alert(data);
         }
    
      });
    });
    I can see now the value is posted but i am not sure if this the right way to have file upload.


    please correct me if i am wrong

    Thank you.

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,065
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You get the name of the file but not the file itself. Please read my previous post again.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •