Can you help me with this?
How can i pass the info that I selected from Index.php to productinfo.php ? My friend told me that I should use "post method" but i don't know how to apply it to my code...
here is my code
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> </td>
</tr>
<tr>
<td> </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> </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);
?>
And This is my 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> </td>
</tr>
<tr>
<td><input type="submit" name="submit" id="submit" value="Add to Cart" /></td>
</tr>
<tr>
<td> </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);
?>
Thanks...
First, you have to create some fields and set it val via javascript when user select a row in table. After this, you get the infos in productinfo.php via $_POST['name_of_field_in_index.php']
Your form doesn't have any fields.
1) Do these changes in your 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) Make sure productinfo.php do something with the data. Use: print_r($_POST) to check post data.
Don't use mysql_* functions. They are already deprecated as of PHP 5.5 and removed in PHP 7. Insead use: http://php.net/manual/en/book.mysqli.php
Learn how to handle a form: http://www.the-art-of-web.com/php/form-handler/
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> </td>
</tr>
<tr>
<td><input type="submit" name="submit" id="submit" value="Add to Cart" /></td>
</tr>
<tr>
<td> </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
Yes your friend told is right you can pass a data from one page to another page using post.
There are Four way to you can pass a data from one page to another page.
1.Using Form
You need to feel the data in the form and on submit the form you redirect to another page(defined in action attribute of form) and you retrieve the data in rdirected page
let's one example 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'];
}
}
?>
above code is check submit button is clicked if clicked than vale of input text is set,if set than it's not a blank than retrieve name
here we use $_POST
coz method of form is set "post" default method is "get".if you didn't defined any method than default method get is set.so you can retrieve data using $_GET
You can also retrieve data no matter which method is set in form method attribute,using $_REQUEST
2.Using Anchor
3.Using Session
4.Using Cookie