Well, yes and no. You can REFRESH a DIV without any issues. But, I do NOT think you can load it with PHP code. Again, that is because Javascript is CLIENT-SIDE and can not load NEW PHP from the server. I may be wrong here, but, I never got it working correctly. What is exciting is that you can load an iFrame with the PHP and move it to a DIV or any other item on the page.
It is fairly easy to use iFrames. Bascially, it is just like a DIV. You just set it up like one.
that's about it. If you do not load any HTML into it, it will never show up and is basically hidden. You can load ANY PHP page into it. And, of course, this is loaded SERVER-SIDE. So everything works just like you refreshed your page. And, the nice part is that all the textboxes you load into it from the PHP are available from Javascript. Since I deleted my code for this, I will have to make some new code up for you. But, it was quite easy to do... AND, since the code is executed SERVER-SIDE, no user can see it and it makes it secure from the world. You need that in a POS!!!
First, change your form around. You only need one big form. Inside the body from top to bottom. Since every button on the page will call Javascript, the code is on the page. This means that you will be using either “ONCLICK=” , “ONCHANGE=” or even “ONBLUR=”. So, check out these and make sure you understand the differences. They are subtle, but, important in HTML/Javascript use. Also, remember any person can look at your Javascript, so do any important code inside PHP if possible. You do not want your code out in the public as hackers will try to get in.
Next, the iFrame… Easy one, just like DIV’s. For this disscussion, we will keep it simple. Here is one for testing…
<iframe ID="hidden_frame" name="hidden_frame" src="" frameborder="0"></iframe>
(Note: the border must be set to zero or you will see a small outline!)
Now to “load” this iframe, you create a new PHP page to load. This second file is SERVER-SIDE, so it is standard PHP. For instance, it will pull your needed data from the database and store it in HIDDEN HTML form fields. (Hidden form fields will not show on the page and you can change them constantly as you need them. So, using your own code, here is a sample file that should load the needed data. Note in the Javascript we will load the variables that are passed to the PHP page as arguments. (SO, to load data based on cart-id, it would be something like “load_data.php?cartid=39”…
load_data.php
[php]
<?PHP
// Query member data from the database and ready it for display
//OLD-LINE: $sql = mysql_query("SELECT * FROM cart where cart_id = ".$cid."");
//NEW-LINE:
$sql = mysql_query("SELECT * FROM cart where cart_id = " . $_GET('cartid');
// previous line pulls cartid from argument passed from javascript in next section
while($row4 = mysql_fetch_array($sql)){
// the next few lines are the same, except we will place this data into hidden text fields
$product = $row4["product123"];
$price = $row4["price"];
$id = $row4["id"];
$qty = $row4["quantity"];
$extended_price = $price * $qty;
$total = $extended_price + $total;
?>
[/php]
Notes: As you seen, I altered your price1 as it did not explain itself. Use variable names that make sense. When you come back in 3 years, what was price1? Note that the calculations are completed and then all items are placed into hidden input fields. These fields can be read from Javascript and used for calcs or copied to areas on the page that are not hidden from view. Simple! Oh, you will need to add your connection string to the top of this page so the code knows where the data is coming from.
Now, at this point you have an iFrame, basically blank and a second file that pulls data from your database using the cartid which is passed to it as an argument. You will have a button or whatever that executes the PHP file, loads it into the hidden iFrame and copies the needed data to visible fields. Now, how to execute that file and get it into the hidden iFrame:
Button that calls the new code: (Javascript Function will be call update_data for testing)
<input type="button" name="Load Stuff" value="Load Stuff!"onclick="update_data();"/>
Still in first main form:
<script etc....>
function update_data(){
// set up a point to the iFrame
var ifrm=document.getElementById('hidden_frame');
ifrm.setAttribute('src', 'load_data.php?cartid=39');
</script>
NOTE: I forced in cartid=39 as a test, make the number a valid one and this will be changed in your program to select whatever cart you wanted…
Now, all at this point… A button fires off to the javascript code to load a new page into the hidden iFrame and the PHP on the new page creates a few hidden fields with values filled in from the database. Just have to move these where you want them and they will appear like magic dynamically…
Move data to “live” text areas onscreen. (This code would be in the previous function…)
document.getElementById('product_txtbox').value = document.getElementById('product').value;
document.getElementById('price_txtbox').value = document.getElementById('price').value;
document.getElementById('total_txtbox').value = document.getElementById('total').value;
// the product_txtbox would be where you want the product to be place on your page, etc...
Well that will truly “DYNAMICALLY” transfer data for you. The other possible choice which would also work is that you could create a visible iFrame and load a PHP page into it that displays the data dynamically. This is simpler, no Javascript calls or moving data around. But, I do not do it this way, because it is very nice to have the PHP data (DB data) on the page to use in the javascript code. You can build tables or arrays in javascript to use this data throughout multiple PHP calls, so you can keep previous data so the user can do an UNDO or whatever you want. This is your choice. Both of these processes makes the database available to Javascript without anyone seeing the underlying PHP code which fetches it. A bit of security!
Good luck, hope this helps…