SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,033
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)

    Parse a javascript serverside.

    Is there a way to parse javascript code server side? Like a way to pass to a javascript engine the code to be eval'ed within the server and get a result back? The reason I'm wondering is it would be nice to write my validation rules for fields once and in once language. Since javascript can be disabled on the clientside it cannot be relied on for validation. However, if I have a serverside parser I can pass the code to be validated over to the js parser, have it return the results of the validation and proceed based on this.

    Advantage -> the coding should be faster to write since validations only need to be written once, and DRY methodology is preserved by having those validations expressed only once. Disadvantage -> This will be likely be slower than handling the validation purely in PHP, but unless the performance hit is sizable I'm willing to live with it on the application I'm planning on applying this to.

    Thoughts?

    (The reason this post is in PHP and not in Javascript is I intend to call the js parser from PHP and the majority of the code will be PHP. That is, I want to call a javascript parser from a PHP environment).

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a node.js

    I am not sure how you would call it from php, but probably can pass it using exec() or passthru()

  3. #3
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:


    Assuming that the validation has to be done on the server in all cases (because, as you said, you can never tell if JS is enabled or not), why not have all the validation done in PHP, instead? Then when you can validate on the client, AJAX it to the appropriate page and wait for the response?

    Off Topic:


    I hate it when I ask a question and, instead of simply answering, some jerk says, "Why don't just do {whatever}..." Because. That's why. :P

    I'm the web overlord for Graphic Business Systems

  4. #4
    Community Advisor silver trophy

    Join Date
    Nov 2006
    Location
    UK
    Posts
    2,551
    Mentioned
    40 Post(s)
    Tagged
    1 Thread(s)
    This might be useful:

    http://devzone.zend.com/1480/using-javascript-in-php-with-pecl-and-spidermonkey/

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    There's also the V8JS PECL extension.
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So what you want to do is make something really complicated?

    I am probably stupid, but why not use php validation and AJAX requests to validate the form on the fly? You can make an AJAX request to the server on every keyUp or Blur event and even if javascript is disabled the server will still validate the form in the same matter/using the same code.

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If Javascript is disable then you dont have onKeyUp and onBlur events. Dah

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ultra1 View Post
    If Javascript is disable then you dont have onKeyUp and onBlur events. Dah
    Doesn't matter the form will still be validated by the server.
    if your form action is "validate.php"
    and your ajax requests goes to "validate.php?field=the_field_that_triggered_the_blur_or_keyup&value=the_value_of_the_field&return=json"

    Or something.

    and on validate.php something like:

    PHP Code:
    <?php
    if(isset($_GET['field'])){
        
    $value $_GET['value']
        
    // validate the particular field and put errors in an array called $errors for example
    } else {
        
    // validate the entire form from the $_POST
    }

    if(isset(
    $_GET['return']) && $_GET['return'] == 'json'){
        return 
    json_encode($errors);
        exit;
    }

  9. #9
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,033
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by brense View Post
    So what you want to do is make something really complicated?

    I am probably stupid, but why not use php validation and AJAX requests to validate the form on the fly?
    That will chew through a considerable amount of bandwidth - more than doubling the data transmission per form. It would also be at the mercy of the internet for its responsiveness.

  10. #10
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    That will chew through a considerable amount of bandwidth - more than doubling the data transmission per form. It would also be at the mercy of the internet for its responsiveness.
    Aha! I think I understand what the problem is now

    I think i have seen something like this done using a combination of Zend Framework and jQuery validate plugin. It basically functioned like this:
    <input name="email" class="required email" />
    That would be generated using Zend Form in which the validators would be set and then jQuery validate would validate those input fields based on the values in the class attribute.

    I understand it's not really what you want though. It lacks custom validation rules and is kinda messy because it uses the class attribute.

    This is a nice challange. I am going to try and write a parser like what you suggest one of these days. It will be a nice addition to the "framework" I use.


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
  •