質問

Hi i'm making a reusable javascript/ajax function used for validating input textboxes, checkboxes..etc. But I'm trying to create some sort of a generic validator using ajax. and so far i've managed to make my url and data dynamic by having this:

<?php
    //contains php variables passed during after rendering the view
    $url = //...some url;
    $data = // array()... some array containing parameters;
    $success = // should contain a string defining a function;
?>

So i get the needed php variables and pass them onto javascript, to use it into ajax like this :

<script>
$(document).ready(function(){
   var submitUrl = '<?php echo Yii::app()->createAbsoluteUrl($url); ?>';
   var params = <?php echo json_encode($data); ?>;

   $.ajax({
      url : submitUrl,
      type : 'POST',
      async : false,
      data : params,
      success : ??? // i don't know how to define this function dynamically based on php values

   });


});
</script>

I don't know how to define success function dynamically. I tried defining :

var successFunction = <?php echo json_encode("function(response){ return 'sample response' ; }");?>;

but no luck. How do i put a success function dynamically from a string defining custom success function? Many thanks!

---------------------------------added-------------------------------------------

Practically i'm using X-edtiable extension.

$('.editable').editable({
        type : editableType,
        savenochange : true,
        pk : 1,
        emptytext: 'Click to edit',
        params : function(){
            return jsParameters;
        },
        url : submitUrl,
        validate : validateFunction,
});

we use validate as :

validate : function(value){
//do some validation rules like if value < someValue or is a negative, or character

}

The problem is I've made the submitUrl dynamic, and editableType dynamic so far. but failed on the validate part. since I have a dynamic editable type. values could either be date, text, numbers, etc. And these have different validation rules, some may require another ajax call if i need to counter check something in the database. I was trying to make a validate function dynamic by basing it to some php variable passed onto the view after render, to see what validation rule is appropriate. Is this possible or am i making sci fi? Many Thanks!

役に立ちましたか?

解決

There's no JSON encoding for functions. Use it for the data within the function body:

var successFunction = function(response) {
    <?php switch($editableType) {
          case 'date':
              echo 'return response.test(dateRegexp);';
              break;
          case 'number':
              echo 'return response.test(numberRegexp);'
              break;
          ...
          } ?>
};

他のヒント

you can use dataType: 'script;

<script>
    $(document).ready(function(){

   var submitUrl = '<?php echo Yii::app()->createAbsoluteUrl($url); ?>';
   var params = <?php echo json_encode($data); ?>;

   $.ajax({
      url : submitUrl,
      type : 'POST',
      async : false,
      data : params,
      dataType: 'script'

   });


});
</script>

https://api.jquery.com/jQuery.ajax/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top