Skip to main content

Including a file in json_encode()



Thsi does not seem to work:







$msg="Your changes have been saved successfully";



$view=include('application/view/admin/cms/_slides-current.php');



$return_array=array('success'=>true, 'msg'=>$msg, 'view'=>$view);



echo json_encode($return_array);







The array needs to be passed back to a jQuery ajax success callback. Anybody know how it should be done?





The include file is simply a HTML template. This HTML template will be inserted on to the page.





Is there any way other than using json_encode() to do what I am trying to do?







success: function(result){

alert(result.msg);



if(result.success == true)

{

$('#slides').html(result.view);

}

}




Comments

  1. include is a statement and not a function (disregard the function.include.php part). It will not return the parsed result to your $view variabe, although the file will be included just above your json_encode.

    You should use ob_start to capture the evaluated result of the view in the manner described by mihneasime:

    ob_start();
    include 'application/view/admin/cms/_slides-current.php';
    $view = ob_get_clean();

    ReplyDelete
  2. ob_start();
    include 'application/view/admin/cms/_slides-current.php';
    $view = ob_get_clean();


    Should do the trick as joar suggested.

    ReplyDelete
  3. Use fopen and friends to read files. include is for including them.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.