php - jQuery ajax post does not always work with long texts -
for reason, long posts not inserted database, when post includes long text (i.e. more 200 words or so). however, issue pretty vague since of these long texts inserted in database (for instance large lorum ipsum text inserted without problem). first thought had punctuation not issue. also, text-breaks etc. not cause of issue. furthermore, checked data structure of comment part (which assigned textarea shown below) in database , there saw has been given long text type. character restrictions aren't problem either. thus, strange thing occurs "some" long texts. short texts 200 words no problem , inserted perfectly.
my code follows. first html part:
<div class="new-com-bt"> <span>schrijf hier uw bericht ....</span> </div> <div class="new-com-cnt"> <input type="text" id="name-com" name="name-com" value="" placeholder="uw naam" /> <input type="text" id="mail-com" name="mail-com" value="" placeholder="uw e-mail adres" /> <input type="text" id="code-com" name="code-com" value="" placeholder="viercijferige code..." /> <textarea class="the-new-com"></textarea> <span class="rating2">beoordeel ons: <input type="radio" class="rating-input" id="rating-input-2-1" name="example" value="5"> <label for="rating-input-2-1" class="rating-star"></label> <input type="radio" class="rating-input" id="rating-input-2-2" name="example" value="4"> <label for="rating-input-2-2" class="rating-star"></label> <input type="radio" class="rating-input" id="rating-input-2-3" name="example" value="3"> <label for="rating-input-2-3" class="rating-star"></label> <input type="radio" class="rating-input" id="rating-input-2-4" name="example" value="2"> <label for="rating-input-2-4" class="rating-star"></label> <input type="radio" class="rating-input" id="rating-input-2-5" name="example" value="1"> <label for="rating-input-2-5" class="rating-star"></label> </span> <div class="bt-add-com">plaats bericht</div> <div class="bt-cancel-com">annuleer</div> </div> now jquery part:
<script type="text/javascript"> $(function(){ //alert(event.timestamp); $('.new-com-bt').click(function(event){ $(this).hide(); $('.new-com-cnt').show(); $('#name-com').focus(); }); /* when start writing comment activate "add" button */ $('.the-new-com').bind('input propertychange', function() { $(".bt-add-com").css({opacity:0.6}); var checklength = $(this).val().length; if(checklength){ $(".bt-add-com").css({opacity:1}); } }); /* on clic on cancel button */ $('.bt-cancel-com').click(function(){ $('.the-new-com').val(''); $('.new-com-cnt').fadeout('fast', function(){ $('.new-com-bt').fadein('fast'); }); }); // on post comment click $('.bt-add-com').click(function(){ var thecom = $('.the-new-com'); var thename = $('#name-com'); var themail = $('#mail-com'); var thecode = $('#code-com'); var therating = $('input[name=example]:checked'); if( !thecom.val()){ alert('u moet een bericht schrijven'); }else if(thecode.val() != '7624'){ alert('vul de viercijferige code in die u heeft gekregen tijdens de ceremonie') }else{ $.ajax({ type: "post", url: "ajax/add-comment.php", data: 'act=add-com&id_post='+<?php echo $id_post; ?>+'&name='+thename.val()+'&email='+themail.val()+'&comment='+thecom.val()+'&rating='+therating.val(), success: function(html){ thecom.val(''); themail.val(''); thename.val(''); therating.val(''); settimeout(function(){ //fade $('.new-com-cnt').html("dank u wel voor uw bericht. deze zal zo spoedig mogelijk op de site verschijnen!"); }, 0); } }); } }); }); this add-comment.php script:
<?php extract($_post); if($_post['act'] == 'add-com'): $name = htmlentities($name); $email = htmlentities($email); $comment = htmlentities($comment); $rating = htmlentities($rating); include('../config.php'); // gravatar image // https://fr.gravatar.com/site/implement/images/php/ $default = "mm"; $size = 35; $grav_url = "http://www.gravatar.com/avatar/" . md5( strtolower( trim( $email ) ) ) . "?d=" . $default . "&s=" . $size; if(strlen($name) <= '1'){ $name = 'guest';} //insert comment in database mysql_query("insert comments (name, email, comment, id_post, rating, display)values( '$name', '$email', '$comment', '$id_post', '$rating', 'nee')"); if(!mysql_errno()){ ?> <div class="cmt-cnt"> <img src="<?php echo $grav_url; ?>" alt="" /> <div class="thecom"> <h5><?php echo $name; ?></h5><span data-utime="1371248446" class="com-dt"><?php echo date('d-m-y h:i'); ?></span><span class="com-dt-rating"><span class="rating"> <input type="radio" class="rating-input" id="rating-input-1-1" value="5" disabled="disabled" <?php echo ($rating=='5')?'checked':'' ?> /> <label for="rating-input-1-1" class="rating-star"></label> <input type="radio" class="rating-input" id="rating-input-1-2" value="4" disabled="disabled" <?php echo ($rating=='4')?'checked':'' ?> /> <label for="rating-input-1-2" class="rating-star"></label> <input type="radio" class="rating-input" id="rating-input-1-3" value="3" disabled="disabled" <?php echo ($rating=='3')?'checked':'' ?> /> <label for="rating-input-1-3" class="rating-star"></label> <input type="radio" class="rating-input" id="rating-input-1-4" value="2" disabled="disabled" <?php echo ($rating=='2')?'checked':'' ?> /> <label for="rating-input-1-4" class="rating-star"></label> <input type="radio" class="rating-input" id="rating-input-1-5" value="1" disabled="disabled" <?php echo ($rating=='1')?'checked':'' ?> /> <label for="rating-input-1-5" class="rating-star"></label> </span></span> <br/> <p> <?php echo $comment; ?> </p> </div> </div><!-- end "cmt-cnt" --> <?php } ?> an example of long text inserted:
he technique described in paper model , formalize requirements called flags. flags offers possibility model , formalize fuzzy goals, besides crisp goals. distinctive these fuzzy goals cannot concretely achieved, whereas crisp goals can. instead, achievability of these goals vague , therefore cannot either satisfied or not, can satisfied extent. hink of saving fuel while driving car goal. not clear whether goal concretely achieved. hat is, fuel consumption can small, doesn’t state whether goal clear-cut achieved or not. instead, satisfied extent, namely when car uses small amount of fuel. same way of thinking can applied physiotherapy modelling these kind of goals evaluate speed , correctness of movements realized person, can done when flags goal model elements formalized.
a few steps need followed in order apply flags. hese steps can found in appendix in table 1 , further elaborated in chapter describing example in application described learn robot how move human being. example concerns robot application, , describes situation, flags meta-model pasquale et al. (2013) extended example described in chapter. extended flags meta-model can consulted in figure 2 included in appendix a. furthermore, example assumed requirements identified.
he flags meta-model consists of several classes describe composition of flags goal model @ abstract level. explains either crisp goals (clear goals) or fuzzy goals (achievable specific extent) can modeled. besides, goals can influence each other (influenced relationship) , can decomposed sub goals (decomposed relationship). achieving these goals important both person , robot. moreover, domain assumptions (conditions) , operations adopted in flags meta-model , person uses controller monitor or movements robot can respond these movement
an example of text not inserted:
mijn iboga ervaring bij iboga farm zeer mooie en rustige locatie en een heerlijke sfeer, goed verzorgd. geen haastigheid maar alles in een relaxte flow. ik wist niet wat ik moest verwachten maar de jongens stellen je goed gerust, goede uitleg en er word goed rekening gehouden met je gezondheid toestand en eet gewoontes. alles aanwezig wat men nodig heeft. eten,drinken,douche gelegenheid,slaap plek etc mooie ceremonie voorafgaand. eerst krijg je een test dosis om vast te kunnen stellen of men allergisch voor de substantie. iets wat veel andere niet doen. als het eenmaal werkt word de sessie begonnen terwijl je boven op een comfortabel bed ligt, met een spiritueel muziekje op de achtergrond. toen het eenmaal goed werkte ervaarde ik een soort van ijl achtige droomstaat trip waarbij er visioenen tevoorschijn komen. ik persoonlijk heb vorige levens en voorouders gezien en gevoeld. ondertussen word er zeer goed op je gelet en je word uitstekend verzorgd, je zal niks tekort komen gedurende de iboga reis. als men het toilet nodig heeft wel om assistentie vragen want het lopen gaat wat moeilijk. tijdens de reis zal het voor ieder andere anders zijn want het zijn allemaal persoonlijke kwesties die verwerkt worden. ondertussen de sessie merk je al dat er veel dwars liggende emotie's en gewoontes verdwijnen uit je systeem, en
example 1 has no single quote. example 2 has single quote, db throwing 1064 error. when using mysql_errno() should error if there one. @ closing do
} else { echo mysql_errno() . ": " . mysql_error(); } the error occurs @ emotie's because ' closes sql string , remaining text sql doesn't know with. how sql injections work. ' maliciously used , sql commands passed in. using mysql_ functions have use escaping functions prevent this. should update mysqli_ or pdo though. can use parameterized queries driver handles quoting.
so assign variables should be:
$name = mysql_real_escape_string(htmlentities($name); $email = mysql_real_escape_string(htmlentities($email)); $comment = mysql_real_escape_string(htmlentities($comment)); $rating = mysql_real_escape_string(htmlentities($rating)); also note warnings on manual's page isn't best way best approach mysql_ driver. http://php.net/manual/en/function.mysql-error.php
for more on topic see:
how can prevent sql injection in php?
https://www.owasp.org/index.php/sql_injection_prevention_cheat_sheet
Comments
Post a Comment