Form submission

submission code that doesn’t refresh page:

[PHP]<?php

// Query member data from the database and ready it for display

$sql = mysql_query(“SELECT * FROM cart where cart_id = “.$cid.””);

while($row = mysql_fetch_array($sql)){

$id =$row[“id”];

?>

<script type="text/javascript">

$(document).ready(function(){

    $(".update<?php echo $id; ?>").validate({

        debug: false,

        submitHandler: function(form) {

            // do other stuff for a valid form

            $.post('update.php', $(".update<?php echo $id;?>").serialize(), function(data) {

                $("#price").load("index.php #price");$("#total").load("index.php #total");

            });

        }

    });

});

</script>

<?php

}

?>[/PHP]

form:

[CODE]<?php

// Query member data from the database and ready it for display

$sql = mysql_query(“SELECT * FROM cart where cart_id = “.$cid.””);

while($row = mysql_fetch_array($sql)){

$product = $row[“product123”];

$price1 = $row[“price”];

$id = $row[“id”];

$qty = $row[“quantity”];

if($product !=""){

?>

Quantity

$

[/CODE]

so my problem is that only everyother time does the page actually not refresh…so one time it does and database doesn’t update…second time it doesn’t refresh and it updates(this is what I want). why is it doing this? is it because it is inside a div that gets refreshed? if this is the problem…how do I fix it?

Well, not sure without putting on a server and testing, but, I did notice one error.
When your code pulls from the database and sets variables to $row[], etc, it has
a “while” and it is opened, but not closed. A “{” , but, no “}”.

And, what does this do???: (Or was it your further code here, type thing?
if($product !=""){

Perhaps one of these is the problem…

I am pretty sure it closes later on. the other one is saying where products does not equal a blank. But I am pretty sure is it has to do with syntax and where my divs open and close…please help

Well, in my opinion, the code is not really standard.

First, you shouldn’t place a form inside a PHP WHILE.
This could make numerous forms with the same name causing havoc.
Your second post, the “form” does not end the WHILE. It is left open.
If you must put the form inside that while, add <?;?> after the

Maybe this will explain it better, your WHILE is outside which means it starts and should finish.
It does in the first part, but, the second is left open so the page does not complete.
(That is what I was talking about in the previous post.) Good luck…

sorry, I was thinking this was a different post. anyways, here is the full code:
[php]


<?php
// Query member data from the database and ready it for display
$sql = mysql_query(“SELECT * FROM cart where cart_id = “.$cid.””);
while($row4 = mysql_fetch_array($sql)){
$product = $row4[“product123”];
$price1 = $row4[“price”];
$id = $row4[“id”];
$qty = $row4[“quantity”];
if($product !=""){

?>

<?php echo $product; ?>
Quantity $
<input type="submit" name="update" id="update" value="update" />

<?php
$price1 = $price1 * $qty;

$total = $price1 + $total;
?>

<?php }} ?>
  <br />
[/php]

Let me try to explain my reasoning. I am building a POS system and I need the update button to submit to my database without the page refreshing. I need multiple forms so that it updates properly, this is teh only way i have found actually works. the only problem as I said, is that the page refreshes every other time. please help:)

Well, first, you can NOT dynamically update PHP from a database. PHP is SERVER-SIDE only. With your code you would be posting to yourself. The screen would refresh and not not stay static. You could do it with Javascript, but would not be secure. So, you would never create a POS this way.

To explain, PHP is SERVER-SIDE only (can connect to MySQL and nobody sees code, so it is secure)

HTML is CLIENT-SIDE as is Javascript. (Client-Side meaning runs inside your Browers)

SO, your PHP webpage loads at the server and pre-processes, then combines with your webpage’s HTML and sends the “combined” PHP/HTML code to your browser. Your browser renders the page using HTML codes, Javascript codes and CSS stylings, then shows you the result. If the HTML contains a “FORM” it is NOT shown until AFTER PHP has been processed. So, when yo POST the form, it has to go back to the server to reload the MySQL data thru PHP SERVER-SIDE. Then, it posts the results back to the browser. So to be secure, you MUST use your SQL code on the server where nobody can look and that means PHP.

BUT, now that I have knocked down your idea. There are a couple ways around this. You can use a hidden “iFrame” and load a PHP file into it processing the dynamic MySQL which would load whatever you need to pull from the database in a hidden form inside the iFrame. Then, you can use Javascript to access the data and the page never flickers and stays static because you never change the entire page, just one small hidden iFrame.

So, I am sure I have not given you what you wanted to hear. The every-other-time posting is due to the page posting to itself and then having no return possible, so it does a real refresh and then starts over again… If you add a few pictures to the page, you will see them flicker due to this odd behavor. So, if you look at your code resulting to your browser, by using RIGHT-CLICK, VIEW-SOURCE, you will see no (NONE) PHP code in it. So, a page can not execute PHP without pulling it from the server.

Let me know if you understand all this mess…

but thie thing is, I have one div refreshing, wouldn’t this be the samething as an iframe?

I do like your idea of the iframe. could you help me with the javascript code that would doit?

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…

so like:

[code][/code]

[php]<?PHP
include_once(“connect.php”);
session_start();
$sql = mysql_query(“SELECT * FROM cart”);
while($row = mysql_fetch_array($sql)){
$cid = $row[“cart_id”];
$_SESSION[‘cart_id’] = $cid;
}
// 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 = ‘1’”);
// 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 } ?>[/php]

here is entire code for the original page(I plan to modify stuff in future):[php]<?php
include_once(“connect.php”);
session_start();
$sql = mysql_query(“SELECT * FROM cart”);
while($row = mysql_fetch_array($sql)){
$cid = $row[“cart_id”];
$_SESSION[‘cart_id’] = $cid;
}
echo $cid;
?>

Untitled Document <?php // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM cart where cart_id = ".$cid.""); while($row = mysql_fetch_array($sql)){ $id =$row["id"]; ?>
<script type="text/javascript">
$(document).ready(function(){
	$(".delete<?php echo $id; ?>").validate({
		debug: false,
		
		submitHandler: function(form) {
			// do other stuff for a valid form
			$.post('delete.php', $(".delete<?php echo $id;?>").serialize(), function(data) {
				$("#price").load("index.php #price");
				$("#total").load("index.php #total");
			});
		}
	});
});
</script>
<?php

}
?>
<?php
// Query member data from the database and ready it for display
$sql = mysql_query(“SELECT * FROM products”);
while($row = mysql_fetch_array($sql)){
$id =$row[“id”];
?>

<?php } ?>
<link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css" />
<?php // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM cart where cart_id = ".$cid.""); while($row4 = mysql_fetch_array($sql)){ $product = $row4["product123"]; $price1 = $row4["price"]; $id = $row4["id"]; $qty = $row4["quantity"]; if($product !=""){

?>

<?php echo $product; ?>
Quantity $
<input type="button" name="Load Stuff" value="Load Stuff!"onclick="update_data();"/>

<?php
$price1 = $price1 * $qty;

$total = $price1 + $total;
?>

<?php }} ?>
  <br />
<?php // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM products"); while($row5 = mysql_fetch_array($sql)){ $product = $row5["product"]; $id =$row5["id"]; $price =$row5["price"];

?>

<?php } ?>
<input name="cart_id" type="hidden" value="<?php echo $cid; ?>" /><input type="submit" name="empty" class="submit" value="empty cart" style="background-color:lightgreen; height:50px; width:100px;left:0px;"">   </form>   

<?php if ($total == ""){$total = "0";} ?> <?php echo "$".$total.""; ?> <?php // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM cart where cart_id = ".$_SESSION['cart_id']." && product123 !=''"); $productnumber = 1; // set a counter for the products while($row = mysql_fetch_array($sql)){ $product = $row["product123"]; $price1 = $row["price"]; $id = $row["product_id"]; $qty = $row["quantity"];

        $month = date("F Y");
        $day = date("d");
        $year = date("Y");
        $date = date("Y-m-d");
        ?>

        <input name="product<?php echo $productnumber; ?>" type="hidden" value="<?php echo $product; ?>" />
        <input name="academy<?php echo $productnumber; ?>" type="hidden" value="<?php echo $academy; ?>" />
        <input name="month<?php echo $productnumber; ?>" type="hidden" value="<?php echo $month; ?>" />
        <input name="day<?php echo $productnumber; ?>" type="hidden" value="<?php echo $day; ?>" />
        <input name="year<?php echo $productnumber; ?>" type="hidden" value="<?php echo $year; ?>" />
        <input name="date<?php echo $productnumber; ?>" type="hidden" value="<?php echo $date; ?>" />
        <input name="price1<?php echo $productnumber; ?>" type="hidden" value="<?php echo $price1; ?>" />
        <input name="id<?php echo $productnumber; ?>" type="hidden" value="<?php echo $id; ?>" />
        <input name="qty<?php echo $productnumber; ?>" type="hidden" value="<?php echo $qty; ?>" />
        <input name="total" type="hidden" value="<?php echo $total; ?>" />

        <?php
        $productnumber ++; //increase the product number counter
    }
    ?>
    <?php if ($total == " "){$total = "0";} ?><input type="submit" name="pay" id="pay" class="pay1" value="" />
</p>

$x = 1; // set $x
for ($i=1; $i<=100; $i += 9)
{ $start = array ($i);} //columns will be started at each of these numbers
for ($n=0; $n<=100; $n += 9)
{
$end = array($n);}

<?php //start the table, and the row. // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM products"); while($row = mysql_fetch_array($sql)){ $product = $row["product"]; $id =$row["id"]; $price =$row["price"]; ?> <?php echo $product; ?> <?php // start a column } ?>
    <?php $sql = mysql_query("SELECT * FROM categories"); while($row1 = mysql_fetch_array($sql)){ $category = $row1["category"]; $sub =$row1["subcategory"]; ?>
  • <?php echo $category; ?>
  • <?php // set layout vars $x = 1; // set $x $start = array ('1', '10', '19', '28', '37', '46', '55', '64', '73', '82', '91', '100', '109', '118', '127', '135'); //columns will be started at each of these numbers $end = array( '9', '18', '27', '36', '45', '54', '63', '72', '81', '90', '99','108','117', '126' ); // set numbers to end columns at ?>
        <?php echo '<table border="1px" cellpadding="0px" cellspacing="0px" ><tr height=\'50px\'>'; //start the table, and the row.
    

    // Query member data from the database and ready it for display
    $sql = mysql_query(“SELECT * FROM products where category=’$category’”);
    while($row2 = mysql_fetch_array($sql)){
    $product = $row2[“product”];
    $id =$row2[“id”];
    $price =$row2[“price”];
    if (in_array($x, $start)) { // if x equals a number in the start array
    echo ‘

    ’; // start a column
    } ?>
    <?php $sql = mysql_query("SELECT * FROM categories");
    

    while($row3 = mysql_fetch_array($sql)){
    $category = $row3[“category”];
    $sub =$row3[“subcategory”];
    ?>

    <?php } ?>
    <div id="products">
      <form action="" method="POST" name="myform<?php echo $id; ?>" class="myform<?php echo $id; ?>">
        <input type="hidden" name="hiddenField" class="hiddenField" value="<?php echo $product; ?>" />
        <input type="hidden" name="hiddenField2" class="hiddenField2" value="<?php echo $id; ?>" />
        <input type="hidden" name="hiddenField1" class="hiddenField1" value="<?php echo $price; ?>" />
      <input type="submit" name="submit" class="submit" value="<?php echo $product; ?>" style="background-color:lightgreen; height:50px; padding: 0px; margin:0px; width:100px;">  </form>
    </div>
    
    <?php if (!in_array($x, $end)){ // if $x equals anything OTHER than 9, 18, etc - put in a line break } else { // else if it DOES equal 9, 18 ,etc end the column echo ''; } $x ++; // increase x to keep count } // while loop ends //now outside of the loop, $x = $x - 1; //subtract the last $X++ so you know exactly how many buttons were added if (!in_array($x, $end)){ // if $x equals anything OTHER than 9, 18, etc - end the column as it wouldn't have ended itself echo ''; } echo ''; // close the row and table ?><?php

    }
    ?>

      </div>  
    
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    
[/php]

where does the code to update the database go at?

The PHP code for pulling your data from the database is in a separate file. It is loaded into the iFrame by the Javascript. The separate file is a .PHP that has database code and echo’s just the text boxes. So, it just loads the data you requested and the Javascript loads this page into the hidden iFrame. One pulled into the current page from the separate php file, the data is now available. I will try to get you a better description tomorrow.

Basically, you can NOT update a PHP page to the current live HTML page. BUT, you can if you load it into an iframe. I will set up a demo for you so it makes sense. (But, bedtime calls…)

just an fyi, that first code whas javascript. the second was the load_data. and the third was the page that displays everyting.

any help?

Have yo9u figured anything out yet?

ok, I figured out where my problem with my original code is:) The problem is that the correct javascript number for the form is not yet created. ie it uses javascript from .update(dynamic number goes here). I need the dynamic number to update when the div refreshes…it makes the whole page refresh in order to do it. I don’t think this probably makes a lot of sense…but maybe it does to someone…

I think what I need is a way to refresh an iframe without refreshing the entire page. any help?

Searls03,
Sorry, I was out of town for a few days. I had sent the code and explained each part in detail in an earlier post. Reread our posts and look at the notes which shows the iFrame layout (one line), a form (one form covering entire page), one button to update the iFrame and one PHP file that would load your new data into the iFrame. It all works together. If you can’t get it working, I can post some entire sample pages for you.
Let me know…

I couldn’t get it working. Some sample pages would be nice:)

Why does the page refresh when I use this code:
[php]<?php
include_once(“connect.php”);
session_start();
$sql = mysql_query(“SELECT * FROM cart”);
while($row = mysql_fetch_array($sql)){
$cid = $row[“cart_id”];
$_SESSION[‘cart_id’] = $cid;
}
echo $cid;
?>

Untitled Document
<script type="text/javascript">
$(document).ready(function(){
	$(".product").validate({
		debug: false,
		
		submitHandler: function(form) {
			// do other stuff for a valid form
			$.post('process1.php', $(".product").serialize(), function(data) {
				$("#price").load("index.php #price");
				$("#total").load("index.php #total");
			});
		}
	});
});
</script>
<?php // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM cart where cart_id = ".$cid.""); while($row = mysql_fetch_array($sql)){ $id =$row["id"]; ?>
<script type="text/javascript">
$(document).ready(function(){
	$(".delete<?php echo $id; ?>").validate({
		debug: false,
		
		submitHandler: function(form) {
			// do other stuff for a valid form
			$.post('delete.php', $(".delete<?php echo $id;?>").serialize(), function(data) {
				$("#price").load("index.php #price");
				$("#total").load("index.php #total");
			});
		}
	});
});
</script>
<?php

}
?>
<?php
// Query member data from the database and ready it for display
$sql = mysql_query(“SELECT * FROM products”);
while($row = mysql_fetch_array($sql)){
$id =$row[“id”];
?>

<?php } ?> /* RESET */ div, span, h1, h2, h3, h4, h5, h6, p, blockquote, a, font, img, dl, dt, dd, ol, ul, li, legend, table, tbody, tr, th, td {margin:0px;padding:0px;border:0;outline:0;font-weight:inherit;font-style:inherit;font-size:100%;font-family:inherit;list-style:none;} a img {border: none;} ol li {list-style: decimal outside;} fieldset {border:0;padding:0;} div#container { width: 780px; margin: 0 auto; padding: 1em 0; } p { margin: 1em 0; max-width: 700px; } h1 + p { margin-top: 0; } h1, h2 { font-family: Georgia, Times, serif; } h1 { font-size: 2em; margin-bottom: .75em; } h2 { font-size: 1.5em; margin: 2.5em 0 .5em; border-bottom: 1px solid #999; padding-bottom: 5px; } h3 { font-weight: bold; } ul li { list-style: disc; margin-left: 1em; } ol li { margin-left: 1.25em; } div.side-by-side { width: 100%; margin-bottom: 1em; } div.side-by-side > div { float: left; width: 50%; } div.side-by-side > div > em { margin-bottom: 10px; display: block; } a { color: orange; text-decoration: underline; } .faqs em { display: block; } .clearfix:after { content: "\0020"; display: block; height: 0; clear: both; overflow: hidden; visibility: hidden; } footer { margin-top: 2em; border-top: 1px solid #666; padding-top: 5px; }
<link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css" />

 

 

Multiple Select with Groups <?php // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM products"); while($row5 = mysql_fetch_array($sql)){ $product = $row5["product"]; $id =$row5["id"]; $price =$row5["price"];

?>

      <option value="<?php echo $product; ?>"><?php echo $product; ?></option><?php

}
?>

    </select>
  </div>
</div>

<h2>&nbsp;</h2>
[/php]

the specific form is at bottom and the submit code is up towards top. this is a javascript don’t refresh page code…this one will work for what I want…

Sponsor our Newsletter | Privacy Policy | Terms of Service