Jump to content

Recommended Posts

Hello!  I'm just starting to learn PHP, and I'm tinkering around with making some pages myself.  I would like to make a page with two form buttons that can do two different things, say, clicking one creates a new entry in a database and clicking another lists all the data currently in the database (These actions could be anything for example's sake).

 

I've gone through the w3schools PHP tutorial and have poked around on other PHP sites, but I can't seem to find how to do this.  In javascript I can simply write a function() and then have the onclick of the button call the function, but no luck with PHP.

 

How do I do this, and where can I go to find some tutorials that will give me a better understanding of having different buttons do different things?  Thanks so much!

Hmm...ok, well, I tried to throw some PHP code in the middle of some javascript, but with no avail...

 

<html>
<head>
<script type="text/javascript">
function displaymessage()
{
<?php echo "Hey"; ?>
}
</script>
</head>

<body>
<form>
<input type="button" value="Click me!" onclick="displaymessage()" >
</form>
</body>
</html>

 

...is this the right direction?

There are different ways to approach this. You could make each button a submit button with different values and then place them in different form tags. Then the page receiving the submission can test the value of $_POST['submit']

<html>
<head></head>
<body>

<?php

if (isset($_POST['submit'])) {
  if ($_POST['submit']=="Choice 1") {
    echo "Do something for choice 1";
  } else {
    echo "Do something for choice 2";
  }
}

?>

<br><br>

<form name="test" action="<?=$PHP_SELF?>" method="POST">
<button type="submit" name="submit" value="Choice 1">Choice 1</button>
</form>

<form name="test" action="<?=$PHP_SELF?>" method="POST">
<button type="submit" name="submit" value="Choice 2">Choice 2</button>
</form>

</body>
</html>

 

Or, you could use two different submit buttons in the same form and use javascript tp populate a hidden field.

<html>
<head></head>
<body>

<?php

if (isset($_POST['choice'])) {
 if ($_POST['choice']=="Choice 1") {
   echo "Do something for choice 1";
 } else {
   echo "Do something for choice 2";
 }
}


?>

<br><br>
<form name="test" action="<?=$PHP_SELF?>" method="POST">
<input type="hidden" name="choice" id="choice">
<button type="submit" onclick="document.getElementById('choice').value='Choice 1'">Choice 1</button>
<button type="submit" onclick="document.getElementById('choice').value='Choice 2'">Choice 2</button>
</form>
</body>
</html>

 

There are other ways as well.

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.