Hello everyone,
I´m newbie in PHP and I can´t solve following problem.
My code contains form of unknown number of rows. I add it using jQuery class ( clone the last row ).
I wanted to use array. Write out complete form for confirmation and insert it i. e. as 'order' into Mysql.
My problems are:
1) I can´t write it out correctly. There is list of two form rows:
( Item:1
Type: Acoustic
Model:
Quantity:
Item:2
Type:
Model:
Standard
Quantity:
Item:3
Type:
Model:
Quantity: 8
Item:4
Type: Acoustic
Model:
Quantity:
Item:5
Type:
Model: Standard
Quantity:
Item:6
Type:
Model:
Quantity: 1
Comments:
Hello, folks)

I suspect that the problem is in the variable $i, but I have no idea how to define it if I don´t know number of rows.

2) How can I insert it into Mysql

Please help. Thanks a lot. Petr

<!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" />
<link rel="stylesheet" type="text/css" media="scReen" href="css/style.css"/>
<script case="javascript" type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.table.addrow.js"></script>
<script type="text/javascript">
(function($){
	$(document).ready(function(){
		$(".addRow").btnAddRow();
		$(".delRow").btnDelRow();
});
})(jQuery);
</script>
</head>
<body>
<?php
echo "<font color='#CC3300' size='5'>Order</font><br /><br />";
echo"<form method='POST' action='#'>";
	echo"<table cellspacing='0'  style='border: #DDDDDD 1px solid;'>
	<tr><td><input type='button' class='delRow' value='Delete Row'/></td>
	<td><div><select name='case[$i][typ]' style='width:200px;'>
		 <option value='Acoustic'>Acoustic</option>
		 <option value='Electric'>Electric</option>
		 <option value='Jazz'>Jazz</option>
		   </select>
		 </td>
	     <td><select name='case[$i][mod]' style='width:180px;'>
		 <option value='Standard'>Standard</option>
		 <option value='Luxury'>Luxury</option>
		 <option value='Professional'>Professional</option>
		 </select></div></td> 
		 <td><div><input type='text' name='case[$i][poc]' size='2' value=''></div></td> 
			</tr>
			<tr><td colspan='3' align='right'>
<input type='button' class='addRow' name='add' value='Add Row'/></td></tr>
<tr><td>Comments: </td></tr>
			<tr><td colspan='5'><textarea name='comment' cols='53' rows='3'></textarea></td><tr>
			  <tr><td colspan='5' align='center'><input type='submit' name='send' value='Send'></td></tr>";
echo"</table>";
echo"</form>";
?>
<?php
if(isset($_POST['send']))
{
   $case = $_POST['case'];
   $n        = count($case);
   $i        = 0;
  $c = $_POST['comment'];

   echo "Your select: \r\n" .
        "<ol>";
  for ($i = 0; $i < $n; $i++)
   {
      echo "<b>Item:</b>" .($i+1);
	  echo "<li><b>Type:</b> {$case[$i]['typ']}</li> \r\n";
	  echo "<li><b>Model:</b> {$case[$i]['mod']}</li> \r\n";
      echo "<li><b>Quantity:</b> {$case[$i]['poc']}</li> \r\n";
   }
      echo "<li><b>Comments:</b> </li>". $c;
   echo "</ol>";
}
?>
</body>
</html>

I´m sorry, I forgot jQuery code.

/*
 *
 * Copyright (c) 2009 C. F., Wong (<a href="http://cloudgen.w0ng.hk">Cloudgen Examplet Store</a>)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * See details in: <a href="http://cloudgen.w0ng.hk/javascript/javascript.php">Javascript Examplet</a>
 *
 */
(function($){
	var ExpandableTableList=[];
	function ExpandableTable(target,maxRow){
		if(target) this.init(target,maxRow);
	}
	ExpandableTable.prototype.init=function(target,maxRow){
		ExpandableTableList.push(this);
		this.target=$(target).data("ExpandableTable",this);
		this.maxRow=maxRow;
		this.seed=Math.round(Math.random()*10000);
		return this
	};
	ExpandableTable.prototype.live=function(){
		if (!this.goLive){
			var t=this;
			$(".addRow"+this.seed).live("click",function(){
				t.addRow().update();
			});
			$(".delRow"+this.seed).live("click",function(){
				$(this).closest("tr").remove();
				t.update();
			});
			this.update();
			this.goLive=true;
		}
		return this
	};
	ExpandableTable.prototype.update=function(){
		this.delRowButtons=$(".delRow"+this.seed,this.target);
		if(this.delRowButtons.size()==1) this.delRowButtons.hide();
		else this.delRowButtons.show();
		return this
	};
	ExpandableTable.prototype.addRow=function(){
		if(!this.maxRow || (this.maxRow && $(".delRow"+this.seed).size()<this.maxRow)){
			this.delRowButtons.show();
			var lastRow=$(".delRow"+this.seed+":last",this.target).closest("tr");
			var newRow=lastRow.clone()
				.insertAfter(lastRow)
				.find("input:text").val("");
		}
		return this
	};
	$.fn.btnAddRow=function(options){
		var maxrow=(options && typeof options.maxRow!="undefined")?options.maxRow:null;
		this.each(function(){
			var tbl=$(this).closest("table");
			if(tbl.size()>0){
				if(typeof tbl.data("ExpandableTable")==="undefined"){
					var etbl=new ExpandableTable(tbl,maxrow);
					$(this)
						.addClass("addRow"+etbl.seed)
						.data("ExpandableTable",etbl);
				}else{
					$(this)
						.addClass("addRow"+tbl.data("ExpandableTable").seed)
						.data("ExpandableTable",tbl.data("ExpandableTable"));
				}
			};
		});
		for(var i=0;i<ExpandableTableList.length;i++){
			if(!ExpandableTableList[i].goLive){
				ExpandableTableList[i].live();
			}
		}
	};
	$.fn.btnDelRow=function(options){
		var maxrow=(options && typeof options.maxRow!="undefined")?options.maxRow:null;
		this.each(function(){
			var tbl=$(this).hide().closest("table");
			if(tbl.size()>0){
				if(typeof tbl.data("ExpandableTable")==="undefined"){
					var etbl=new ExpandableTable(tbl,maxrow);
					$(this)
						.addClass("delRow"+etbl.seed);
				}else{
					$(this)
						.addClass("delRow"+tbl.data("ExpandableTable").seed)
				}
			}
		});
		for(var i=0;i<ExpandableTableList.length;i++){
			if(!ExpandableTableList[i].goLive){
				ExpandableTableList[i].live();
			}
		}
	};
})(jQuery);

Recommended Answers

All 5 Replies

Instead of using an array (which is usually for a fixed amount of objects) you may want to look into a linked-list method.

As for the mySQL problem I wish I could help you... I hope me posting this response doesn't keep others from looking at this.

Thank you for fast reply PsychicTide.
May I ask you for suggestion - where can I start to learn more about linked list? However I know nothing about it ...
Thanks Petr

O.k. I tried before ... I found a few of codes, but without success with tutorial.
I´m going to go to google again :o)) . Regards Petr

Dear PetrQ,

Your HTML component should use "[]" as the last part of the name in PHP. For example, <input type="text" name="type[]"/> .

Thus, your code should be:

<!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" />
<link rel="stylesheet" type="text/css" media="scReen" href="css/style.css"/>
<script case="javascript" type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/jquery.table.addrow.js"></script>
<script type="text/javascript">
(function($){
	$(document).ready(function(){
		$(".addRow").btnAddRow();
		$(".delRow").btnDelRow();
});
})(jQuery);
</script>
</head>
<body>
<?php
echo "<font color='#CC3300' size='5'>Order</font><br /><br />";
echo"<form method='POST' action='#'>";
	echo"<table cellspacing='0'  style='border: #DDDDDD 1px solid;'>
	<tr><td><input type='button' class='delRow' value='Delete Row'/></td>
	<td><div><select name='type[]' style='width:200px;'>
		 <option value='Acoustic'>Acoustic</option>
		 <option value='Electric'>Electric</option>
		 <option value='Jazz'>Jazz</option>
		   </select>
		 </td>
	     <td><select name='mod[]' style='width:180px;'>
		 <option value='Standard'>Standard</option>
		 <option value='Luxury'>Luxury</option>
		 <option value='Professional'>Professional</option>
		 </select></div></td> 
		 <td><div><input type='text' name='poc[]' size='2' value=''></div></td> 
			</tr>
			<tr><td colspan='3' align='right'>
<input type='button' class='addRow' name='add' value='Add Row'/></td></tr>
<tr><td>Comments: </td></tr>
			<tr><td colspan='5'><textarea name='comment' cols='53' rows='3'></textarea></td><tr>
			  <tr><td colspan='5' align='center'><input type='submit' name='send' value='Send'></td></tr>";
echo"</table>";
echo"</form>";
?>
<?php
if(isset($_POST['send']))
{
   $type = $_POST['type'];
   $mod = $_POST['mod'];
   $poc = $_POST['poc'];
   $n        = count($type);
   $i        = 0;
  $c = $_POST['comment'];

   echo "Your select: \r\n" .
        "<p></p>";
  for ($i = 0; $i < $n; $i++)
   {
      echo "<hr/><b>Item:</b>" .($i+1);
	  echo "<br/><b>Type:</b> {$type[$i]}</li> \r\n";
	  echo "<br/><b>Model:</b> {$mod[$i]}</li> \r\n";
      echo "<br/><b>Quantity:</b> {$poc[$i]}</li> \r\n";
   }
      echo "<br/><b>Comments:</b> ". $c;
}
?>
</body>
</html>
</html>

By the way, thanks for using my jQuery plugin :)
You may see the online demo for the above code in: http://cloudgen.w0ng.hk/sandbox/items.php

~~cloudgen

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.