Reputation: 471
<div class = 'buttons'>
<button type="submit" class="regular" name="save">
<img src="elephant.png" alt=""/>
Memory
</button>
</div>
This is the code for a submit button
however when i try validate the form using php, the button does not seem to be submitting
<?php if( isset($_POST['save']) && $_POST['save'])
{
$submit = $_POST['save'];
echo "lol";
}
else
{
echo "lola";
}
Upvotes: 3
Views: 16894
Reputation: 892
Here is the code that you want:
<form name="myForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div class="buttons">
<button type="submit" class="regular" name="save" value="Save">
<img src="elephant.png" alt="Elephant" />
Memory
</button>
</div>
</form>
Upvotes: 1
Reputation: 2181
<form method="post" action="path/to/your/php/processing/file">
and </form>
tags<input type="submit" class="regular" name="save" />
isset($_POST['save']) &&
part from condition of if as you don't have set value
for your button.Upvotes: 0
Reputation: 3615
As written already your code lacks the wrapping. While submit button as a button might work without the form, then catching the _POST data surely doesnt.
Also, your solution for image-button could be better, maybe something like this:
<style type="text/css">
input[type="submit"] {background: url('elephant.png') right no-repeat; padding: 10px; padding-left: 30px; font-size: 14px; font-weight: bold;} /* or call this .regular */
</style>
<?
if ($_POST['save']) { // the trigger
echo '<h1>Cooking with fire now!</h1>';
}
?>
<form method="POST" action="">
<div class="buttons">
<input type="submit" name="save" value="Memory" class="regular" />
</div>
</form>
class = "buttons"
with the spaces, is incorrect syntax! Upvotes: 0
Reputation: 54729
Your submit button doesn't have any value to send when posting the form. The <button>
element does not send its element's content as its submit value. You still have to add a value
attribute to the element in order for it to have a submitted value. Since you don't, it's sending an empty string, which is causing your conditional statement to fail:
if( isset($_POST['save']) && $_POST['save'])
Since $_POST['save']
is empty, that second part returns false and thus it goes to your else
block.
Upvotes: 1
Reputation: 974
Submit buttons only work in forms. Try to wrap the whole thing in a form
tag and try again.
Upvotes: 3