php - 在我的代码中使用 Post 方法

标签 php html

你能帮我解决这个问题吗?

如何将我从 Index.php 中选择的信息传递给 productinfo.php? 我的 friend 告诉我,我应该使用“post 方法”,但我不知道该怎么做 将其应用于我的代码...

这是我的代码

index.php:

<?php require_once('Connections/localhost.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$maxRows_showproduct = 6;
$pageNum_showproduct = 0;
if (isset($_GET['pageNum_showproduct'])) {
  $pageNum_showproduct = $_GET['pageNum_showproduct'];
}
$startRow_showproduct = $pageNum_showproduct * $maxRows_showproduct;

mysql_select_db($database_localhost, $localhost);
$query_showproduct = "SELECT * FROM product";
$query_limit_showproduct = sprintf("%s LIMIT %d, %d", $query_showproduct,  $startRow_showproduct, $maxRows_showproduct);
$showproduct = mysql_query($query_limit_showproduct, $localhost) or die(mysql_error());
$row_showproduct = mysql_fetch_assoc($showproduct);

if (isset($_GET['totalRows_showproduct'])) {
  $totalRows_showproduct = $_GET['totalRows_showproduct'];
} else {
  $all_showproduct = mysql_query($query_showproduct);
  $totalRows_showproduct = mysql_num_rows($all_showproduct);
}
$totalPages_showproduct =     ceil($totalRows_showproduct/$maxRows_showproduct)-1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home</title>
<link href="css/mystyle.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
}
</style>
</head>
<body>
<div id="container">
<div id="header"></div>
     <!-- *************************** MENU -->
    <div id="menu">
        <ul>
            <li><a href="login.php">Login</a></li>  
            <li><a href="signup.php">Signup</a></li>
            <li><a href="#">Home</a></li>
        </ul>
    </div>
    <!-- *************************** CATEGORY -->
    <div id="category"><ul>
            <li><a href="computers.php"><img src="images/menudesktop.png" width="333" height="100" border="0"/></a></li>
            <li><a href="laptops.php"><img src="images/menulaptop.png" width="333" height="100" alt=""/></a></li>
            <li><a href="mobiles.php"><img src="images/menumobile.png" width="333" height="100" alt=""/></a></li>
        </ul>
        </div>       
    <!-- *************************** BRAND -->     
    <div id="brand">
        <ul>
            <li><a href="#">Intel</a></li>  
            <li><a href="#">Lenovo</a></li>
            <li><a href="#">Samsung</a></li>
            <li><a href="#">Dell</a></li>   
            <li><a href="#">Cherry Mobile</a></li>
            <li><a href="#">Apple</a></li>
        </ul>
    </div>   
    <form id="mainbodyform" name="mainbodyform" method="post"     action="productinfo.php">
    <!-- *************************** MAINBODY --> 

    <div id="mainbody">

        <!-- *************************** PRODUCT CONTAINER -->

      <?php do { ?>
        <div id="productcontainer">
          <div id="imgbox"></div>
          <div id="txtbox">
            <table width="250" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td><?php echo $row_showproduct['name']; ?></td>
              </tr>
              <tr>
                <td><?php echo $row_showproduct['brand']; ?></td>
              </tr>
              <tr>
                <td><?php echo $row_showproduct['category']; ?></td>
              </tr>
              <tr>
                <td>₱<?php echo $row_showproduct['price']; ?></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td><input type="submit" name="submit" id="submit" value="Buy Now!" /></td>
              </tr>
            </table>
          </div>
        </div>
          <?php } while ($row_showproduct = mysql_fetch_assoc($showproduct)); ?>
    </div>
    </form>
  <!-- *************************** FOOTER -->
  <div id="footer"><p>Runts & Giants</p></div>   

</div>
</body>
</html>
<?php
mysql_free_result($showproduct);
?>

这是我的 productinfo.php

<?php @session_start(); ?>
<?php require_once('Connections/localhost.php'); ?>
<?php
$_SESSION['Index.php']=3;
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_localhost, $localhost);
$query_productinfo = "SELECT * FROM product";
$productinfo = mysql_query($query_productinfo, $localhost) or die(mysql_error());
$row_productinfo = mysql_fetch_assoc($productinfo);
$totalRows_productinfo = mysql_num_rows($productinfo);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>layout</title>
<link href="css/mystyle.css" rel="stylesheet" type="text/css" />
<link href="css/productinfostyle.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
}
</style>
</head>
<body>
<div id="container">
<div id="header"></div>
     <!-- *************************** MENU -->
    <div id="menu">
        <ul>
            <li><a href="#">Order</a></li>  
            <li><a href="#">Cart</a></li>
            <li><a href="#">Home</a></li>
        </ul>
    </div>
    <!-- *************************** CATEGORY -->
    <div id="category"><ul>
            <li><a href="computers.php"><img src="images/menudesktop.png" width="333" height="100" border="0"/></a></li>
            <li><a href="laptops.php"><img src="images/menulaptop.png" width="333" height="100" alt=""/></a></li>
            <li><a href="mobiles.php"><img src="images/menumobile.png" width="333" height="100" alt=""/></a></li>
        </ul>
  </div>       
  <!-- *************************** BRAND -->     
    <div id="brand">
        <ul>
            <li><a href="#">Intel</a></li>  
            <li><a href="#">Lenovo</a></li>
            <li><a href="#">Samsung</a></li>
            <li><a href="#">Dell</a></li>   
            <li><a href="#">Cherry Mobile</a></li>
            <li><a href="#">Apple</a></li>
        </ul>
    </div>   
  <!-- *************************** MAINBODY --> 
    <div id="mainbody">
        <form id="productform" name="productform.php" method="">
          <div id="itemimg">1</div>
            <div id="itemdesc"><br />
              <br />
              <table width="600" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="301"><table width="300" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td><?php echo $row_productinfo['name']; ?></td>
                    </tr>
                    <tr>
                      <td><?php echo $row_productinfo['brand']; ?></td>
                    </tr>
                    <tr>
                      <td><?php echo $row_productinfo['category']; ?></td>
                    </tr>
                    <tr>
                      <td><?php echo $row_productinfo['price']; ?></td>
                    </tr>
                    <tr>
                      <td>&nbsp;</td>
                    </tr>
                    <tr>
                      <td><input type="submit" name="submit" id="submit" value="Add to Cart" /></td>
                    </tr>
                    <tr>
                      <td>&nbsp;</td>
                    </tr>
                  </table></td>
                  <td width="299"><?php echo $row_productinfo['description']; ?></td>
                </tr>
              </table>
            </div>


        </form>
  </div>
  <!-- *************************** FOOTER -->
    <div id="footer"><p>Runts & Giants</p></div>   

</div>
</body>
</html>
<?php
mysql_free_result($productinfo);
?>

谢谢...

最佳答案

you need to add from as below in index.php when this form submit it will redirect to product page and you will get your value

<form id="productform" name="productform.php" action="productform.php" method="post">
          <div id="itemimg">1</div>
            <div id="itemdesc"><br />
              <br />
              <table width="600" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="301"><table width="300" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td><?php echo $row_productinfo['name']; ?>
                      <input type="hidden" value="<?php echo $row_productinfo['name']; ?>" name="product_name"></td>
                    </tr>
                    <tr>
                      <td><?php echo $row_productinfo['brand']; ?>
                      <input type="hidden" value="<?php echo $row_productinfo['brand']; ?>" name="product_brand">
                      </td>
                    </tr>
                    <tr>
                      <td><?php echo $row_productinfo['category']; ?>
                      <input type="hidden" value="<?php echo $row_productinfo['category']; ?>" name="product_category">
                      </td>
                    </tr>
                    <tr>
                      <td><?php echo $row_productinfo['price']; ?>
                      <input type="hidden" value="<?php echo $row_productinfo['price']; ?>" name="product_price">
                      </td>
                    </tr>
                    <tr>
                      <td>&nbsp;</td>
                    </tr>
                    <tr>
                      <td><input type="submit" name="submit" id="submit" value="Add to Cart" /></td>
                    </tr>
                    <tr>
                      <td>&nbsp;</td>
                    </tr>
                  </table></td>
                  <td width="299"><?php echo $row_productinfo['description']; ?></td>
                </tr>
              </table>
            </div>


        </form>

and in productinfo.php you need to add

    <?php  if(isset($_POST['submit'])):
        echo $name=$_POST['product_name'];
        echo $brand=$_POST['product_brand'];
       echo  $category=$_POST['product_category'];
       echo  $price=$_POST['product_price'];
endif;
        ?>

use this variable where you need on this page

关于php - 在我的代码中使用 Post 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37177485/

相关文章:

python - 从网络缓存数据的好方法(和/或独立于平台的地方)

php - 如何通过键名/路径访问和操作多维数组?

javascript - 在 Web 应用程序中上传大文件的最可靠方法是什么?

php - 数据因 PHP 和 mySQL 的列错误而被截断?

php - 从 css 和 php 创建简单的条形图

javascript - 包含脚本

html - 如何将 Logo 和文字并排放置

javascript - 显示一秒钟的 PHP 输出

php - 从 MySQL 获取数据到 bootstrap 表时不显示撇号

php - 覆盖身份验证失败处理程序 - Symfony2