RIRedinPA Posted January 7, 2008 Share Posted January 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84880-form-with-images-for-submit-buttons/ Share on other sites More sharing options...
chronister Posted January 7, 2008 Share Posted January 7, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/84880-form-with-images-for-submit-buttons/#findComment-432766 Share on other sites More sharing options...
wildteen88 Posted January 7, 2008 Share Posted January 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84880-form-with-images-for-submit-buttons/#findComment-432862 Share on other sites More sharing options...
Taorluath Posted January 7, 2008 Share Posted January 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84880-form-with-images-for-submit-buttons/#findComment-432920 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.