dsa99349 2016-05-29 06:42
浏览 192
已采纳

循环在html和php中创建多个按钮

So basically what I am trying to do is using a loop to display multiple image and create a corresponding button to each image. If I pressed on the corresponding button, it will store the image into another database.

The first part display quite well. However, the second part I can hardly figure out how to determine the corresponding button for each image.

<?php while($row=mysqli_fetch_array($result)) { ?>
<div id="item">
  <?php echo '<img height="200" width="200" src="data:image;base64,'.$row[2]. '">';?>
  </br>
  <?php echo $row[ "name"];?>
  </br>
  <?php echo $row[ "price"];?>
  </br>
  <?php echo $row[ "description"];?>
  </br>
 <form>Quantity:
  <input type="text" value="" name="quantity" />
  </br>
  <input type="submit" value="add to cart" name="cart" />
 </form>
  <?php 
if (isset($_POST[ "cart"])){ 
$addtoname=$_SESSION[ 'username']; 
$addtoprice=$row[ 'price']; 
$addtodiscount=$row[ 'discount']; 
$addtoid=$row[ 'id']; 
$addtoimage=$row[ 'image']; 
$addtoquantity=$_POST[ 'quantity']; 
$hostname="localhost" ; 
$username="root";
$password="" ; 
$database="myproject" ; $con=mysqli_connect($hostname,$username,$password,$database) or die(mysqli_error()); 
$select=mysqli_select_db($con, "myproject")or die( "cannnot select db"); mysqli_query($con,"INSERT INTO cart(username,quantity,price,image,id,discount) VALUES('$addtoname','$addtoquantity','$addtoprice','$addtoimage','$addtoid','$addtodiscount')"); 
echo "success"; 
} 
else{ 
echo "fail"; 
}?>
</div>
<?php }//end of while ?>

It seems like it always go to fail AND never run into isset($_POST[ "cart"]))

Any tips will be much appreciated.

Thank you

</div>
  • 写回答

2条回答 默认 最新

  • duanpo1498 2016-05-29 09:35
    关注

    Create 2 files:

    One with the actual form populated by the DB:

    <?php while($row=mysqli_fetch_array($result)) { ?>
    <div id="item">
     <form method="post" action="cartReceiver.php">Quantity:
      <?php echo '<img height="200" width="200" src="data:image;base64,'.$row[2]. '">';?>
      </br>
      <?php echo $row[ "name"];?>
      </br>
      <?php echo $row[ "price"];?>
      </br>
      <?php echo $row[ "description"];?>
      </br>
      <input type="text" value="" name="quantity" />
      </br>
      <input type="submit" value="add to cart" name="cart" />
     </form>
    </div>
    <?php }//end of while ?>
    

    And then the receiver of the action declared in form(cartReceiver.php):

    <?php
    if (isset($_POST[ "cart"])){ 
    $addtoname=$_SESSION[ 'username']; 
    $addtoprice=$row[ 'price']; 
    $addtodiscount=$row[ 'discount']; 
    $addtoid=$row[ 'id']; 
    $addtoimage=$row[ 'image']; 
    $addtoquantity=$_POST[ 'quantity']; 
    $hostname="localhost" ; 
    $username="root";
    $password="" ; 
    $database="myproject" ; $con=mysqli_connect($hostname,$username,$password,$database) or die(mysqli_error()); 
    $select=mysqli_select_db($con, "myproject")or die( "cannnot select db"); mysqli_query($con,"INSERT INTO cart(username,quantity,price,image,id,discount) VALUES('$addtoname','$addtoquantity','$addtoprice','$addtoimage','$addtoid','$addtodiscount')"); 
    echo "success"; 
    } 
    else{ 
    echo "fail"; 
    }
    

    Note that I changed the tag order of the form. If you wish to receive the fetched parameters from POST without querying the DB after transition, then you can change the first file like this:

     <?php while($row=mysqli_fetch_array($result)) { ?>
        <div id="item">
         <form method="post" action="cartReceiver.php">Quantity:
          <?php echo '<img height="200" name="image" width="200" src="data:image;base64,'.$row[2]. '">';?>
          </br>
          <input type="text" name="username" value="<?php echo $row[ "name"];?>" readonly /> 
          </br>
          <input type="text" name="price" value="<?php echo $row[ "price"];?>" readonly />
          <input type="hidden" name="discount" value="<?php echo $row[ "discount"];?>" />
          </br>
          <input type="text" name="description" value="<?php echo $row[ "description"];?>" readonly />  
          </br>
          <input type="text" value="" name="quantity" />
          </br>
          <input type="submit" value="add to cart" name="cart" />
         </form>
        </div>
        <?php }//end of while ?>
    

    So the cartReceiver.php file will look like this in this case:

    <?php
    if (isset($_POST[ "cart"])){ 
    $addtoname=$_POST['username']; 
    $addtoprice=$_POST['price']; 
    $addtodiscount=$_POST['discount']; 
    $addtoid=$_POST['id']; 
    $addtoimage=$_POST['image']; 
    $addtoquantity=$_POST[ 'quantity']; 
    $hostname="localhost" ; 
    $username="root";
    $password="" ; 
    $database="myproject" ; $con=mysqli_connect($hostname,$username,$password,$database) or die(mysqli_error()); 
    $select=mysqli_select_db($con, "myproject")or die( "cannnot select db"); mysqli_query($con,"INSERT INTO cart(username,quantity,price,image,id,discount) VALUES('$addtoname','$addtoquantity','$addtoprice','$addtoimage','$addtoid','$addtodiscount')"); 
    echo "success"; 
    } 
    else{ 
    echo "fail"; 
    }
    

    Notice the readonly attirbute in the inputs. It will prevent the users from altering their contents.

    Now you can access all the inputs after you submit the form (click the add to cart button) by using $_POST['name'],$_POST['description'] etc

    EDIT: Code updated for your needs. Since it seems you dont want to display the discount, you pass it to the form through a hidden field which can be then accessed as the others through $_POST.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?