Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fluctuating variable 1

Status
Not open for further replies.

chessbot

Programmer
Mar 14, 2004
1,524
US
Hi all!

I am writing a PHP script. Included is a function that outputs certain information based on a given name. Here is some code
Code:
  echo "<tr>";
  echo "<td><input type=\"text\" name=\"" . $family . "_firstname\"></td>";
  echo "<td><input type=\"text\" name=\"" . $family . "_lastname\" value=\"{$lastname}\"></td>";
  echo "<td><input type=\"text\" name=\"familyname\" value=\"" . $family . "\"></td>";
  foreach ($fields as $field)
  {
    echo "<td>";
    echo "<select name=\"" . $family . "_{$field}\">";
    echo "<optgroup class=\"indefinite\">";
    echo "<option value=\"e\">Expected to come</option>";
    echo "<option value=\"f\">Expected not to come</option>";
    echo "</optgroup>";
    echo "<option value=\"y\">Coming</option>";
    echo "<option value=\"n\">Not coming</option>";
    echo "<option value=\"x\">Not invited</option>";
    echo "</select>";
    echo "</td>";
  }
  echo "<td>";
  echo "<select name=\"" . $family . "_group\">";
  echo "<option value=\"c\">Child</option>";
  echo "<option value=\"t\">Teenager</option>";
  echo "<option value=\"a\">Adult</option>";
  echo "</select>";
  echo "</td>";
  echo "<td><a href=\"#\" onclick=\"submitForm(this,'insert', '{$family}');\">Add</a></td>";
  echo "</form>";
  echo "</tr>";
[tt]$family[/tt] is a given variable.
My problem is this: When I first run the function, everything prints fine. The next times, for some reason, [tt]$family[/tt] defaults to the value of the first time passed, except when called the last time. I tried echoing it separately and it returned the correct value; in fact, I have tried everything I could think of. Any ideas?

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
First I would clean up your code, way too many double quotes (") where single quotes(') would do.

For example, you have:
Code:
  echo "<tr>";
  echo "<td><input type=\"text\" name=\"" . $family . "_firstname\"></td>";
  echo "<td><input type=\"text\" name=\"" . $family . "_lastname\" value=\"{$lastname}\"></td>";
  echo "<td><input type=\"text\" name=\"familyname\" value=\"" . $family . "\"></td>";
You can make it easier on the eyes with the following:
Code:
  echo '<tr>';
  echo '<td><input type="text" name="' . $family . '_firstname"></td>';
  echo '<td><input type="text" name="' . $family . '_lastname" value="' . $lastname .'"></td>';
  echo '<td><input type="text" name="familyname" value="' . $family . '"></td>';
When you use single quotes in your echo statements, you don't have to escape the double quotes in you HTML.

Another way, that I've started to use lately (don't know how efficient it is...)
Code:
  $tmp = array();
  $tmp[] = '<tr>';
  $tmp[] = '<td><input type="text" name="' . $family . '_firstname"></td>';
  $tmp[] = '<td><input type="text" name="' . $family . '_lastname" value="' . $lastname .'"></td>';
  $tmp[] = '<td><input type="text" name="familyname" value="' . $family . '"></td>';
  echo implode("\n",$tmp)."\n";
Has the advantange of adding a carraige return to each HTML line that is output.

Second, why are you using a Javascript submit function, when a normal HTML submit will work fine?

I don't have an answer for you, but cleaning up your code might help you find the problem.

Ken
 
That was just one of multiple versions I have done, including using {$family} and echoing it on a separate line. The reason for the Javascript submit is so that I can change the action of the form before it is submitted (include stuff in the query string). I will make a javascript-free page when I get around to it.

I'll try with single quotes, but...

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
Cleaning it up didn't help directly, but I realized that the problem was not the variable: I was getting the values with [tt]print_r($_POST);[/tt] and I was closing the form each time, so only the first records were sent.

Thanks; my code looks better now.

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top