$_POST array problem from a dynamically generated form.
I'm trying to write a php web program that allows me to produce
custom text that I use often. For example, I might say:
Hello Bob
I will contact you at VARIABLE>>>9:30 AM // this time could change
Thank you
JeepiePHP
Or I might say
Hello Bob
Thank you for contacting me about VARIABLE>>>boxes. Here is the VARIABLE>>>box.doc. // these variables could change
Thank you
JeepiePHP
I am using SQL to pull the text from a MySQL database.
THE PROBLEM
The issue I have is with the use of $POST to collect my custom variables. Here is an example I've coded to show the issue. The key point is there custom controls are written out which the user can then populate and submit. However, $POST only ever holds the last control - why is this? and how do I get all three posted values?
Thanks
JEEPIEPHP
<html>
<title>Forms test</title>
<h1>New Controls POC</h1>
<body>
<?php
//function
//write the left function for cutting variables
function left($string, $length) {
return substr($string, 0, $length);
}
// Connecting, selecting database
$lk = mysql_connect('localhost', 'jeepiephp', 'password')
or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully';
mysql_select_db('test') or die('Could not select database');
// Performing SQL query
$query = 'SELECT customtext FROM textstore ';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "<table>\n";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$customtext = $row['customtext'];
echo "$customtext<br><br>";
//String returned the control 1 is called &&&brighteyes and the control 2 is called &&&will the control number 3 is called &&&cash
}
$vars = explode("&&&", $customtext);
//bin the first element of the array
$junk = array_shift($vars);
//get the control names
foreach ($vars as $v)
{
$spacepresent = strpos($v, " ");
if ($spacepresent==NULL) {
//this is used to handle last array element
$arraycontrols[] = $v;
} else {
//Used to handle all other array elements
$arraycontrols[] = left($v, $spacepresent);
}
}
//Here is my array of controls in the arraycontrols
//Now I build a form for them
print "<form action=\"$PHP_SELF\" method=\"POST\">\n";
foreach ($arraycontrols as $c) {
print "<input type=\"text\" name=\"\$c\"> ";
print "$c<p>\n";
}
echo "<br><br>";
print "<input type=\"submit\" value=\"Submit Custom Info\">\n</form>\n";
//////THE PROBLEM: THIS ONLY ECHOS OUT THE LAST VALUE///////////////////////////////
foreach($_POST as $x) {
print "<br><br>$x<br>";
}
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($lk);
?>
</body>
</html>