SitePoint Sponsor

User Tag List

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

    Post How do i return the error message

    Hi, I am having problem in my trappings...I am submitting the form to other page which is adduser.php via jquery.ajax, and then in the adduser.php i have server side validation for the fields but the problem is how can i returned the message back to to the register.php ?

    register.php

    Code:
        formdata = $('#userform').serialize();
       $.ajax({
                         type: 'post',
                         data: formdata,
                         url: "adduser.php",
                         success: function(data){
                              if(data=="ok"){
                                  //process some script here...
                              }
                          }
    
    
                      });
    adduser.php

    PHP Code:

     
    include_once 'myfunction.php';

        if(isset(
    $_POST['firstname'])){

          
    $firstname   $_POST['firstname'];
          
    $middlename   $_POST['middlename'];
          
    $firstname  $_POST['lastname'];
          
    $address    $_POST['address'];
      
          
    $error validate_firstname($firstname);
         
         if(
    $error != ' '){
           
    //how do i return back to my register.php ?
      
    }
        
      
    adduser($firstname,$middlename,$lastname,$address);



    Thank you in advance.

  2. #2
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,001
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My bad, i was thinking that the JS is disabled,..anyways the user cannot submit the form if the js is disabled...sorry for this.

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by jemz View Post
    My bad, i was thinking that the JS is disabled,..anyways the user cannot submit the form if the js is disabled...sorry for this.
    Sure they can, there are a lot of tools out there to post arbitrary data to arbitrary URLs. You always need to check input on the server. Always. No excuses.

    In this case I would return JSON. Something like

    PHP Code:
    include_once 'myfunction.php';

    if (isset(
    $_POST['firstname'])) {
          
    $firstname   $_POST['firstname'];
          
    $middlename   $_POST['middlename'];
          
    $firstname  $_POST['lastname'];
          
    $address    $_POST['address'];
      
          
    $error validate_firstname($firstname);
         
         if (
    $error != ' '){
             echo 
    json_encode(array('error' => true'errorMessage' => $error));
             exit;
         }
      }
        
      
    adduser($firstname,$middlename,$lastname,$address);

    then in the javascript you can check for data.error, and if it is set, display data.errorMessage somewhere
    Rémon - Hosting Advisor

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

  4. #4
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,001
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Sure they can, there are a lot of tools out there to post arbitrary data to arbitrary URLs. You always need to check input on the server. Always. No excuses.

    In this case I would return JSON. Something like

    PHP Code:
    include_once 'myfunction.php';

    if (isset(
    $_POST['firstname'])) {
          
    $firstname   $_POST['firstname'];
          
    $middlename   $_POST['middlename'];
          
    $firstname  $_POST['lastname'];
          
    $address    $_POST['address'];
      
          
    $error validate_firstname($firstname);
         
         if (
    $error != ' '){
             echo 
    json_encode(array('error' => true'errorMessage' => $error));
             exit;
         }
      }
        
      
    adduser($firstname,$middlename,$lastname,$address);

    then in the javascript you can check for data.error, and if it is set, display data.errorMessage somewhere
    Hi Thank you for the reply...but what if the user will try to use the firebug and they let the error message to false in my condition in my js.does this will not cause problem?...can you please show me how will you check in the JS so that i can have some idea.

    Thank you in advance.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    If they fake the response in Firebug they are only fooling themselves because that has no effect on what happens on the server.
    Rémon - Hosting Advisor

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

  6. #6
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,001
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for enlighten my mind.

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    As for the JS, it goes a little something like this

    Code javascript:
    var formdata = $('#userform').serialize();
    $.ajax({
        type: 'post',
        data: formdata,
        url: "adduser.php",
        success: function(data) {
            if (data.error) {
                alert(data.errorMessage);
            } elseif (data === 'ok') {
                // continue processing
            } else {
                // unknown response
            }
        }
    });
    Rémon - Hosting Advisor

    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
  •