Multiple Forms on One Page - Process Posts???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Multiple Forms on One Page - Process Posts???

Post by pizzipie »

Hi, I have two forms on a single page. i have used some Javascript I found on the internet to process checkboxes. I can't get the two arrays $home and $work to
pick up the $_POST information. It seems to get the last information only.

Really hope someone can help. driving me crazy for two days.

Thanks in advance

Rick P. PS: Sorry, had to put code here - Upload file thingee won't let me upload the file. Won't allow file extension - there is none ?????

Code: Select all


<?php session_start();

  require_once 'db-contacts.inc';    
  require_once "functions.inc"; 
  require_once "functions-contacts.inc"; 

  if (!($cxn = @ mysqli_connect($hostName, $username, $password, $databaseName))) //Connect to the MySQL server
     showerror($cxn);
     
   $tblFields=getTableFields($cxn, $table);         
   $labels=array_combine($tblFields, $colhead);

  $header= <<<END
	<div id="header">

  		<h1><img src="/images/AmericanFlag2.png" alt="Company logo" height="90px" />

				Rick and Polly&rsquo;s Contacts</h1>

   		<h3> <span style="padding-left: 140px"> Query Builder - Select </span></h3>
 	 </div>  		
END;
printf("%s",$header);

 ?>

<!DOCTYPE HTML PUBLIC
                 "-//W3C//DTD HTML 4.01 Transitional//EN"
                 "http://www.w3.org/TR/html401/loose.dtd">
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet " href="/css/multiList.css" type="text/css" </link>

<SCRIPT LANGUAGE="JavaScript">

function highlight_div(checkbox_node)
{
    label_node = checkbox_node.parentNode;

    if (checkbox_node.checked)
	{
		label_node.style.backgroundColor='#0a246a';
		label_node.style.color='#fff';
	}
	else
	{
		label_node.style.backgroundColor='#fff';
		label_node.style.color='#000';
	}
}

</SCRIPT>

</head>

<body>

<?php

if (array_key_exists('home', $_POST)) {
echo "<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>";	
	
$home=$_POST;
}
 
if (array_key_exists('work', $_POST)) {
	echo"<span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span>";
$work=$_POST;
}



//  =============  home  ==========

$i=0;
printf("<div id='home'>\n");
printf("Home Related Fields<br>\n");
printf("<form name='home' method='POST' action=''>\n");

printf("<div id='div1'>\n");
printf("<div id='div2'>\n");

	foreach($labels as $key => $value) {

		if($key=='Name_ID')  continue;
		
		if($key=="Job_Title") break;
		 
		$K=strtolower(preg_replace("/_/","",$key));			 	
		printf("<label for='$K'>\n");
		printf("<input type='checkbox' name='checkbox[]' value=%s id=%s
					 onclick='highlight_div(this);' />%s</label>\n", $key, $K, $key);
	$i++;
	}
		printf("<input type='hidden' name='home' value='1'/>\n");
		
printf("</form>\n");
printf("</div>\n");
printf("</div>\n");
printf("<br><input type='submit' id='home' value='Make your Picks'>\n");
printf("</div>\n");



//  ==============  work  ============

$i++;
$j=0;

printf("<div id='work'>\n");
printf("Work Related Fields<br>\n");
printf("<form name='work' method='POST' action=''>\n");

printf("<div id='div1'>\n");
printf("<div id='div2'>\n");

	foreach($labels as $key => $value) {
		
		if($j++<$i)  continue;

		$K=strtolower(preg_replace("/_/","",$key));			 	
		printf("<label for='$K'>\n");
		printf("<input type='checkbox' name='checkbox[]' value=%s id=%s
					 onclick='highlight_div(this);' />%s</label>\n", $key, $K, $key);
	}
		printf("<input type='hidden' name='work' value='1'/>\n");
		
printf("</form>\n");
printf("</div>\n");
printf("</div>\n");
printf("<br><input type='submit' id='work' value='Make your Picks'>\n");
printf("</div>\n");


?>				



</body>
</html>


<?php
echo "<br><br>Items checked<br><br>";

echo "home"; listArray($home);         //print_r with <pre>
echo "work"; listArray($work);


?>

[text]
/* =========== results below ==============

Items checked

home

1

work

Array
(
[checkbox] => Array
(
[0] => Vendor_ID
[1] => Organization
[2] => Work_Address
[3] => Work_City
)

[work] => 1
)
1 */
[/text]
/* ====================== multiList.css ===============

Code: Select all

#home #div1 {
	font:13.3px sans-serif;
	width:15em;
	border-left:2px solid #808080;
	border-top:2px solid #808080;
	border-bottom:2px solid #fff;
	border-right:2px solid #fff;
}

#home #div2 {
	background:#fff;
	overflow:auto;
	height:13em;
	border-left:2px solid #404040;
	border-top:2px solid #404040;
	border-bottom:2px solid #d4d0c8;
	border-right:2px solid #d4d0c8;
}

#work {
	position: relative;
	right: -400px;
	top: -290px;
}

#work #div1 {
	font:13.3px sans-serif;
	width:15em;
	border-left:2px solid #808080;
	border-top:2px solid #808080;
	border-bottom:2px solid #fff;
	border-right:2px solid #fff;
}

#work #div2 {
	background:#fff;
	overflow:auto;
	height:19em;
	border-left:2px solid #404040;
	border-top:2px solid #404040;
	border-bottom:2px solid #d4d0c8;
	border-right:2px solid #d4d0c8;
}



label {
	padding-right:3px;display:block;
}     

*/
Last edited by Benjamin on Fri Oct 22, 2010 4:43 pm, edited 1 time in total.
Reason: Fixed BB Code
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Re: Multiple Forms on One Page - Process Posts???

Post by invisibled »

Why have them in two forms? I didn't fully dive into your code but it seems you could just make it one form? Also when posting code, just use the PHP Code button above and it wraps it nicely :)
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: Multiple Forms on One Page - Process Posts???

Post by pizzipie »

Thanks Benjamin,

I'm trying to split a lengthy 'contacts table' into 2 parts, Home and Work. This is a first step towards building a query builder.

By splitting this into two forms the query entry fields are all shown neatly without scrolling.

Sorry I thought I had put the code into the PHP Code box. I tried to upload the file itself but the program wouldn't let me.
Said it couldn't have an extension even though there was no extension to the file name. Go figure???

R
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: Multiple Forms on One Page - Process Posts???

Post by pizzipie »

RESOLVED:

I decided to just split the table visually and use one submit. Works fine.

Thanks, R
Post Reply