开发者

Two image buttons or two submits

I'm a .NET developer almost giving up on a personal project using PHP.

I want the following PHP page:

A form with two random images (identified by a code), where the user chooses between them, then the form refreshes, taking care of the choice. Simple.

I've already done the random image part (stored URL's in a table. I plan on changing that in the future, but that's not an issue for now), so I've got to make the "choosing" part. I'd like to either make both the images clickable, or put two buttons below them with the same text.

In PHP I can't figure this 'submit' way to work, mostly because I'm too used to ASP.NET.

I've tried using two normal submit buttons, but I don't want them both to have different texts, but the very same property used to distinguish both buttons is also used to write the text of the button: value. Not to mention that this whole PHP_SELF along with checking the value at the beginning of the page load seems too hacky.

Then I've tho开发者_运维百科ught about making two forms, each with an action that sends the code via querystring, but I don't want that. I don't want the (regular) user to be able to do the action just by typing in the URL.

Remember: since I lose the variable values when the page is refreshed, I have to send the selected image's code along (not via querystring).

This kind of conflict is what is making me consider giving up this project. When I've learned ASP.NET by myself this was so easy, intuitive, and I've figured it out quickly.


How about giving the submit buttons two different names:

<input type="submit" name="submit1" value="submit" />
<input type="submit" name="submit2" value="submit" />

Then you could check with php doing the following:

if (isset($_REQUEST['submit1']) && $_REQUEST['submit1'] == "submit") {
    //its image 1
} else if (isset($_REQUEST['submit2']) && $_REQUEST['submit2'] == "submit") {
    //its image 2
}

By the way, you don't have to use the $_REQUEST array, you can use $_GET or $_POST depending on your form action. I wasn't sure which one you were using so I put $_REQUEST here which covers both.


So first you need to create your form to be submitted via POST instead of GET:

<form name="myform" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <button type="submit" name="submitButton" value="image1"><img src="xxx"></button>
    <button type="submit" name="submitButton" value="image2"><img src="xxx"></button>
</form>

Now on the PHP side:

if(isset($_POST['submitButton'])) {
    $image = $_POST['submitButton']; 
}

If a user clicks on a button that piece of code would be triggered.

Here's a full example of how tis would all fit together:

<?php
if(isset($_POST['submitButton'])) {
    echo $_POST['submitButton'];
}
?>
<html>
    <body>
        <form name="myform" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
            <button type="submit" name="submitButton" value="image1"><img src="xxx"></button>
            <button type="submit" name="submitButton" value="image2"><img src="xxx"></button>
        </form>
    </body>
</html>


For the submit button problem, you can use the name attribute of the submit button to determine which button was clicked.

This clicked button will provoke a key to appear in $_POST array. That way, you can distinguish the values but still have the same text/value.


Just have one form. That's really all you need.

Give the submit buttons the same name and different values.

<input type="submit" name="submit" value="Option1" />
<input type="submit" name="submit" value="Option2" />

When the form is posted, you can check $_POST['your_submit_value_name'] for each one of the values.

Additionally, if you leave the <form action="" method="post" enctype="multipart/form-data"> as blank, it will post to itself. So when you load your page, just check for:

if (!empty($_POST)) { 
    if ($_POST['submit'] == 'value1') {
        // Do A
    } else if ($_POST['submit'] == 'value2') {
        // Do B
    }
}

Dont't forget to validate the user data!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜