Jump to content

Recommended Posts

Hi

 

I have a form which has two images I am using in lieu of the submit button - one is for saving the form data to a mysql database, cleverly titled "Save" and the second is for submitting the form, which would then trigger a validation script and then (if it validates) also save to the database and email form data to relevant parties.

 

so I have these two inputs:

 

<input type="image" src="images/btn_save.gif" name="btn_save">

<input type="image" src="images/btn_submit.gif" name="btn_submit">

 

the form action just reloads the page and data is sent via POST.

 

my question is how do I check which button the user selected? If they were just two submit buttons I could do something like

 

if(isset("btn_submit")) { //do submit stuff }

 

but what kind of value will an image pass? Thanks

 

Just to clarify - when I do echo $_POST['btn_submit'] I get no value (after clicking the submit button)

 

I also tried this

 

if ($POST['submit_x']) { echo "an image was used to submit the form"; } 

 

and one other problem, when I click the submit button all the data is sent via GET, not POST...the save button does nothing.

Link to comment
https://forums.phpfreaks.com/topic/84880-form-with-images-for-submit-buttons/
Share on other sites

Give the buttons a value like I did below and just check for those values. the sample I have below works perfectly. It will tell me which button is pressed every time.

 

 

<?php
if(isset($_POST['btn_save']) && $_POST['btn_save']=='save')
{ // we make sure that the post var exists and then we check to see if it equals 'save'
echo 'Save Button Was Pressed';
}
elseif(isset($_POST['btn_submit']) && $_POST['btn_submit']== 'submit')
{ // we make sure that the post var exists and then we check to see if it equals 'submit'
echo 'Submit Button Was Pressed';
}
?>

<form  method="post">
<input type="image" src="images/btn_save.gif" name="btn_save" value="save">
<input type="image" src="images/btn_submit.gif" name="btn_submit" value="submit">
</form>

 

and one other problem, when I click the submit button all the data is sent via GET, not POST...the save button does nothing.

If the from data is being sent by GET why are you using $_POST to recieve the form data that was submitted. You should use $_GET instead.

 

Also when using an image as a button you have to use $_GET['(btn_name)_x'] or $_GET['(btn_name)_y'] in order to receive the variable associated to the image button, This is because the browser sends the x and y co-ordinates of the location of the cursor when the button was clicked. Note: Change (btn_name) to the actual name of your button.

Couldn't you just use something like this?

<button type ='submit' name='btn_save'><img src='images/btn_save.gif' alt='Save' /></button>
<button type ='submit' name='btn_submit'><img src='images/btn_submit.gif' alt='Submit' /></button>

This seems the easiest way to do it. Also if the user is using a text-based browser, this way he/she will still know that these are submit buttons. (and be able to distinguish the different purposes of each button)

 

This way, they ARE submit buttons, but all people see is images really. (there will be the grayness of a submit button under it and the border of one around it, so it may not appear how you like it. If it looks bad, you can just use CSS to remove the submit-button-like appearance of it.)

 

Then you can just use:

if(isset($_POST['btn_save']))  {//execute save stuff}

like you wanted.

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.