javascript - how to insert php into jquery append via ajax? -


i have checked few posts "how insert php jquery?" don't seem find want. not sure if work have todo list in php , trying implement ajax it. in ajax upon success want appened

<a href="done.php?as=done&item=<?php echo $item['id']; ?>" class="done-button">done</a> 

but somehow stuck @ php part. have inside script

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> <script> $(document).ready(function() {     $(".submit").click(function(e){         var todotext = $("input[name='todotext']").val();         e.preventdefault();          $.ajax({             method: "post",             url: "add-ajax.php",             data: {todotext : todotext},             success: function(){                 $('p.empty').empty();                 $('input.input').val('');                 $('ul.items').append('<li>'+todotext+'<a href="#" class="done-button">done</a></li>');             }         })     }); }); 

the append here works when trying add php in it, wouldn't work.

edit (the errors get):

let's if put whole line

$('ul.items').append('<li>'+todotext+'<a href="done.php?as=done&item=<?php echo $item["id"]; ?>" class="done-button">done</a></li>'); 

the error firefox is

syntaxerror: unterminated string literal $('ul.items').append('<li>'+todotext+'<a href="done.php?as=done&item=<br /> 

in chrome get

uncaught syntaxerror: unexpected token illegal 

my html:

        <ul class="items">             <?php foreach($items $item): ?>                 <li>                     <span class="item<?php echo $item['done'] ? ' done' : '' ?>">                         <?php echo $item['todotext']; ?>                     </span>                     <?php if($item['done']): ?>                         <a href="delete.php?as=delete&item=<?php echo $item['id']; ?>" class="delete-button">delete task</a>                     <?php endif; ?>                      <?php if(!$item['done']): ?>                       <a href="done.php?as=done&item=<?php echo $item['id']; ?>" class="done-button">done</a>                     <?php endif; ?>                 </li>             <?php endforeach; ?> 

i want ajax return pretty same thing html can click done button.

my add-ajax.php

require_once 'app/init.php';  if (isset($_post['todotext'])) { $todotext = htmlentities(trim($_post['todotext']), ent_quotes);  if (!empty($todotext)) {       $addedquery = $db->prepare("             insert todoitems (todotext, user, done, created)             values (:todotext, :user, 0, now())         "); 

you can return "id" json ajax call.

in "add-ajax.php":

$returnvalue = array("id"=>$id); exit(json_encode($returnvalue)); 

in ajax call:

$.ajax({     method: "post",     url: "add-ajax.php",     datatype: "json", // type of data you're expecting server     data: {todotext : todotext} }) .done(function(data) {     $('p.empty').empty();     $('input.input').val('');     $('ul.items').append(         '<li>' + todotext +          '<a href="done.php?as=done&item=' + data.id +          '" class="done-button">done</a></li>'     ); }); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -