form branching question

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
cuttcards
Forum Newbie
Posts: 2
Joined: Sun May 20, 2007 1:46 pm

form branching question

Post by cuttcards »

Hi guys,

This code below shows a form, and allows you to adjust the number of rocks you're entering, then returns to PHP_SELF so you can enter rocks.

When done, I hit the SEARCH button, which I'd like to pass values to a different page. [not shown] Unfortunately, it's not quite what I wanted...

Code: Select all

<?
session_start();
?>
  <?
  
  if(!isset($_SESSION['count'])) {
    $_SESSION['count'] = 4;
	$formaction=$_SERVER['PHP_SELF'];
  }
  if(isset($_SESSION['count']) && isset($_POST['ADD'])) {
    $_SESSION['count']++;
	$formaction = $_SERVER[’PHP_SELF’];
  }
  if(isset($_SESSION['count']) && isset($_POST['REMOVE'])) {
    $_SESSION['count']--;
	$formaction=$_SERVER[’PHP_SELF’];
  }
  if(isset($_POST['SEARCH'])) {
     $formaction="/results.php";
  }
	 
  $count = $_SESSION['count'];

  echo "<form id = \"form\" method=\"POST\" action=$formaction>";
  for ($i = 0; $i < $count; $i++) {
    echo "<p>";
    echo "Rock: <input type=\"text\" name=\"rock[]\" />";
    echo "Amount: <input type=\"text\" name=\"amount[]\" />";
    echo "Amount Type: <input type=\"text\" name=\"amounttype[]\" />";
    echo "</p>";
  }
  echo "<input type=\"submit\" value = \"Add another rock\" 
name=\"ADD\" />";
  echo "<input type=\"submit\" value = \"Remove a rock\"
name=\"REMOVE\" />";
  echo "<input type=\"submit\" value = \"Search\"
name=\"SEARCH\" />";
?>

Of course, a viewer needs to hit the SEARCH button twice... I'm trying to think of a different way to exit the form and pass values to a different page. Any ideas?

Cut
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Two thoughts:

1. If users often need to enter multiple rocks, why not provide sufficient number of textboxes, then when you process the form data, test each for null and only process those with data?

2. This would be a good application for Ajax, where you don't have to refresh the page, merely process the data and return new values (null, in this case) until user clicks "Done". I've been trying to justify learning Ajax, but I haven't found an application where it would really make sense. This one might.
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

you know, you dont even really need ajax for this... all it would take would be a little bit of javascript to add another bit to the end of the form, and then your form could just submit to your next page.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

maliskoleather wrote:you know, you dont even really need ajax for this... all it would take would be a little bit of javascript to add another bit to the end of the form, and then your form could just submit to your next page.
True, but if he's going to cycle back and get the same page, time after time, it would be a whole lot smoother not to have to reload the page every time. I'm just trying to think of situations where Ajax would be useful, and this seems to be one of those.
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

califdon wrote:True, but if he's going to cycle back and get the same page, time after time, it would be a whole lot smoother not to have to reload the page every time. I'm just trying to think of situations where Ajax would be useful, and this seems to be one of those.
it just appeared to me that the only reason he was cycling that page over was to add extra sections to the form... and if thats the case theres really no need to.

if theres other stuff going on, then yeah, AJAX would be the best method. :D

and a pretty basic example of AJAX can be found here. Once you understand whats going on, its one of the easiest things to learn ;)
cuttcards
Forum Newbie
Posts: 2
Joined: Sun May 20, 2007 1:46 pm

Post by cuttcards »

It occurred to me that maybe explaining what this thing is supposed to do might help...

Once I have my form of rocks, the rocks names are submitted to a database which returns characteristics for each rock: crystal structure, elements, average density, and mass (hence the amount field).

I could just put a static form up and check for null, but it seemed like an interesting way to add more textfields if for some reason I didn't have enough the first time.

The plan was to take this self-referencing page and submit it once I had all of my textfields finished - alas, hitting the other submit button twice is a pain.

Ajax is on the long list of things I'd like to learn for kicks, but I didn't really have a need up until now.
Post Reply