Jump to content

[SOLVED] two button form


ag3nt42

Recommended Posts

<?php

echo "<h1> Hello $name </h1>";

if ($action == "b1")
echo "you just clicked button 1";
if ($action == "b2")
echo "you just clicked button 2";

?>

Thats because $action is not defined in this php file. Define action before the if statements and it will work

Link to comment
Share on other sites

Like this:

 

<?php

if(isset($_POST['action'])) {
$action = $_POST['action'];
$name = $_POST['name'];

echo "<h1> Hello $name </h1>";

if ($action == "b1")
	echo "you just clicked button 1";
if ($action == "b2")
	echo "you just clicked button 2";
}

echo '
<form  method="post" action= "testpage.php">
Name : <input type="text" name="name" size="30" value="" maxlength="60"/><br /><br />
<input type="submit" value="b1" name="action" />
<input type="submit" value="b2" name="action" /><br /><br />
</form>';
?>

Link to comment
Share on other sites

You first need to get the POST data from the form.

 

<form action="my_form.php" method="POST">
 <p>Name: <input type="text" name="name" /></p>
 <p>
   <input type="submit" name="submit" value="Go" />
   <input type="submit" name="submit" value="Cancel" />
 </p>
</form>

 

my_form.php

<?php
$action = $_POST['submit'];

if(isset($action)) {
 switch ($action) {
   case 'Go':
     print 'Hello ' . $_POST['name'] . ', you hit ' . $action;
     break;
   case 'Cancel':
    print 'Hello ' . $_POST['name'] . ', you hit ' . $action;
    break;
 }
}
?>

   

Link to comment
Share on other sites

More wasted time due to register_globals... Laughingly, register_globals were created to save a little typing time for lazy programmers. The problems with them have easily cost a thousand times more wasted time then what they ever saved in typing time.

 

Register_globals were depreciated and turned off long ago in php4.2 in the year 2002. No new code, new books, new tutorials, or new hosting accounts should have been created after that point in time that used register_globals. Register_globals have been completely eliminated in upcoming php6.

 

That site is doing you and anyone else who finds it a huge disservice by listing code that is dependent on register_globals, a full six years after all programmers should have stopped using register_globals.

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.