Пример #1
0
function ttr_db_subject_editor()
{
    if (isset($_POST['action']) && $_POST['action'] == "upt") {
        TTR_db::save_subjects($_POST['sbj']);
    }
    $j_sbj = TTR_db::get_subjects();
    $sjb = json_decode($j_sbj);
    ?>
	<div class="warp">
		<h1><?php 
    _e("Subjects Editor", "ttr-db");
    ?>
</h1>
		<div id="sbj-editor">
			<input type="text" id="sbj_name_in">
			<button type="button" id="sbj_add_btn" class="btn-default"><?php 
    _e("Add", "ttr-db");
    ?>
</button>
			
			<form method="post">
				<ul id="sbj-list">
					<?php 
    foreach ($sjb as $i) {
        ?>
						<li>
							<span class="sbj"><?php 
        echo stripslashes_deep($i);
        ?>
</span> 
							<button type="button" class="del-btn">-</button> 
							<input type="hidden" name="sbj[]" value="<?php 
        echo esc_attr($i);
        ?>
">
						</li>
					<?php 
    }
    ?>
				</ul>

				<button class="btn-default" name="action" value="upt" type="submit"><?php 
    _e("Update", "ttr-db");
    ?>
</button>
			</form>
		</div>
	</div>

	<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
	<script>
		jQuery(document).ready(function ($) {
			$("#sbj-list").sortable();
			$("#sbj-list").disableSelection();

			$(".del-btn").on("click", function () {
				$($(this).parent()).remove();
			});

			$("#sbj_add_btn").on("click", function () {
				var txt = $("#sbj_name_in").val();
				var c_txt = addslashes(txt);
				$("#sbj-list").append("<li><span class='sbj'>"+txt+"</span><button type='button' class='del-btn'>-</button><input type='hidden' name='sbj[]' value=\""+c_txt+"\"></li>");

				$(".del-btn").on("click", function () {
					$($(this).parent()).remove();
				});

			});
		});

		function addslashes( str ) {
			return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
		}
	</script>
<?php 
}