Jump to content

Php and forms


bob_rock

Recommended Posts

Hi , I just started to learn php and I ran into a problem.The script should work like this when you press send button it will show the form inputs and go to the other form and when you press send on the other form it does something else....

I understand condition if (!$_POST['xxx']) , but when i press submit on the other form it just starts the script from the beginning ... Thank you

 

This is just a simple example how i imagine the structure of the script, is this correct ?  ???

<?php
if (!$_POST['button1'])
{
?>
<form method="post" action="<?php $PHP_SELF?>"/>
Name one: <input type="text" name="name" size="10" /><br />
<input type="submit" name="button1" value="Send!" />
<?php
}
else
{
	$name = $_POST['name'];
	echo $name;
	if (!$_POST['button2'])
	{
	?>
	<form method="post" action="<?php $PHP_SELF?>"/>
	Name two: <input type="text" name="name2" size="10" /><br />
	<input type="submit" name="button2" value="Send" />
	<?php
	}
	else
	{
	$name2 = $_POST['name2'];
	echo $name2;
	}
}
?>

 

Link to comment
Share on other sites

I only looked very quickly, however, i think you need to change this line:

 

if (!$_POST['button1'])

 

To:

 

if (!$_POST['button1'] && !$_POST['button2'])

 

Since you need to check if neither button have been pressed, which only occurs at the very start. The reason why it appears to 'start over' is that when you submit the form the second time, $_POST['button1'] is again false, as this hasn't been clicked.

 

As an aside, using isset() is better practice than just testing if a variable is false. So you would do:

 

if(!isset($_POST['button1']) && !isset($_POST['button2']))

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.