Jump to content

jimfog

Members
  • Posts

    1,803
  • Joined

  • Last visited

Posts posted by jimfog

  1. I am close in finding a solution(maybe). I will let you know soon...

    It seems now that the code is OK, only some minor adjustments must be made so that when the user submits

    an invalid url the message must be "You have submit an invalid url"

  2.  

    I was going to propose the following, but I don't know where the variable url comes from...

    success:function(data){           var fields = ['address','city','municipality','url'];           var missing = [];                      for (var i=0; i < fields.length; i++) {                                   if(i<3){                     var f = fields[i];                     if (data[f] === false) {                          missing.push(f);                     }                }                if(i==3 && url!='') { //?????????? url ??????????????                     if (data.url === false) {                          missing.push('url');                     }                }           }           if (missing.length > 0) {                $('#adrserror').text('You have to fill in: ' + missing.join(', '));           }      }

    The above code(despite, at first it seems right) has some issues:

    • If for example address is not filled(and then the corresponding message appears)...and after that I choose to submit an invalid URL(at which point the server will emit FALSE)...the message that is displayed that informs the user about invalid URL...will overwrite the displayed message about the address.But that can be fixed easily I think.
    • The second and more serious problem is that if I choose to submit the URL first and then submit other data(addres,city etc)...the messages about the other data(if the server returns false) will not be displayed at all.

    I made a fiddle with the HTML here http://jsfiddle.net/fiddlehunt/wFmNv/

  3.  

     

    could you clarify what type of check you want on uri

    1.what values do you expect data.uri to have?

    2.what do you want done if a specific thing is set to data.uri?)

     

    1.data.url, when submitted to the server will pass from PHP url validate filters.

    And these will return true(if the tests gets passed) or false.

     

    2.Simply, if the server returns false, I want a message displayed saying "You did enter a correct URL", that's it

     

    And one last thing...could you make your code more jquery friendly?

    If that is possible of course?

  4. I don't understand if he is running validator functions or merely reporting the results that were obtained from the server-side validation.

    I am reporting the results obtained from server-side validation.

  5. my code is just as good other suggestions. but each suggestion has their tradeoffs. whenever I code, I usually try to put some degree of decoupling into them. its simply in my nature since I'm a software engineer. decoupling, encapsulation, composition, inheritance, etc. are all just coding principals that provide ways to make certain code more flexible and easier to add to. no code can be 100% decoupled,encapsulated, etc because at some point one variable must be able to interact with another. on the other hand it can't be 0% because its no use that all your strings will always be "Hello world" and nothing else.

     

    its like getting a new computer.

    1)other suggestions are akin simply buying a new computer:

    the process is simple, quick, and straightforward.

    but you don't have full control of what your computer starts with.

     

    2)my suggestion was more akin to building a custom computer yourself:

    you have a lot more control in what goes into your new computer.

    But the process is slightly more complex and you have to do more to get the job done.

     

    either option has their pros and cons,but both get the job done. if you have no plans to expand/change on this ajax checking (if there's only one place that will make this specific ajax call) then the suggestions most others have provided is more than sufficient (and quicker). my suggestion provides a sort of blueprint if you plan to expand on this later, making it easier to add logic.

     

    Your views are really VERY interesting,software engineering in all its glory, I avoided doing your code in the first place cause it was many lines of it.

    Instead I have concluded in this...which does not solve my problems completely,that being said...I might, after all use your code. I have not decided yet, I am still searching my options.

    Here is the code to which I am referring above, as I mentioned it has problems:

       success:function(data){                      var missing = [];          if(url=='')          {            var fields = ['address', 'city', 'municipality'];            for (var i =0; i < fields.length; i++) {                              var f = fields[i];                if (data[f] === false) {                    missing.push(f);                }            }                       if (missing.length > 0) {                $('#adrserror').text('You have to fill in: ' + missing.join(', '));            }          }          else if(url!=='')          {           var fields = ['address', 'city', 'municipality','url'];           for (var i =0; i < fields.length; i++) {                   if(i<3)                   {                        var f = fields[i];                        if (data[f] === false) {                            missing.push(f);                        }                      if (missing.length > 0) {                        $('#adrserror').text('You have to fill in: ' + missing.join(', '));                    }                        }                   if(i>2)                   {                                  if (data.url === false) {                           missing.push('url');                              }                         if (missing.length > 0) {                         $('#adrserror').text('You have to fill in: ' + missing.join(', '));                         }                    }              }           }             },
  6. what you could do is write up an inputvalidator object and just run something like input.validate() command per input element.

    	var missing = [];	function input(id_name,check){                //this check is just so you don't have to say "new input()" everytime you                 // want to make a new input class.		if (this instanceof input){			this.id =id_name;			this.field = $("input#"+this.id);                        //if a validating function was passed, set it to validate, otherwise                        // use a default validate function that does nothing			this.validate = (typeof check=="function")					?check					:function(){						//default validate does nothing and returns self						return this					};			return this;		}else{			return new input(id_name,check);		}	};		var requiredcheck = function (){		if(data[this.id] === false)			missing.push(this.id);		return this;	};	var emailcheck = function(){		//Add code to validate the url	};	var fields = [input('address',      requiredcheck),                      input('city',         requiredcheck),                       input('municipality', requiredcheck),                      input('url',          emailcheck)];	for( var inp in fields){		fields[inp].validate();	}	if (missing.length > 0) {		$('#adrserror').text('You have to fill: ' + missing.join(', '));	} 

    this way the validate() function worries about all what to do to which elements and you don't have to put in any fancy, inflexible logic like make sure to only require the first 3 elements. (what if later on you add 20 more inputs and they aren't all grouped together by how you want to work on them?). properly encapsulating the logic lets you avoid tons of if/swtich statements (your example was comparatively simple so its nowhere near as bad and it can get.

     

     

    Your code seems good(I say "seems" because I am not a very experienced js developer to say for certain "this code is bad or good" ), I just want to point out that I am not concerned about client-side validation here(I have left that to the jquery validate plugin).

     

    I am trying to write code here that checks the results sent for server-side validation-with ajax:

     $.ajax({          type: "POST",          dataType:"json",          url: "adrsajax.php",          data: {"address":address,"city":city,"municipality":municipality,"url":url},             success:function(data){                     .....code for checking..... 
  7.  

    I just started reading the "Patterns" book and it suggests this approach, especially for DOM-related arrays...

    for (var i=0, var len=fields.length ; i < len ; i++){if( i < 3){//test first three items}else{// test url}}

     

    I am not so sure about that, I am not saying it is wrong, I am just saying that maybe it is not for my case.

     

    In my case the user is presented with 4 input fields, the 3 are obligatory(address,city,municipality) and the one is optional(url).

     

    So, it is not that the user has to fill these 3 fields OR the url field(something I think the code you present tries to address).

     

    Here we have 2 scenarios:

    The user fills the 3 obligatory fields or the 3 obligatory fields AND the url.

     

    I hope that I made my point.

     

    NONETHELESS MAYBE YOUR CODE DOES THE JOB...WITH SLIGHT MODIFICATIONS.

  8. I am using this code to loop through an array:

    var missing = [];               {var fields = ['address', 'city', 'municipality','url'];                                   for (var i = 0; i < fields.length; i++) {                 var f = fields[i];                 if (data[f] === false) {                    missing.push(f);                 }             }                         if (missing.length > 0) {                $('#adrserror').text('You have to fill: ' + missing.join(', '));                      }                 }

    I want to make the loop stop at the 3rd array item...namely municipality.

    What this code does is that it checks if the user has submitted some input fields in a form(the labels of the inputs are represented by the array items ). Nonetheless the message which informs the user about not submitting data regards only the first 3 elements of the array...for the 4th element(url) I want to show a different message(a message about the validity of the supplied url).

     

    That is the reason I want to know how to loop till element 3.

  9. Sorry I did not quite understand your answer.

    I suppose you mean that if I want to stick with what the server has already as collation I should NOT use the code I list above.

     

    Is that what you mean/

  10. Whenever I make a connection to the db and perform whether this an update, a select or an insert I always set the chaset to utf8, like this:

    $connection->set_charset("utf8");

    The question is the above needed in every kind of query.Is it needed for example in a SELECT statement where we do not put in the db?

  11. I am making a cms for a booking app and the admin will be able to see the number of bookings at any moment.

    How can I do it so that the above updates continuously with ajax...like facebook newsfeed for example.

     

    So far the only ajax I have made is based upon clicking events...but this is something different I think.

  12. there is another side of the coin to consider in programming, and that is readability. These days, the benefits of trying to be as compact and trying smash things into one line aren't really necessary, so it doesn't hurt to spread things out so it's easier to read and follow what the code is doing.

    You did not say anything about my code. Is it a good practice to have the same name variable and just change its value where necessary.

  13. To stick with as few variable names as possible I do this-not unusual:

     $address=trim($_POST['address']); $address=filter_var($address, FILTER_SANITIZE_STRING);

    The value of the address variable changes of course from one line of code to another.

     

    Do you think there are negatives to the above coding scheme,using variables that way?

     

    I just do in order not to mess with many variable names.

  14. My question WHERE it is mentioned in the code below:

    $postfilter =    // set up the filters to be used with the trimmed post array    array(            'user_tasks'                        =>    array('filter' => FILTER_SANITIZE_STRING, 'flags' => !FILTER_FLAG_STRIP_LOW),    // removes tags. formatting code is encoded -- add nl2br() when displaying            'username'                            =>    array('filter' => FILTER_SANITIZE_ENCODED, 'flags' => FILTER_FLAG_STRIP_LOW),    // we are using this in the url            'mod_title'                            =>    array('filter' => FILTER_SANITIZE_ENCODED, 'flags' => FILTER_FLAG_STRIP_LOW),    // we are using this in the url        );

    The writer of the code says "....be used with the trimmed post array".

    So, do you see the variable POST anywhere above?

  15. That means $value is a string. You weren't meant to just copy the code he gave you, he said to restructure your arrays.

    An example that corresponds to the word "restructure" would help here.The obvious thing was to follow the example given...and so I did.

  16. take a look at the code...it gives me Warning: Illegal string offset 'services' :

    $serviceslist['services']=$services->servicename;
    foreach ($serviceslist as $value )
    {
    echo $value['services'].'<br>';
    }

    Ι cannot understand why this is hapenning.

  17. I am using a foreach loop to print the contents of an array to the browser:

    <?php $serviceslist=get_services($conn,$_SESSION['valid_user']);                           foreach ($serviceslist as  $value )                                    {                                     print_r($value);                                     echo '<br>';                                    }?>   

    the above array lists the contents from 2 columns(services,price)...all in all, there are 4 values and the problem is that due to the br tag the values are listed one below another.

     

    What I want is a table structure that resembles the table. I want the data printed in the browser rows after rows,

    For example:

    service price

    service price.

     

    Now it is like that:

    service

    price

    service

    price.

     

    How I will fix it? I think it s clearly an HTML problem

×
×
  • Create New...