Jump to content

[SOLVED] Two Forms on One Page?


Cetanu

Recommended Posts

I am trying to make a page where someone selects an option on one form, a code is executed depending on the selection, and then a new form appears for another option.

 

Could it be that my forms are clashing into each other because when I have them both on one page AND them action="" set to the same page, they don't work. If I have them on the same page but they have different actions, it works.

:shrug:

 

 

Link to comment
Share on other sites

I do not want to redirect my users to another page and then back (because the one with the different action redirects them to its action). Also, when it redirects them I cannot execute code based on whether or not the second form was completed AFTER they were sent away and back.

 

here:

FORM 1

<form action='fcenter.php?page=planet1' method='post'>
		<select size='4' name='fight'>
		<option value='Aliens'>Aliens</option>
		<option value='Marines'>Marines</option>
		<option value='Native Animals'>Native Species</option> 
		<option value='Totally Random'>Randomize</option>
		</select> 
		<input type='submit' name='choose' value='Search'/> 
		</form>

 

FORM 2

 <form action='fcenter.php?page=planet1' method='post'>
					<input type='radio' name='attack' value='Attack'/>Attack<br/>
					<input type='radio' name='attack' value='Flee'/> Flee <br/> 
					<input type='submit' name='confirm' id='confirm' value='Choose'/>
					</form>

 

Ideal situation:

 

Member answers form 1 --> Form 2 comes up (answers form 2) --> Code is executed based on what they chose on form 2 (this code involves $_SESSIONs)

 

What it is doing is this:

 

Member answers form 1 --> Form 2 comes up (with different action) --> Send to enemy.php --> Redirect back to original page --> Now the original page is reloaded so I cannot execute the code based on form 2!!!!

 

 

What if form 1 was a $_GET form? >.> <.< then I could redirect them to...the page they chose?  :shrug: :shrug: :confused:

Link to comment
Share on other sites

What you are hoping for will require some javascript. PHP is executed on the server, javascript is executed in the browser.

 

 

Or you can have the form's action point the the same page...then have a hidden input on each form.  the hidden input will give an if statement or a switch to choose a peice of code to execute

Link to comment
Share on other sites

The php way would be something like this

 

<?php
if ($_POST['action'] == 1) { /* DO SOMETHING */}
else { /* DO SOMETHING ELSE */ }
?>


<form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="action" type="hidden" value = "1">
<input="submit" name="submit" value="submit"> 
</form>

<form name="form2" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="action" type="hidden" value = "2">
<input="submit" name="submit" value="submit"> 
</form>

Link to comment
Share on other sites

The php way would be something like this

 

<?php
if ($_POST['action'] == 1) { /* DO SOMETHING */}
else { /* DO SOMETHING ELSE */ }
?>


<form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="action" type="hidden" value = "1">
<input="submit" name="submit" value="submit"> 
</form>

<form name="form2" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="action" type="hidden" value = "2">
<input="submit" name="submit" value="submit"> 
</form>

 

I don't get it, that's supposed to do what I want? I tried $_SERVER but it doesn't work because the page it is supposed to execute on is a switch (?page=planet1), when I tried it it redirected me to the main page with out the switch.

Link to comment
Share on other sites

Ideal situation:

 

Member answers form 1 --> Form 2 comes up (answers form 2) --> Code is executed based on what they chose on form 2 (this code involves $_SESSIONs)

 

What it is doing is this:

 

Member answers form 1 --> Form 2 comes up (with different action) --> Send to enemy.php --> Redirect back to original page --> Now the original page is reloaded so I cannot execute the code based on form 2!!!!

 

 

What if form 1 was a $_GET form? >.> <.< then I could redirect them to...the page they chose?  :shrug: :shrug: :confused:

 

why exactly is the form sending to enemy.php and redirecting? how are you trying to process the forms on fcenter.php?

 

if the first form is sent, then $_POST['choose'] will be set. to execute the code for form1 on submission, use:

 

if (isset($_POST['choose']))
{
  // form1 execution
}

 

if the second form is sent, then $_POST['confirm'] will be set, but not $_POST['choose']. for the form2 code execution, you can use:

 

if (isset($_POST['confirm']))
{
  // form2 execution
}

 

these can easily go on the same page in the header, and only one statement will process on any given page submission. in fact, to make it all streamlined, you could go:

 

if (isset($_POST['choose']))
{
  // form1 was submitted
}
elseif (isset($_POST['confirm']))
{
  // form2 was submitted
}
else
{
  // nothing was submitted
}

 

perhaps i'm missing something here..

 

EDIT: note that if you want the information from form1 to post along with the information selected/posted from form2, you will need to add hidden inputs into form2's code to plug that information in.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.