PHP Form "Shopping Cart" için değişkenleri ekleme

3 Cevap php

Ben HTML biçiminde bu kodu vardır:

<form action="cart.php" method="post">

<input type="hidden" value="1" name="sample_" id="sample_">
Sample Bottle  <br /><input type="submit" value="Add to Cart" name="sample"><br />

</form>


<h1>Cart Total: $<?php echo $cart; ?></h1>

Ve sonra başvurulan cart.php dosyasında I vardır:

        $cart = 0;
    $samples = ($_POST['sample_']) * 50;
    $cart = $cart + $samples;

    echo $cart;'

Unfortunately, this code isn't doing what I want it to and I need some help. Ideally, I simply want the code to add "50" to the Cart, which starts at 0, when the Add to Cart button is pressed.

Örnek: http://www.craighooghiem.com/linpap/order

Can someone tell me how I can do this in a much simpler way? Apparently I can use <?=$_SERVER['PHP_SELF'];?> somehow to perform this function without leaving the file, but everytime I attempt to do that it takes be back to the website's homepage.

Bir fark yaparsa ben PHP için bir Wordpress şablon dosyası kullanıyorum.

EDIT: I have quickly learned that I am not all that familiar with PHP at all, I barely know the ropes at this point.

3 Cevap

, Aynı sayfada kalır ve form boş örneğin bir action niteliğini bırakmak için:

<form method="post" action="">

Şimdi, teslim tarihinde, bu sitenin ana sayfasına gitmek olmaz.

Son olarak, bu gibi kodunuzu değiştirin:

$samples = intval($_POST['sample_']) * 50;
$cart = intval($cart) + $samples;
echo $cart;
<?php
$cart = 0;
 // good practice to see if the submit button has been pressed.
 if (isset($_POST['submit'])) {
 // no need to explicitly cast to an int, PHP is loosely typed.
  $samples = ($_POST['sample_']) * 50;
  $cart = $cart + $samples;
 }
?>
<form action="echo.php" method="post">
<input type="hidden" value="1" name="sample_" id="sample_">
Sample Bottle  <br />
<input type="submit" value="Add to Cart" name="submit"><br />
</form>
<h1>Cart Total: $<?php echo $cart; ?></h1>

Benim için bu kod çıkışları "50".

Ben PHP dosyasına denk action niteliği değişmişti. Ayrıca, açıklık nedeniyle submit benim göndermek düğmesinin name özniteliği değiştirdi.

Eğer gizli alan için giriş etiketi kapatın vermedi olabilir mi? Yani eklemek

</input> 

Ondan sonra. Işte bu sorunu çözmezse, bunu yankılanan tarafından yazılan değişkeni aldığınızdan emin olun. Bu doğru görünüyor ve yine (yine yaparım gereken) intval ile bir int dönüştürmek için php zorlayarak denemek çalışma değilse. İyi şanslar.