tags:

views:

54

answers:

3

I have this code in my HTML form:

<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>

And then in the referenced cart.php file I have:

        $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.

Example: 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.

I am using a Wordpress template file for the PHP if that makes a difference.

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

+1  A: 

To remain on the same page, leave the action attribute of the form empty eg:

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

Now, on submit, it won't go to the site's homepage.

Finally, modify your code like this:

$samples = intval($_POST['sample_']) * 50;
$cart = intval($cart) + $samples;
echo $cart;
Sarfraz
You do not need the `intval` method, it works without it.
Anthony Forloney
Oh man. I can't believe how easy that is.Thanks so much!
gamerzfuse
@Anthony Forloney, how would I do it without intval? Just remove Intval as it doesn't matter whether it is that content type or not?
gamerzfuse
Because when you type `echo $_POST['sample_']` it outputs `1`. The code you provided work just fine for me, with only the minor edit of the `action` attribute to reference my file name.
Anthony Forloney
@Anthony Forloney: all processed data via get, post, etc is sent back to the script as string, you got to convert it in case you are looking for an idea or correct answer
Sarfraz
@Craig: you are most welcome, thanks...
Sarfraz
@Sarfraz: `$number = "1"; echo $number + 11;` outputs `12` Regardless that `$number` is initialized as a String, PHP is loosely typed, it allows it. That's why you do not need the `intval`, arithmetic will still work.
Anthony Forloney
@Anthony: no man iam talking about values coming from POST and GET, they are all sent as string, you might be right at your own place though.
Sarfraz
They are sent over at strings, you are absolutely right, `is_string($_POST['sample_'])` returns `1` indicating `true`. But, they are not bound to a *specific* type, their type can change on the fly, that is why they allow you to manipulate the variables. By knowing that, `intval` isn't explicitly needed. That's the message I was trying to relay.
Anthony Forloney
@Anthony: thanks for that :)
Sarfraz
+1  A: 
<?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>

That code outputs "50" for me.

I had changed the action attribute to coincide to my PHP file. Also, I changed the name attribute of my submit button to submit for clarity reasons.

Anthony Forloney
Thanks Anthony, this code should work equally as well. As long as it's working, which it seems to be now, I am going to individually add the other three products as separate forms and see what happens.
gamerzfuse
As long as you have it working, that's all that matters. Good luck with the other ones.
Anthony Forloney
Works a charm! http://www.craighooghiem.com/linpap/order/
gamerzfuse
@Craig: Congratulations, what did it for you?
Anthony Forloney
I used Sarfraz' code 3 separate times for each button, but I modified it not to force integers as you noted in the comments.
gamerzfuse
@Craig: You could force the integers if your skeptical to loosely typed languages, such as PHP. But I am glad everything worked out for you. Let us know if there any other problems you encounter.
Anthony Forloney
A: 

Could it be that you didn't close your input tag for the hidden field? So add

</input> 

After it. If that doesn't fix it, make sure you're getting the post variable by echoing it. If that looks right and it's still not working try forcing php to convert to an int with intval (which it should do anyway). Good luck.

chamiltongt