Mixing PHP, Javascript, Ajax in a single piece of code -


is bad practice write code consists of php, javascript , ajax in same piece of code? example:

<?php  $randnum = rand(1,10)   if ($randnum <= 5) {     echo "enemy got jump on you! <script> enemyturn() </script>";   }else {     echo "you got jump! <script> playerturn() </script>";   } ?> 

enemyturn(), playerturn() javascript functions contain jquery , ajax.

it is, bad practice wash dishes in bathtub: can still it, pain have shower when need it.

concerns should stay decoupled possible, example be:

php

<?php if ($randnum <= 5): ?>  <div data-function="enemyturn">enemy got jump on you!<div>  <?php else: ?>  <div data-function="playerturn">you got jump!<div>  <?php endif; ?> 

js

$(function(){    var yourfunctions = {      enemyturn : function(){ // stuff },     playerturn : function(){ // stuff }    };    $('[data-function]').each(function(){      yourfunctions[$(this).data('function')].call(this);    })  }); 

this way js code decoupled html. nevertheless smell can logic of game client side, using server login / data save purposes.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -