Form button not passing its value
Im new to PHP and am currently making a basic calculator, it is working perfectly when I use a drop down menu to select the operation (addition, subtraction etc) however I now want to change it so that you select the operation by clicking the appropriate button. (like a real calculator) The actual calculation is done in the class - calculator and I am using extract ($_GET) to pull in all the values from the form.
I have used <button.....> </button> as I have read it is more functional then <input type="button" etc...> but I have tried using both methods yet the PHP is unable to receive any value from any of the buttons.
Code is below, any help would be brilliant. Thank You.
Welcome to the forum. Please edit your first post to enclose your code in [CODE][/CODE] tags. It makes things much easier on the eyes. :D
Now, as for the buttons, as far as I am aware forms should be using <input type="submit" ......./> elements for sending form data. This is what I routinely use and I never have issues with it. The submit button really only submits the name of the button and the value on the button. So, for example, this button:
That comes in handy if you have more than one type of submit button (such as "save and add another" vs. "save and go to next step" buttons) and need to pay attention to the user's intended submit type.
Now, if you simply want to pass other ancillary variables you should either be keeping/setting session variables or else using hidden input elements to pass the data like so:
I understand what you are saying about using <input type="submit" name="opp" value="+"/> however any input type="submit" will run the form method and action once clicked.
Obviously when using a calculator the user will enter an x value then click one of the operation buttons (+, -, x, / etc) then they will enter their y value and click '=' (Currently my '=' button is input type="submit") if I make any other button submit it will send the data to early if you follow me?
I cannot use hidden input fields as the operation must be selected by the user.
I will upload the working version (which uses a drop down menu to select the operation) to the following link - Working calculator
Here is the version which isnt working with the operation selected by buttons - Button calculator
Thanks in advance.
Thank you for trying tho :thumbsup:
I'm no Pro, but here's some basic fodder the input vs button debate.
Based on w3schools, it kinda sounds like input may be preferred over button.
Now as for buttons (whether via <input> or <button>), aren't they and actionable element?
If my presumption is correct, then the buttons are working exactly how they are supposed to. They're doing nothing :)
What about getting rid of the "=" button, and make each of the operator buttons into submit buttons.
Rowsdower kinda touched on this with:
1) X valueto:
1) X valueHTH.
I had thought about that but its a last ditch method for me, basically because I want to make it as close to a real life physical calculator as possible....but it would seem PHP doesn't really make this easy...or perhaps even possible....
Perhaps I should go and learn some C lol
On further reflection, it seems to me as though you really could do this with all submit buttons and a bunch of conditional tests. It would load the page with every button press and it would be very slow and annoying, but it could technically be done the way you are asking about.
Really, what you're limited by here is the HTML form, not the PHP. But really, HTML was not meant to be dynamic on its own anyway. Your problem lies on the client-side and no amount of server-side-only solution can fix the problem entirely.
|All times are GMT +1. The time now is 01:49 AM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.