在我的代码中使用Post方法

时间:2016-05-12 04:47:09

标签: php html

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

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

这是我的代码

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);
?>

...谢谢

4 个答案:

答案 0 :(得分:1)

  

你需要在index.php中添加以下内容,当这个表单提交时,它将重定向到产品页面,你将获得你的价值

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

在productinfo.php中你需要添加

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

在此页面上使用此变量

答案 1 :(得分:0)

您的表单没有任何字段。

1)在index.php中执行以下更改:

      <tr>
        <td><input name="name" type="text" value="<?php echo $row_showproduct['name']; ?>" /></td>
      </tr>
      <tr>
        <td><input name="brand" type="text" value="<?php echo $row_showproduct['brand']; ?>" /></td>
      </tr>
      <tr>
        <td><input name="category" type="text" value="<?php echo $row_showproduct['category']; ?>" /></td>
      </tr>
      <tr>
        <td><input name="price" type="text" value="<?php echo $row_showproduct['price']; ?>" /></td>
      </tr>

2)确保productinfo.php对数据做了些什么。使用:print_r($ _ POST)来检查发布数据。

  

不要使用mysql_ *函数。从PHP 5.5开始,它们已被弃用,并在PHP 7中被删除.Insead使用:http://php.net/manual/en/book.mysqli.php

     

了解如何处理表单:http://www.the-art-of-web.com/php/form-handler/

答案 2 :(得分:0)

是的,您的朋友告诉您是对的,您可以使用帖子将数据从一个页面传递到另一个页面。

您可以通过四种方式将数据从一个页面传递到另一个页面。

1.使用表格

您需要感受表单中的数据并提交您重定向到另一个页面的表单(在表单的操作属性中定义)并在rdirected页面中检索数据

让我们举个例子 的的index.php

<form action="production.php" method="post">
   <input type="text" name="txt_name">
   <input type="submit" value="Submit" name="product">
</form>

production.php

<?php
// on submit button is clicked
if(isset($_POST['product'])){
   if(isset($_POST['txt_name']) && $_POST['txt_name'] != ""){
     $name = $_POST['txt_name'];
    }

}
?>

上面的代码是检查提交按钮,如果单击则单击,而不是设置输入文本的值,如果设置的话,它不是空白而不是检索名称

这里我们使用$_POST表单的coz方法设置“post”默认方法是“get”。如果你没有定义任何方法而不是设置默认方法get。因此您可以使用$_GET

检索数据

无论使用$_REQUEST

在表单方法属性中设置哪种方法,您都可以检索数据

2.使用Anchor

3.使用会话

4.使用Cookie

答案 3 :(得分:-2)

首先,当用户在表格中选择一行时,您必须创建一些字段并通过javascript将其设置为val。在此之后,您可以通过$ _POST ['name_of_field_in_index.php']获取productinfo.php中的信息。