Jump to content

How to run different code based on which button is clicked?


MetalSmith

Recommended Posts

Hello,

 

Is there anyway to post different forms based on the submit button? I want to be able to click a submit button but have it post to a different php script based on the button pushed.

 

I have this which works ok but I need more submit buttons within each form.

</head>
<body>

<?php
?>

<form action = "analysis-1.php"
method = "post">

<input type="submit" name="submit" value="Analysis-1" />

</form>

<form action = "analysis-2.php"
method = "post">

<input type="submit" name="submit" value="Analysis-2" />
</form>

</body>
</html>

 

EDIT: Please use [code][/code] tags when posting code on the forums, and use descriptive topic subjects.

Link to comment
Share on other sites

You may have several submit buttons post to the same php file and then do differently depending on which one was used if that will work for you? Javascript and buttons will also do the job. It all depends on what you wish to accomplish, really. It's difficult for us to offer the best solution without knowing more.

Link to comment
Share on other sites

You could name each button differently and use PHP to check which one was pressed:

<?php
if(isset($_POST['button_1'])) {
    //do something
} elseif(isset($_POST['button_2'])) {
    //do something else
} elseif(isset($_POST['button_3'])) {
    //do another thing
}
?>

<form name="foo" action="" method="post">
    <input type="submit" name="button_1" value="Button 1" />
    <input type="submit" name="button_2" value="Button 2" />
    <input type="submit" name="button_3" value="Button 3" />
</form>

Link to comment
Share on other sites

Well my thinking is that I have several php scripts that run certain reports. I was thinking the best way to do it was to have a submit button pushed to run a certain php script.

 

Like by pushing "phone report" analyzes our phone usage.

Then another would be "video conference" analyzes our video conference usage.

 

So each button is attached to a php script. Maybe the way you posted would work. I need to think about it more.

 

 

Thanks!

 

Link to comment
Share on other sites

This is what I have very simple. If I can do this within one form that would give me some flexability to position my submit buttons. Otherwise I can't group them together. I would say this is more like links than an actual form. Its not gathering any data from a user. It just takes the persons uploaded file and analyzes it.

 

<body>

 

<p style="text-align: center;">

 

<img src="logo.jpg" width="192" height="56" border="0" />

 

</p>

 

<hr align="center" width="100%" size="0" noshade />

 

<br />

 

<?php

 

    if (move_uploaded_file ($_FILES['thefile']['tmp_name'], "{$_FILES['thefile']['name']}"))

 

    {

 

    print '<p style="text-align: center;">

    <font size="+2">Your file was successfully uploaded.</font>

    </p>';

 

    } else {

 

    print '<p style="text-align: center;">Your file could not be uploaded because: <b>';

 

      switch ($_FILES['thefile']['error'])

 

      {

 

      case 1:

        print 'The file exceeds the upload_max_filesize setting in php.ini';

        break;

 

      case 2:

        print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';

        break;

 

      case 3:

        print 'The file was only partially uploaded';

        break;

 

      case 4:

        print 'No file was uploaded';

        break;

 

      }

 

      print '</b>.</p>';

 

    }

 

?>

 

<form action = "analysis-1.php"

method = "post">

 

 

 

<input type="submit" name="submit" value= "Analysis-1" />

 

 

</form>

 

<form action = "analysis-2.php"

method = "post">

 

 

 

<input type="submit" name="submit" value= Analysis-2" />

 

 

</form>

 

</body>

</html>

 

Link to comment
Share on other sites

Ok, well, since the user is uploading files there needs to be a form. Here's what seems to be the best solution:

 

1. Use one single form with several differently named submit buttons

2. Check which submit button was used in the target file

3. Do the analysis depending on 2.

 

Setting up functions for the various analysis methods seems like a good idea, if they do not already exist.

 

You need to add enctype to your form but maybe you already know that?

Link to comment
Share on other sites

You could name each button differently and use PHP to check which one was pressed:

<?php
if(isset($_POST['button_1'])) {
    //do something
} elseif(isset($_POST['button_2'])) {
    //do something else
} elseif(isset($_POST['button_3'])) {
    //do another thing
}
?>

<form name="foo" action="" method="post">
    <input type="submit" name="button_1" value="Button 1" />
    <input type="submit" name="button_2" value="Button 2" />
    <input type="submit" name="button_3" value="Button 3" />
</form>

instead of this i would use a switch like this

 

<?php
switch ($_POST['submit']){
    case 'Button 1':
        include('file1.php');
    break;
case 'Button 2':
        include('file2.php');
    break;
case 'Button 3':
        include('file3.php');
    break;
}
?>

<form name="foo" action="" method="post">
    <input type="file" />
    <input type="submit" name="submit" value="Button 1" />
    <input type="submit" name="submit" value="Button 2" />
    <input type="submit" name="submit" value="Button 3" />
</form>

 

just my preference,

include the file that you want to handle that request

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.