Thread: PHP Problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    PHP Problem


    Hello, I have been having issue in my School project.

    I want to increment a variable when the button "Aumentar" is pressed.

    Though right now it is incrementing both Val and Val2
    when I only want the first button to increment Var1 and the second button to only increment Var2.

    Code:
    <?php
    
    //1
    $val = 0;
    if (isset($_POST["val"])) {
       $val = $_POST["val"];
       $val=$val+1;
    }
    
    //2
    $val2 = 0;
    if (isset($_POST["val2"])) {
       $val2 = $_POST["val2"];
       $val2=$val2+1;
    }
    ?>
    
    <html>
    <body>
     <body bgcolor="#00CCCC">
    <?php
    $horas=date("H:i:s");
    $data=date("M/d/Y");
    echo $horas;
    echo "<br>";
    echo $data;
    ?> 
     <marquee> <i><h1>Bem Vindo(a) :)</h1></i></marquee>
     
      <marquee> <i><h2>Escolha os seus produtos desportivos </h2></i></marquee>
      
     <?php
    echo "<i><h3>Sobre a loja:</h3></i>";
    echo "A loja é de estilo desportista mas simples, tem como  função agradar 
    todos os clientes que a frequentam online  e a utilizam para fazer algumas compras que possam ser úteis para o seu bem-estar.";
    ?>
      <p>
      <i> Para ver quais os produtos visite a galeria </i><br>
    <a href="galeria.html">
            <input type="button" value="Galeria de Produtos">
    </a> </p>
    <center>
    <table border="1" cellpadding="1">
    <tr>
    <form action="nomediferente.php" method="POST">
    <tr>
    <td>T-Shirt &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <?php echo $val;?>
    <input name='val' type=hidden value=<?php echo $val; ?>/>
    <input type=submit value='Adicionar'/><br></td>
    <br>
    	
    <td>Boné &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade:  <?php echo $val2;?>
    <input name='val2' type=hidden value=<?php echo $val2; ?>/>
    <input type=submit value='Adicionar'/><br></td>
    
    <br>
    <td>Pin &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp  Quantidade: <input type="number" value="0" name="Quantidade3"><br></tr>  </td>
    <br>
    <tr>
    <td>Sapatilha &nbsp &nbsp &nbsp &nbsp  &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade4"><br></td>
    <br>
    
    
    <td>Bola &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade5"><br></td>
    <br>
    
    <td>Equipamento &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade6"><br> </tr></td>
    <br>
    
    <tr>
    <td>Polo &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade7"><br> </td>
    <br>
    
    <td>Jeans &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade8"><br> </td>
    <br>
    
    <td>Casaco  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade9"><br> </tr></td>
    <br>
    
    <tr>
    <td>Calção  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade10"><br> </td>
    <br>
    
    <td>Luvas &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade11"><br></td>
    <br>
    
    <td>Cachecol  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade12"><br></tr></td>
    <br>
    <tr>
    <td>Meias &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade13"><br></td>
    <br>
    
    <td>Cana de Pesca  &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade14"><br></td>
    <br>
    
    <td>Cintos &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade15"><br></tr> </td>
    <br>
    <tr>
    <td>Bicicleta  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade16"><br> </td>
    <br>
    
    <td>Skate &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade17"><br> </td>
    <br>
    
    <td>Patins &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade18"><br></tr> </td>
    <br>
    <tr>
    <td>Garrafa  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade19"><br> </td>
    <br>
    
    <td>Fato de Banho  &nbsp &nbsp Quantidade: <input type="number" value="0" name="Quantidade20"><br></td>
    <br>
    <td> Veja os produtos na galeria <br></tr> </td></tr>
    <br><br>
    	
    
            	
    </table>
    <input type="submit" value="Total" name="Multiplicacao"> 
    <br><br>
    
    </form>
    
    </center>
    
    </body>
    </html>
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,179
    Rep Power
    9398
    Hidden values will always be passed in the form. isset($_POST["val"]) and isset($_POST["val2"]) will always both be true (or both be false).

    You need to know what button the user pressed. Easiest way is to name the button and look for it in the $_POST array.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Thanks I got what you meant, though I don't know to apply it, So I named both buttons, though I don't know how I should edit the PHP part, could you give an example?

    Code:
    <?php
    
    //1
    $val = 0;
    if ($_POST["button"]=$_POST["val"]) {
       $val = $_POST["val"];
       $val=$val+1;
    }
    
    //2
    $val2 = 0;
    if ($_POST["button2"]=$_POST["val2"]) {
       $val2 = $_POST["val2"];
       $val2=$val2+1;
    }
    ?>
    
    <html>
    <body>
     <body bgcolor="#00CCCC">
    <?php
    $horas=date("H:i:s");
    $data=date("M/d/Y");
    echo $horas;
    echo "<br>";
    echo $data;
    ?> 
     <marquee> <i><h1>Bem Vindo(a) :)</h1></i></marquee>
     
      <marquee> <i><h2>Escolha os seus produtos desportivos </h2></i></marquee>
      
     <?php
    echo "<i><h3>Sobre a loja:</h3></i>";
    echo "A loja é de estilo desportista mas simples, tem como  função agradar 
    todos os clientes que a frequentam online  e a utilizam para fazer algumas compras que possam ser úteis para o seu bem-estar.";
    ?>
      <p>
      <i> Para ver quais os produtos visite a galeria </i><br>
    <a href="galeria.html">
            <input type="button" value="Galeria de Produtos">
    </a> </p>
    <center>
    <table border="1" cellpadding="1">
    <tr>
    <form action="nomediferente.php" method="POST">
    <tr>
    <td>T-Shirt &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade: <?php echo $val;?>
    <input name='val' type=hidden value=<?php echo $val; ?>/>
    <input type=submit value='Adicionar' name="button1"/><br></td>
    <br>
    	
    <td>Boné &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Quantidade:  <?php echo $val2;?>
    <input name='val2' type=hidden value=<?php echo $val2; ?>/>
    <input type=submit value='Adicionar' name="button2"/><br></td>
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    you really love &nbsp;, do you?

    Anyway, there's a syntax error and a logical error. Checking for equality is a double equals sign: ==. What you currently do is an assignment. And there's nothing to compare the button values with. Just check if either "button1" or "button2" has been submitted:
    PHP Code:
    $val 0;
    if (isset(
    $_POST['button1'])) {
       
    $val $_POST['val'];
       
    $val += 1;
    }

    $val2 0;
    if (isset(
    $_POST['button2'])) {
       
    $val2 $_POST['val2'];
       
    $val2 += 1;

    Apart from that, your HTML is pretty broken and will lead to misinterpretations by the browser. The "val" inputs all submit a trailing "/" after the actual number.
    Last edited by Jacques1; February 28th, 2013 at 08:25 AM.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    you really love &nbsp;, do you?

    Anyway, there's a syntax error and a logical error. Checking for equality is a double equals sign: ==. What you currently do is an assignment. And there's nothing to compare the button values with. Just check if either "button1" or "button2" has been submitted:
    PHP Code:
    $val 0;
    if (isset(
    $_POST['button1'])) {
       
    $val $_POST['val'];
       
    $val += 1;
    }

    $val2 0;
    if (isset(
    $_POST['button2'])) {
       
    $val2 $_POST['val2'];
       
    $val2 += 1;

    Apart from that, your HTML is pretty broken and will lead to misinterpretations by the browser. The "val" inputs all submit a trailing "/" after the actual number.
    Thanks, Well It's not 100% my code, It's one of my friend's code, though they asked for help :P.

    and the "/" is also fixed here. Though now I have another problem.
    When I press button 1 for example 5 times it will show as Val=5 which is correct, but as soon i press button2 that will increment Val2 to 1, for whatever reason Val(the first one) will go back down to 0 .
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by fiox
    When I press button 1 for example 5 times it will show as Val=5 which is correct, but as soon i press button2 that will increment Val2 to 1, for whatever reason Val(the first one) will go back down to 0 .
    Well, that's exactly what you (or your friend) have programmed it to do: You set both values to 0 initially and then only change it back to the transmitted value when the corresponding button has been pressed. In other words, when a button has not been pressed, the corresponding value will automatically become 0.

    If you don't want the values to become 0, you simply must not set them to 0 and instead always use the value from the hidden field:
    PHP Code:
    <?php

    // when the form hasn't been submitted yet (on the first visit), initialize both values with 0, otherwise always use the transmitted value
    $val = isset($_POST['val']) ? $_POST['val'] : 0;
    $val2 = isset($_POST['val2']) ? $_POST['val2'] : 0;

    // increment the value for which the button has been pressed
    if (isset($_POST['button1']))
       
    $val += 1;
    elseif (isset(
    $_POST['button2']))
       
    $val2 += 1;
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo