i am trying to clean up my code.. is there a way to run this code with Ajax or jquery rather than placing the code in the html page itself..thanks in advance
<?php $acct = new acct; echo $acct->accountProfile("accountphone"); ?>
You can have js execute an ajax function (via $.post
) on a load event like $( document ).ready(function() {//code}
, using something like $("#div").html(data);
to populate data into a container.
That said, I don't think that's necessarily more organized than putting it into your HTML code itself. You don't want to process LOGIC too much in your view, but the nature of web development is that it is "messy" (IMO) and that you have to often just insert PHP all over the place, to do singular functions/echos as you seem to be doing.
Yes, you could put all your PHP code in a separated PHP file, eg.
PrintAccount.php
$acct = new acct;
echo $acct->accountProfile("accountphone");
And at your HTML , via AJAX (I'm using ajax with jquery in this example) you "print" php file response in your html
index.html
<div id="accountphone"></div>
<script>
$.ajax({
type: "POST",
url: "PrintAccount.php",
success: function(data){
$("#accountphone").html(data);
}
});
</script>
Now, my personal opinion: This is good? depends, if you have a lot of PHP code is actually good doing this, you could improve it as real services like REST, it'll be awesome for clean code at HTML files. You have also another option, using MVC, so most of code will be written at Controller, and just a few at View. Now, about your given example, with just a small amount of PHP code this is totally unnecessary, you are complicating too much a simple task.